diff options
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/cli/main.go | 119 |
1 files changed, 52 insertions, 67 deletions
diff --git a/cmd/cli/main.go b/cmd/cli/main.go index ab0b408..f23f7ff 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -8,7 +8,7 @@ import ( "github.com/eduvpn/eduvpn-common/client" "github.com/eduvpn/eduvpn-common/internal/server" - "github.com/eduvpn/eduvpn-common/types" + "github.com/go-errors/errors" ) type ServerTypes int8 @@ -21,15 +21,14 @@ const ( // Open a browser with xdg-open. func openBrowser(url interface{}) { - urlString, ok := url.(string) - + str, ok := url.(string) if !ok { return } - fmt.Printf("OAuth: Initialized with AuthURL %s\n", urlString) + fmt.Printf("OAuth: Initialized with AuthURL %s\n", str) fmt.Println("OAuth: Opening browser with xdg-open...") - cmdErr := exec.Command("xdg-open", urlString).Start() - if cmdErr != nil { + if exec.Command("xdg-open", str).Start() != nil { + //TODO(): Shouldn't this if statement be inverted? fmt.Println("OAuth: Browser opened with xdg-open...") } } @@ -37,38 +36,32 @@ func openBrowser(url interface{}) { // Ask for a profile in the command line. func sendProfile(state *client.Client, data interface{}) { fmt.Printf("Multiple VPN profiles found. Please select a profile by entering e.g. 1") - serverProfiles, ok := data.(*server.ProfileInfo) - + sps, ok := data.(*server.ProfileInfo) if !ok { fmt.Println("Invalid data type") return } - var profiles string - - for index, profile := range serverProfiles.Info.ProfileList { - profiles += fmt.Sprintf("\n%d - %s", index+1, profile.DisplayName) + ps := "" + for i, p := range sps.Info.ProfileList { + ps += fmt.Sprintf("\n%d - %s", i+1, p.DisplayName) } // Show the profiles - fmt.Println(profiles) + fmt.Println(ps) - var chosenProfile int - _, scanErr := fmt.Scanf("%d", &chosenProfile) - - if scanErr != nil || chosenProfile <= 0 || - chosenProfile > len(serverProfiles.Info.ProfileList) { + var idx int + if _, err := fmt.Scanf("%d", &idx); err != nil || idx <= 0 || + idx > len(sps.Info.ProfileList) { fmt.Println("invalid profile chosen, please retry") sendProfile(state, data) return } - profile := serverProfiles.Info.ProfileList[chosenProfile-1] - fmt.Println("Sending profile ID", profile.ID) - profileErr := state.SetProfileID(profile.ID) - - if profileErr != nil { - fmt.Println("Failed setting profile with error", profileErr) + p := sps.Info.ProfileList[idx-1] + fmt.Println("Sending profile ID", p.ID) + if err := state.SetProfileID(p.ID); err != nil { + fmt.Println("Failed setting profile with error", err) } } @@ -76,12 +69,7 @@ func sendProfile(state *client.Client, data interface{}) { // If OAuth is started we open the browser with the Auth URL // If we ask for a profile, we send the profile using command line input // Note that this has an additional argument, the vpn state which was wrapped into this callback function below. -func stateCallback( - state *client.Client, - oldState client.FSMStateID, - newState client.FSMStateID, - data interface{}, -) { +func stateCallback(state *client.Client, oldState client.FSMStateID, newState client.FSMStateID, data interface{}) { if newState == client.StateOAuthStarted { openBrowser(data) } @@ -92,83 +80,80 @@ func stateCallback( } // Get a config for Institute Access or Secure Internet Server. -func getConfig(state *client.Client, url string, serverType ServerTypes) (string, string, error) { +func getConfig(state *client.Client, url string, srvType ServerTypes) (string, string, error) { if !strings.HasPrefix(url, "http") { url = "https://" + url } // Prefer TCP is set to False - if serverType == ServerTypeInstituteAccess { - _, addErr := state.AddInstituteServer(url) - if addErr != nil { - return "", "", addErr + if srvType == ServerTypeInstituteAccess { + _, err := state.AddInstituteServer(url) + if err != nil { + return "", "", err } return state.GetConfigInstituteAccess(url, false) - } else if serverType == ServerTypeCustom { - _, addErr := state.AddCustomServer(url) - if addErr != nil { - return "", "", addErr + } else if srvType == ServerTypeCustom { + _, err := state.AddCustomServer(url) + if err != nil { + return "", "", err } return state.GetConfigCustomServer(url, false) } - _, addErr := state.AddSecureInternetHomeServer(url) - if addErr != nil { - return "", "", addErr + _, err := state.AddSecureInternetHomeServer(url) + if err != nil { + return "", "", err } return state.GetConfigSecureInternet(url, false) } // Get a config for a single server, Institute Access or Secure Internet. -func printConfig(url string, serverType ServerTypes) { - state := &client.Client{} +func printConfig(url string, srvType ServerTypes) { + c := &client.Client{} - registerErr := state.Register( + err := c.Register( "org.eduvpn.app.linux", "configs", "en", func(old client.FSMStateID, new client.FSMStateID, data interface{}) bool { - stateCallback(state, old, new, data) + stateCallback(c, old, new, data) return true }, true, ) - if registerErr != nil { - fmt.Printf("Register error: %v", registerErr) + if err != nil { + fmt.Printf("Register error: %v", err) return } - defer state.Deregister() - - config, _, configErr := getConfig(state, url, serverType) + defer c.Deregister() - if configErr != nil { + cfg, _, err := getConfig(c, url, srvType) + if err != nil { + err1 := err.(*errors.Error) // Show the usage of tracebacks and causes - fmt.Println("Error getting config:", types.ErrorTraceback(configErr)) - fmt.Println("Error getting config, cause:", types.ErrorCause(configErr)) + fmt.Printf("Error getting config: %s\nCause:\n%s\nStack trace:\n%s\n\n'", + err1.Error(), err1.Err, err1.ErrorStack()) return } - fmt.Println("Obtained config", config) + fmt.Println("Obtained config:", cfg) } // The main function // It parses the arguments and executes the correct functions. func main() { - customURLArg := flag.String("get-custom", "", "The url of a custom server to connect to") - urlArg := flag.String("get-institute", "", "The url of an institute to connect to") - secureInternet := flag.String("get-secure", "", "Gets secure internet servers") + cu := flag.String("get-custom", "", "The url of a custom server to connect to") + u := flag.String("get-institute", "", "The url of an institute to connect to") + sec := flag.String("get-secure", "", "Gets secure internet servers") flag.Parse() // Connect to a VPN by getting an Institute Access config - customURLString := *customURLArg - urlString := *urlArg - secureInternetString := *secureInternet switch { - case customURLString != "": - printConfig(customURLString, ServerTypeCustom) - case urlString != "": - printConfig(urlString, ServerTypeInstituteAccess) - case secureInternetString != "": - printConfig(secureInternetString, ServerTypeSecureInternet) + case *cu != "": + printConfig(*cu, ServerTypeCustom) + case *u != "": + printConfig(*u, ServerTypeInstituteAccess) + case *sec != "": + printConfig(*sec, ServerTypeSecureInternet) default: flag.PrintDefaults() } |
