Skip to content

Commit

Permalink
Add token name support in token command
Browse files Browse the repository at this point in the history
  • Loading branch information
royroyee committed Mar 4, 2024
1 parent 9ddec43 commit 74696de
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ $ alpacon token create -n [TOKEN NAME] --expiration-in-days=7
$ alpacon token ls
# Delete API token
$ alpacon token delete [TOKEN ID]
$ alpacon token delete [TOKEN_ID_OR_NAME]
# Log in via API token
$ alpacon login -s [SERVER URL] -t [TOKEN KEY]
Expand Down
2 changes: 1 addition & 1 deletion api/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func GetAPITokenList(ac *client.AlpaconClient) ([]APITokenAttributes, error) {
return tokenList, nil
}

func getAPITokenIDByName(ac *client.AlpaconClient, tokenName string) (string, error) {
func GetAPITokenIDByName(ac *client.AlpaconClient, tokenName string) (string, error) {
params := map[string]string{
"name": tokenName,
}
Expand Down
17 changes: 12 additions & 5 deletions cmd/token/token_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,29 @@ var tokenDeleteCmd = &cobra.Command{
This command requires the token name to identify the token to be deleted.
`,
Example: `
alpacon token delete [TOKEN NAME]
alpacon token delete [TOKEN_ID_OR_NAME]
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
tokenID := args[0]
tokenId := args[0]

alpaconClient, err := client.NewAlpaconAPIClient()
if err != nil {
utils.CliError("Connection to Alpacon API failed: %s. Consider re-logging.", err)
}

err = auth.DeleteAPIToken(alpaconClient, tokenID)
if !utils.IsUUID(tokenId) {
tokenId, err = auth.GetAPITokenIDByName(alpaconClient, tokenId)
if err != nil {
utils.CliError("Failed to delete the api token %s.", err)
}
}

err = auth.DeleteAPIToken(alpaconClient, tokenId)
if err != nil {
utils.CliError("Failed to delete the api token %s. ", err)
utils.CliError("Failed to delete the api token %s.", err)
}

utils.CliInfo("API Token successfully deleted: %s", tokenID)
utils.CliInfo("API Token successfully deleted: %s", tokenId)
},
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/google/go-github v17.0.0+incompatible
github.com/google/uuid v1.6.0
github.com/gookit/color v1.5.4
github.com/gorilla/websocket v1.5.1
github.com/olekukonko/tablewriter v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4r
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
Expand Down
6 changes: 6 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bufio"
"fmt"
"github.com/google/uuid"
"golang.org/x/term"
"net/url"
"os"
Expand Down Expand Up @@ -247,3 +248,8 @@ func StringToStringPointer(value string) *string {
return &value
}
}

func IsUUID(str string) bool {
_, err := uuid.Parse(str)
return err == nil
}

0 comments on commit 74696de

Please sign in to comment.