/
list.go
78 lines (69 loc) · 1.82 KB
/
list.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cmd
import (
"fmt"
"io/ioutil"
"os"
"github.com/mitchellh/go-homedir"
"github.com/olekukonko/tablewriter"
"github.com/pelletier/go-toml"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var listCmd = &cobra.Command{
Use: "list",
Short: "List all available profiles",
Long: "",
PreRun: func(cmd *cobra.Command, args []string) {
if verbose {
log.SetLevel(log.DebugLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
home, err := homedir.Dir()
if err != nil {
log.Fatal("unable to find home directory: ", err)
}
configData, err := ioutil.ReadFile(home + defaultFullConfigPath)
if err != nil {
log.Fatal(err)
}
config := tomlConfig{}
err = toml.Unmarshal(configData, &config)
if err != nil {
log.Fatal(err)
}
if len(config.Profiles) == 0 {
fmt.Printf("no profiles found at %s\n", home+defaultFullConfigPath)
os.Exit(0)
}
tableData := [][]string{}
for profileName, profile := range config.Profiles {
// Only display the email if we're using API tokens otherwise the value is
// not used and pretty superfluous.
var emailString string
if profile.AuthType == "api_key" {
emailString = profile.Email
}
tableData = append(tableData, []string{
profileName,
profile.AuthType,
emailString,
})
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Profile name", "Authentication type", "Email"})
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t")
table.SetNoWhiteSpace(true)
table.AppendBulk(tableData)
table.Render()
},
}