Skip to content

Commit

Permalink
cleanup: extract to function (#28)
Browse files Browse the repository at this point in the history
* isURL
* isJwtToken
  • Loading branch information
xx4h authored Oct 12, 2024
1 parent e389413 commit 74bc0c7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.22.7

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f
github.com/gosuri/uitable v0.0.4
github.com/lithammer/fuzzysearch v1.1.8
github.com/pterm/pterm v0.12.79
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f h1:Wl78ApPPB2Wvf/TIe2xdyJxTlb6obmF18d8QdkxNDu4=
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f/go.mod h1:OSYXu++VVOHnXeitef/D8n/6y4QV8uLHSFXX4NeXMGc=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand Down
21 changes: 16 additions & 5 deletions pkg/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ func getHubType() string {
return hubType
}

func isURL(url string) bool {
up, err := u.Parse(url)
if err != nil || up.Scheme == "" || up.Host == "" {
return false
}
return true
}

func getURL() string {
var url string
fmt.Print("Enter API URL of your hub (e.g. https://home-assistant.example.com/api): ")
Expand All @@ -88,14 +96,19 @@ func getURL() string {
fmt.Printf("Error: %v\n", err)
return getURL()
}
up, err := u.Parse(url)
if err != nil || up.Scheme == "" || up.Host == "" {
if ok := isURL(url); !ok {
fmt.Printf("Not a valid URL: %s\n", url)
return getURL()
}
return url
}

func isJwtToken(token []byte) bool {
t := string(token)
_, _, err := new(jwt.Parser).ParseUnverified(t, jwt.MapClaims{})
return err == nil
}

func getToken() string {
var token string
fmt.Print("Enter your hub token: ")
Expand All @@ -104,9 +117,7 @@ func getToken() string {
fmt.Printf("Error: %v\n", err)
return getToken()
}
token = string(byteToken)
_, _, err = new(jwt.Parser).ParseUnverified(token, jwt.MapClaims{})
if err != nil {
if ok := isJwtToken(byteToken); !ok {
fmt.Printf("\nNot a valid Token (JWT): %v\n", err)
return getToken()
}
Expand Down

0 comments on commit 74bc0c7

Please sign in to comment.