/
listSessionCmd.manual.go
219 lines (177 loc) · 5.36 KB
/
listSessionCmd.manual.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/manifoldco/promptui"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
type listSessionCmd struct {
*baseCmd
sessionFilter string
}
func newListSessionCmd() *listSessionCmd {
ccmd := &listSessionCmd{}
cmd := &cobra.Command{
Use: "list",
Short: "Get a Cumulocity session",
Long: `Get a Cumulocity session`,
Example: `
Example 1: Show an interactive list of all available sessions
#> c8y sessions list
Example 2: Select a session and filter the selection of session by the name "customer"
#> export C8Y_SESSION=$( c8y sessions list --sessionFilter "customer" )
`,
RunE: ccmd.listSession,
}
cmd.SilenceUsage = true
cmd.Flags().StringVar(&ccmd.sessionFilter, "sessionFilter", "", "Filter to be applied to the list of sessions even before the values can be selected")
ccmd.baseCmd = newBaseCmd(cmd)
return ccmd
}
func matchSession(session CumulocitySession, input string) bool {
// strip url scheme
uri := strings.ReplaceAll(session.Host, "https://", "")
uri = strings.ReplaceAll(uri, "http://", "")
name := strings.ToLower(fmt.Sprintf("#%02d %s %s %s %s",
session.Index,
filepath.Base(session.Path),
uri,
session.Tenant,
session.Username,
))
input = strings.ToLower(input)
searchTerms := strings.Split(input, " ")
match := true
for _, term := range searchTerms {
if !strings.Contains(name, term) {
match = false
}
}
return match || input == ""
}
func (n *listSessionCmd) listSession(cmd *cobra.Command, args []string) error {
config := &CumulocitySessions{}
config.Sessions = make([]CumulocitySession, 0)
subDirToSkip := "ignore"
files := make([]string, 0)
outputDir := getSessionHomeDir()
Logger.Infof("using c8y session folder: %s", outputDir)
err := filepath.Walk(outputDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
if info.IsDir() && info.Name() == subDirToSkip {
fmt.Printf("skipping a dir without errors: %+v \n", info.Name())
return filepath.SkipDir
}
// skip settings file
if strings.HasPrefix(info.Name(), SettingsGlobalName+".") {
return nil
}
Logger.Infof("Walking folder/file: %s", path)
files = append(files, path)
if session, err := NewCumulocitySessionFromFile(path); err == nil {
config.Sessions = append(config.Sessions, *session)
}
return nil
})
if err != nil {
cmd.PrintErrf("Failed to walk directory. %s", err)
}
// Add index numbers
for i := range config.Sessions {
config.Sessions[i].Index = i + 1
}
// template.Fun
funcMap := promptui.FuncMap
funcMap["hide"] = func(v interface{}) string {
if !strings.EqualFold(os.Getenv(c8y.EnvVarLoggerHideSensitive), "true") {
return fmt.Sprintf("%v", v)
}
return "*****"
}
funcMap["hideUser"] = func(v interface{}) string {
msg := fmt.Sprintf("%v", v)
if !strings.EqualFold(os.Getenv(c8y.EnvVarLoggerHideSensitive), "true") {
return msg
}
if os.Getenv("USERNAME") != "" {
msg = strings.ReplaceAll(msg, os.Getenv("USERNAME"), "******")
}
return msg
}
templates := &promptui.SelectTemplates{
// Label: "{{ .Host }}?",
Active: `-> {{ printf "#%02d: %-25s" .Index .Name | cyan }} {{ .Host | hide | magenta }} {{ printf "(%s/" .Tenant | hide | red }}{{ printf "%s)" .Username | hide | red }}`,
Inactive: ` {{ printf "#%02d: %-25s" .Index .Name | cyan }} {{ .Host | hide | magenta }} {{ printf "(%s/" .Tenant | hide | red }}{{ printf "%s)" .Username | hide | red }}`,
Selected: "{{ .Path | hideUser }}",
FuncMap: funcMap,
Details: `
--------- Details ----------
{{ "File:" | faint }} {{ .Path | hideUser }}
{{ "Host:" | faint }} {{ .Host | hide }}
{{ "Tenant:" | faint }} {{ .Tenant | hide }}
{{ "Username:" | faint }} {{ .Username | hide }}
`,
}
filteredSessions := make([]CumulocitySession, 0)
sessionIndex := 1
for _, session := range config.Sessions {
if matchSession(session, n.sessionFilter) {
session.Index = sessionIndex
filteredSessions = append(filteredSessions, session)
sessionIndex++
}
}
searcher := func(input string, index int) bool {
session := filteredSessions[index]
return matchSession(session, input)
}
prompt := promptui.Select{
Stdout: &bellSkipper{}, // Workaround to pervent the terminal bell on MacOS
HideSelected: false,
IsVimMode: false,
StartInSearchMode: false,
Label: "Select a Cumulocity Session",
Items: filteredSessions,
Templates: templates,
Size: 10,
Searcher: searcher,
}
var idx int
var result string
if len(filteredSessions) == 1 {
Logger.Info("Only 1 session found. Selecting it automatically")
idx = 0
result = filteredSessions[0].Path
err = nil
} else {
idx, result, err = prompt.Run()
}
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return err
}
// check if the user cancelled the select (i.e. ctrl+c)
if result == "" {
// required inorder to flush the screen buffer
fmt.Println("")
return nil
}
if idx >= 0 && idx < len(filteredSessions) {
fmt.Printf("%s", filteredSessions[idx].Path)
} else {
fmt.Println("")
}
return nil
}
func (n *listSessionCmd) formatFilename(name string) string {
if !strings.HasSuffix(name, ".json") {
name = fmt.Sprintf("%s.json", name)
}
return name
}