Skip to content

Commit

Permalink
Merge pull request #6 from jacobbednarz/automatically-detect-auth-type
Browse files Browse the repository at this point in the history
add: detect which authentication type is used
  • Loading branch information
jacobbednarz committed Sep 17, 2020
2 parents f07f90f + b2bd432 commit 4a8bc0c
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"regexp"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -43,14 +44,15 @@ var addCmd = &cobra.Command{
emailAddress, _ := reader.ReadString('\n')
emailAddress = strings.TrimSpace(emailAddress)

fmt.Print("Authentication type (api_token or api_key): ")
authType, _ := reader.ReadString('\n')
authType = strings.TrimSpace(authType)

fmt.Print("Authentication value: ")
fmt.Print("Authentication value (API key or API token): ")
authValue, _ := reader.ReadString('\n')
authValue = strings.TrimSpace(authValue)

authType, err := determineAuthType(authValue)
if err != nil {
log.Fatalf("failed to detect authentication type: %s", err)
}

home, err := homedir.Dir()
if err != nil {
log.Fatal("unable to find home directory: ", err)
Expand Down Expand Up @@ -78,3 +80,13 @@ var addCmd = &cobra.Command{
})
},
}

func determineAuthType(s string) (string, error) {
if apiTokenMatch, _ := regexp.MatchString("[A-Za-z0-9-_]{40}", s); apiTokenMatch {
return "api_token", nil
} else if apiKeyMatch, _ := regexp.MatchString("[0-9a-f]{37}", s); apiKeyMatch {
return "api_key", nil
} else {
return "", errors.New("invalid API token or API key format")
}
}

0 comments on commit 4a8bc0c

Please sign in to comment.