-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_list.go
More file actions
90 lines (79 loc) · 1.89 KB
/
auth_list.go
File metadata and controls
90 lines (79 loc) · 1.89 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
package cmd
import (
"fmt"
"github.com/loops-so/cli/internal/config"
"github.com/spf13/cobra"
)
var authListCmd = &cobra.Command{
Use: "list",
Short: "List stored API keys",
RunE: func(cmd *cobra.Command, args []string) error {
if err := validatePickFlags(cmd); err != nil {
return err
}
entries, activeTeam, err := runAuthList()
if err != nil {
return err
}
if isJSONOutput() {
type jsonEntry struct {
Name string `json:"name"`
APIKey string `json:"apiKey"`
Active bool `json:"active"`
}
out := make([]jsonEntry, len(entries))
for i, e := range entries {
out[i] = jsonEntry{e.Name, maskKey(e.APIKey), e.Name == activeTeam}
}
return printJSON(cmd.OutOrStdout(), out)
}
if len(entries) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "No keys stored.")
return nil
}
headers := []string{"NAME", "ACTIVE", "API KEY"}
rows := make([][]string, 0, len(entries))
for _, e := range entries {
active := ""
if e.Name == activeTeam {
active = "*"
}
rows = append(rows, []string{e.Name, active, maskKey(e.APIKey)})
}
if isPicking(cmd) {
out := cmd.OutOrStdout()
return runPicker(headers, rows, []pickBinding{{
Key: "enter",
Label: "switch team",
Action: func(rowIdx int) error {
name := entries[rowIdx].Name
if err := runAuthUse(name); err != nil {
return err
}
fmt.Fprintf(out, "Active team set to %q.\n", name)
return nil
},
}})
}
t := newStyledTable(cmd.OutOrStdout(), headers...)
for _, r := range rows {
t.Row(r...)
}
return t.Render()
},
}
func runAuthList() ([]config.KeyEntry, string, error) {
entries, err := config.ListKeys()
if err != nil {
return nil, "", err
}
pc, err := config.LoadPersistentConfig()
if err != nil {
return nil, "", err
}
return entries, pc.ActiveTeam, nil
}
func init() {
addPickFlag(authListCmd)
authCmd.AddCommand(authListCmd)
}