Skip to content

Commit ed147cc

Browse files
committed
Add Review suggestions
1 parent 5831182 commit ed147cc

1 file changed

Lines changed: 94 additions & 89 deletions

File tree

cmd/root.go

Lines changed: 94 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/loophole/cli/config"
1919
"github.com/loophole/cli/internal/pkg/cache"
2020
"github.com/loophole/cli/internal/pkg/closehandler"
21-
"github.com/loophole/cli/internal/pkg/token"
21+
"github.com/loophole/cli/internal/pkg/communication"
2222
"github.com/mattn/go-colorable"
2323
"github.com/rs/zerolog"
2424
"github.com/rs/zerolog/log"
@@ -30,6 +30,10 @@ var signalChan chan os.Signal
3030

3131
var alreadyRunning bool
3232

33+
const http = "Expose an HTTP Port"
34+
const path = "Expose a local path"
35+
const webDAV = "Expose a local path with WebDAV"
36+
3337
var rootCmd = &cobra.Command{
3438
Use: "loophole",
3539
Short: "Loophole - End to end TLS encrypted TCP communication between you and your clients",
@@ -42,36 +46,14 @@ var rootCmd = &cobra.Command{
4246
},
4347
}
4448

45-
func interactivePrompt() {
46-
cmd := httpCmd.Root() //find a better way to access rootCMD
47-
48-
if !token.IsTokenSaved() {
49-
cmd.SetArgs([]string{"account", "login"})
50-
cmd.Execute()
51-
}
52-
53-
argPath := cache.GetLocalStorageFile("lastArgs", "logs")
54-
var lastArgs string = ""
55-
if _, err := os.Stat(argPath); err == nil {
56-
argBytes, _ := ioutil.ReadFile(argPath)
57-
lastArgs = string(argBytes)
58-
fmt.Println(string(lastArgs))
59-
}
60-
argsq := &survey.Select{
61-
Message: "Your last settings were: " + lastArgs + " , would you like to reuse them?",
62-
Options: []string{"Yes", "No"},
63-
}
64-
initq := &survey.Select{
65-
Message: "Welcome to loophole. What do you want to do?",
66-
Options: []string{"Expose an HTTP Port", "Expose a local path", "Expose a local path with WebDAV", "Logout"},
67-
}
68-
var portPrompt = []*survey.Question{
49+
func getPortPrompt() []*survey.Question {
50+
return []*survey.Question{
6951
{
7052
Name: "port",
7153
Prompt: &survey.Input{Message: "Please enter the http port you want to expose: "},
7254
Validate: func(val interface{}) error {
7355
if port, ok := val.(string); !ok {
74-
return errors.New("enter a valid string")
56+
return errors.New("port must be between 0-65535")
7557
} else { //else is necessary here to keep access to port
7658
n, err := strconv.Atoi(port)
7759
if err != nil {
@@ -86,7 +68,10 @@ func interactivePrompt() {
8668
},
8769
},
8870
}
89-
var pathPrompt = []*survey.Question{
71+
}
72+
73+
func getPathPrompt() []*survey.Question {
74+
return []*survey.Question{
9075
{
9176
Name: "path",
9277
Prompt: &survey.Input{Message: "Please enter the path you want to expose: "},
@@ -106,71 +91,20 @@ func interactivePrompt() {
10691
}),
10792
},
10893
}
109-
logoutPrompt := &survey.Select{
110-
Message: "Are you sure you want to logout?",
111-
Options: []string{"No", "Yes, I'm sure"},
112-
}
113-
var res string
114-
var exposePort int
115-
var exposePath string
116-
var arguments []string
94+
}
11795

118-
if lastArgs != "" {
119-
err := survey.AskOne(argsq, &res)
120-
if err != nil {
121-
signalChan <- nil
122-
}
123-
if res == "Yes" {
124-
cmd.SetArgs(strings.Split(lastArgs, " ")) //needs validation
125-
cmd.Execute()
126-
os.Exit(0)
127-
}
128-
}
129-
err := survey.AskOne(initq, &res)
130-
if err != nil {
131-
signalChan <- nil
132-
}
133-
if res == "Expose an HTTP Port" {
134-
err = survey.Ask(portPrompt, &exposePort)
135-
if err != nil {
136-
signalChan <- nil
137-
}
138-
arguments = []string{"http", strconv.Itoa(exposePort)}
139-
} else if res == "Expose a local path" {
140-
err = survey.Ask(pathPrompt, &exposePath)
141-
if err != nil {
142-
signalChan <- nil
143-
}
144-
arguments = []string{"path", exposePath}
145-
} else if res == "Expose a local path with WebDAV" {
146-
err = survey.Ask(pathPrompt, &exposePath)
147-
if err != nil {
148-
signalChan <- nil
149-
}
150-
arguments = []string{"webdav", exposePath}
151-
} else if res == "Logout" {
152-
err := survey.AskOne(logoutPrompt, &res)
153-
if err != nil {
154-
signalChan <- nil
155-
}
156-
if res == "Yes, I'm sure" {
157-
cmd.SetArgs([]string{"account", "logout"})
158-
cmd.Execute()
159-
}
160-
os.Exit(0) //if Execute() should fail, don't ask for hostname etc. but instead exit
96+
func getLastArgsPrompt(lastArgs string) *survey.Select {
97+
return &survey.Select{
98+
Message: "Your last settings were: '" + lastArgs + "', would you like to reuse them?",
99+
Options: []string{"Yes", "No"},
161100
}
101+
}
162102

163-
hostname := askHostname()
164-
if hostname != "" {
165-
arguments = append(arguments, "--hostname", hostname)
166-
}
167-
basicAuth := askBasicAuth()
168-
if basicAuth != "" {
169-
arguments = append(arguments, "-u", basicAuth)
103+
func getInitialPrompt() *survey.Select {
104+
return &survey.Select{
105+
Message: "Welcome to loophole. What do you want to do?",
106+
Options: []string{http, path, webDAV},
170107
}
171-
closehandler.SaveArguments(arguments)
172-
cmd.SetArgs(arguments)
173-
cmd.Execute()
174108
}
175109

176110
func askBasicAuth() string {
@@ -199,7 +133,6 @@ func askBasicAuth() string {
199133
return ""
200134
}
201135
return res
202-
203136
}
204137

205138
func askHostname() string {
@@ -238,6 +171,78 @@ func askHostname() string {
238171
return res
239172
}
240173

174+
func interactivePrompt() {
175+
argPath := cache.GetLocalStorageFile("lastArgs", "logs")
176+
var lastArgs string = ""
177+
if _, err := os.Stat(argPath); err == nil {
178+
argBytes, err := ioutil.ReadFile(argPath)
179+
if err != nil {
180+
communication.Fatal("Error reading last used arguments:" + err.Error())
181+
}
182+
lastArgs = string(argBytes)
183+
}
184+
var lastArgsPrompt = getLastArgsPrompt(lastArgs)
185+
var initialPrompt = getInitialPrompt()
186+
var portPrompt = getPortPrompt()
187+
var pathPrompt = getPathPrompt()
188+
189+
var res string
190+
var exposePort int
191+
var exposePath string
192+
var arguments []string
193+
194+
cmd := httpCmd.Root() //TODO: find a better way to access rootCMD
195+
196+
if lastArgs != "" {
197+
err := survey.AskOne(lastArgsPrompt, &res)
198+
if err != nil {
199+
signalChan <- nil
200+
}
201+
if res == "Yes" {
202+
cmd.SetArgs(strings.Split(lastArgs, " ")) //needs validation
203+
cmd.Execute()
204+
os.Exit(1)
205+
}
206+
}
207+
err := survey.AskOne(initialPrompt, &res)
208+
209+
if err != nil {
210+
signalChan <- nil
211+
}
212+
switch res {
213+
case http:
214+
err = survey.Ask(portPrompt, &exposePort)
215+
if err != nil {
216+
signalChan <- nil
217+
}
218+
arguments = []string{"http", strconv.Itoa(exposePort)}
219+
case path:
220+
err = survey.Ask(pathPrompt, &exposePath)
221+
if err != nil {
222+
signalChan <- nil
223+
}
224+
arguments = []string{"path", exposePath}
225+
case webDAV:
226+
err = survey.Ask(pathPrompt, &exposePath)
227+
if err != nil {
228+
signalChan <- nil
229+
}
230+
arguments = []string{"webdav", exposePath}
231+
}
232+
233+
hostname := askHostname()
234+
if hostname != "" {
235+
arguments = append(arguments, "--hostname", hostname)
236+
}
237+
basicAuth := askBasicAuth()
238+
if basicAuth != "" {
239+
arguments = append(arguments, "-u", basicAuth)
240+
}
241+
closehandler.SaveArguments(arguments)
242+
cmd.SetArgs(arguments)
243+
cmd.Execute()
244+
}
245+
241246
func init() {
242247
cobra.OnInitialize(initLogger)
243248

0 commit comments

Comments
 (0)