forked from Luzifer/awsenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdShell.go
57 lines (45 loc) · 1.38 KB
/
cmdShell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"os"
"strings"
"github.com/Luzifer/awsenv/shellsupport"
log "github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
_ "github.com/Luzifer/awsenv/shellsupport/bash"
_ "github.com/Luzifer/awsenv/shellsupport/fish"
)
func getCmdShell() *cobra.Command {
cmd := cobra.Command{
Use: "shell [environment]",
Short: "print the AWS credentials in a format for your shell to eval()",
Run: actionCmdShell,
}
cmd.Flags().StringVarP(&cfg.Shell.Shell, "shell", "s", os.Getenv("SHELL"), "name of the shell to export for")
cmd.Flags().BoolVarP(&cfg.Shell.Export, "export", "x", true, "Adds proper export options for your shell")
return &cmd
}
func actionCmdShell(cmd *cobra.Command, args []string) {
if len(cfg.Shell.Shell) == 0 {
log.Errorf("Could not determine your shell. Please provide --shell")
os.Exit(1)
}
s := strings.Split(cfg.Shell.Shell, "/")
shell := s[len(s)-1]
log.Debugf("Found shell '%s'", shell)
handler, err := shellsupport.GetShellHandler(shell)
if err != nil {
log.Errorf("Could not find a handler for '%s' shell", shell)
os.Exit(1)
}
if len(args) < 1 {
log.Errorf("Please specify the enviroment to load")
os.Exit(1)
}
if a, ok := awsCredentials.Credentials[args[0]]; ok {
fmt.Println(strings.Join(handler(a, cfg.Shell.Export), "\n"))
os.Exit(0)
}
log.Errorf("Could not find environment '%s'", args[0])
os.Exit(1)
}