From 74696deef75fe5118414a60858adb8ac85992ab8 Mon Sep 17 00:00:00 2001 From: royroyee Date: Mon, 4 Mar 2024 20:05:30 +0900 Subject: [PATCH] Add token name support in token command --- README.md | 2 +- api/auth/auth.go | 2 +- cmd/token/token_delete.go | 17 ++++++++++++----- go.mod | 1 + go.sum | 2 ++ utils/utils.go | 6 ++++++ 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2a3cb3c..6084c20 100644 --- a/README.md +++ b/README.md @@ -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] diff --git a/api/auth/auth.go b/api/auth/auth.go index 4d5fcdf..0c3ba3f 100644 --- a/api/auth/auth.go +++ b/api/auth/auth.go @@ -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, } diff --git a/cmd/token/token_delete.go b/cmd/token/token_delete.go index 329cd8b..483a447 100644 --- a/cmd/token/token_delete.go +++ b/cmd/token/token_delete.go @@ -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) }, } diff --git a/go.mod b/go.mod index 13c2da1..298de13 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b412893..1bed324 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/utils/utils.go b/utils/utils.go index 5ffe4d0..45b3957 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "bufio" "fmt" + "github.com/google/uuid" "golang.org/x/term" "net/url" "os" @@ -247,3 +248,8 @@ func StringToStringPointer(value string) *string { return &value } } + +func IsUUID(str string) bool { + _, err := uuid.Parse(str) + return err == nil +}