-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
183 lines (165 loc) · 3.68 KB
/
input.go
File metadata and controls
183 lines (165 loc) · 3.68 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
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
package main
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
// "./k8s"
"wen/gotty-pod/k8s"
// "github.com/AlecAivazis/survey"
prompt "github.com/c-bata/go-prompt"
jwt "github.com/dgrijalva/jwt-go"
"gopkg.in/AlecAivazis/survey.v1"
)
func VerifyPermission() (err error) {
token, err := gettokenfrominput()
if err != nil {
return
}
// fmt.Printf("got token: %q", token)
err = validateJWT(token)
if err != nil {
err = fmt.Errorf("validate token err %v", err)
return
}
return
}
func gettokenfrominput() (token string, err error) {
prompt := &survey.Input{
Message: "Enter token for permission( ask yunwei for it): ",
}
err = survey.AskOne(prompt, &token, nil)
if err != nil {
err = fmt.Errorf("enter token err %v", err)
return
}
// token = answers.Token
return
}
var SecretKey = "secret"
func validateJWT(token string) error {
if token == "" {
return fmt.Errorf("token is empty")
}
t, err := jwt.Parse(token, func(tok *jwt.Token) (interface{}, error) {
return []byte(SecretKey), nil
})
if err == nil && t.Valid {
return nil
} else {
return fmt.Errorf("Invalid")
}
}
// type inputentry struct {
// id int
// text string
// }
func GetProject(token string, admin bool) (pod k8s.Pod, err error) {
cancelprint := printprogress()
grouplist, err := GetGroupLists(token)
cancelprint()
if err != nil {
err = fmt.Errorf("get project lists err: %v", err)
return
}
pod, err = GetProjectFromInput(grouplist, admin)
if err != nil {
err = fmt.Errorf("get project from input err: %v", err)
return
}
return
}
// Get project by user input
func GetProjectFromInput(gitlist []string, admin bool) (pod k8s.Pod, err error) {
podlist, e := listpods()
if e != nil {
err = fmt.Errorf("walk error %v", e)
return
}
var list []string
for k := range podlist {
list = append(list, k)
}
loglist := list
if !admin {
loglist = Filter(list, gitlist)
}
sort.Slice(loglist, func(i, j int) bool {
return loglist[i] < loglist[j]
})
n := len(loglist)
inputlist := make([]string, n+1)
for i, v := range loglist {
// fmt.Println("convert gitlist ", i, v)
inputlist[i+1] = strconv.Itoa(i+1) + " " + v
}
text := "0 quit"
inputlist[0] = text
// spew.Dump("inputlist", inputlist)
completer := func(d prompt.Document) []prompt.Suggest {
s := []prompt.Suggest{}
for i, v := range inputlist {
if i == 0 {
continue
}
s = append(s, prompt.Suggest{
Text: v,
})
}
s = append(s, prompt.Suggest{
Text: inputlist[0],
})
// return prompt.FilterFuzzy(s, d.GetWordBeforeCursor(), true)
return prompt.FilterContains(s, d.GetWordBeforeCursor(), true)
}
var p string
for {
fmt.Printf("\rPlease select the project ( keyword search is ok ): \n")
for i, v := range inputlist {
if i == 0 {
continue
}
fmt.Println(" ", v)
}
fmt.Println(" ", inputlist[0])
t := prompt.Input("> ", completer)
if t == "" {
fmt.Println("input is invalid")
continue
}
// spew.Dump("t", t)
x := searchInput(t, inputlist)
if x == -1 {
fmt.Println("input is invalid, not found any")
continue
}
if x == 0 {
err = fmt.Errorf("exited")
fmt.Println("exiting...")
return
}
// // index, _ := strconv.Atoi(t)
// index := x
// if index <= 0 || index > len(loglist) {
// fmt.Println("input is invalid, input number only")
// continue
// }
p = loglist[x-1]
break
}
fmt.Println("You selected " + p)
pod = podlist[p]
return
}
func searchInput(t string, inputlist []string) int {
s := fmt.Sprintf(".*%v.*", strings.ReplaceAll(t, " ", ".*"))
for i, v := range inputlist {
// fmt.Println("start ", i, v, t)
// if strings.Contains(v, t) {
if matched, _ := regexp.MatchString(s, v); matched {
return i
}
}
return -1
}