/
main.go
207 lines (168 loc) · 4.39 KB
/
main.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
package prompt
import(
"github.com/chzyer/readline"
"fmt"
"strconv"
. "github.com/ml27299/lit-cli/helpers"
"strings"
"github.com/ml27299/lit-cli/helpers/paths"
//"gopkg.in/src-d/go-git.v4"
)
func PromptForUpdate() (string, error) {
var response string
Info("Would you like to update the cli to the latest version?")
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
for {
response, err = rl.Readline()
if err != nil {
break
}
str := strings.TrimSpace(strings.ToLower(response))
if err != nil {
fmt.Println(err.Error())
} else if str == "y" || str == "n" || str == "yes" || str == "no" {
break
}else {
fmt.Println("Please enter Yy/Nn or yes/no")
}
}
return response, nil
}
func PromptForInteractive(args []string, submodule *Module) (string, error) {
var response string
status, err := submodule.Status()
if err != nil {
return response, err
}
Info("Interactive mode for "+*&status.Path)
Info("Below is the command that will be supplied to "+*&status.Path+", edit if not correct")
rl, err := readline.NewEx(&readline.Config{
Prompt: "> ",
ForceUseInteractive: true,
})
rl.WriteStdin([]byte(strings.Join(args, " ")))
if err != nil {
panic(err)
}
defer rl.Close()
for {
response, err = rl.Readline()
if err != nil {
break
}
break
}
return response, nil
}
func PromptForMultiSource(sources []string, newfilename string, newfilepath string) (int, error) {
fmt.Println("There are multiple sources mapped to "+newfilepath)
fmt.Println("Choose which source to add "+newfilename)
var (
response string
)
for index, source := range sources {
fmt.Println("["+strconv.Itoa(index)+"] "+source)
}
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
for {
response, err = rl.Readline()
if err != nil {
break
}
index, err := strconv.Atoi(response)
if err != nil {
fmt.Println("Please enter a number for the corresponding sources")
}else if index > len(sources) - 1 {
fmt.Println("Please enter a number within range of the number of sources")
} else {
break
}
}
responseInt, err := strconv.Atoi(response)
if err != nil {
return responseInt, err
}
return responseInt, nil
}
func PromtForDest(dests []string, newfilename string, newfilepath string, submodule_path string) (int, error) {
fmt.Println("There are multiple destinations pointing to "+newfilepath)
fmt.Println("Choose which source to add "+newfilename)
var (
response string
)
for index, dest := range dests {
dest, err := paths.Normalize(dest)
if err != nil {
return 0, err
}
newpath := strings.Replace(newfilepath, dest, "", -1)
newpath = submodule_path+newpath
fmt.Println("["+strconv.Itoa(index)+"] "+newpath)
}
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
for {
response, err = rl.Readline()
if err != nil {
break
}
index, err := strconv.Atoi(response)
if err != nil {
fmt.Println("Please enter a number for the corresponding destination")
}else if index > len(dests) - 1 {
fmt.Println("Please enter a number within range of the number of destinations")
} else {
break
}
}
responseInt, err := strconv.Atoi(response)
if err != nil {
return responseInt, err
}
return responseInt, nil
}
func PromptForNoDest(newfilename string, newfilepath string) (bool, error) {
fmt.Println("Couldnt find a corresponding destination for "+newfilepath)
fmt.Println("This means that you're trying to create a file in a linked directory, but there werent any matching sources to map to")
fmt.Println("You can either add the file explicitly or implicitly (using /*) in lit.link.json to fix this")
fmt.Println("If you wish to continue, "+newfilepath+"/"+newfilename+" will be created but will not link back to any submodule")
fmt.Println("Do you wish to continue?")
var (
response string
)
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
for {
response, err = rl.Readline()
if err != nil {
break
}
response = strings.ToLower(response)
if response != "yes" && response != "y" && response != "no" && response != "n" {
fmt.Println("Please enter either Y/y/Yes/yes or N/n/No/no")
}else {
break
}
}
responseBool := false
if response == "yes" || response == "y"{
responseBool = true
}else if response == "no" || response == "n" {
responseBool = false
}
return responseBool, nil
}