Skip to content

Commit

Permalink
--proxy cmd proxy url handled
Browse files Browse the repository at this point in the history
* proxy related tasks moved to flag.go --> proxyCmd()
* default port number added to proxy url
* url variable renamed to URL because of name conflict with url pkg
  • Loading branch information
gozeloglu committed Sep 20, 2021
1 parent e40feb9 commit 7be7d0c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
24 changes: 24 additions & 0 deletions cmd/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"fmt"
"net/url"
)

const defaultPort = "1080"

// proxyCmd parses proxy url and adds default port number if not exists.
func proxyCmd(proxy string) (string, error) {
u, err := url.Parse(proxy)
if err != nil {
err = fmt.Errorf("not valid proxy address: %s", err.Error())
return "", err
}
if u.Port() == "" {
if proxy[len(proxy)-1] != ':' {
proxy += ":"
}
proxy += defaultPort
}
return proxy, nil
}
19 changes: 14 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
url = ""
URL = ""
proxy = ""
c = src.NewClient()
)
Expand All @@ -18,9 +18,13 @@ var rootCmd = &cobra.Command{
Short: "gURL is open source CLI tool written in Go.",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
url = args[0]
fmt.Println("URL: ", url)
checkFlags()
URL = args[0]
fmt.Println("URL: ", URL)
err := checkFlags()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}

Expand All @@ -34,8 +38,13 @@ func Execute() {
}

// checkFlags checks the flags, get input, and sets the inputs to Client.
func checkFlags() {
func checkFlags() error {
if proxy != "" {
proxy, err := proxyCmd(proxy)
if err != nil {
return err
}
c.SetProxy(proxy)
}
return nil
}

0 comments on commit 7be7d0c

Please sign in to comment.