Skip to content

Commit

Permalink
[check-credentials] add check-credentials command (#12)
Browse files Browse the repository at this point in the history
Hey, I don't really know Go or how to test this locally, but I think
this should do what i'm looking for. Just trying to add a command that I
can use to understand if the credentials cache is valid or not. Maybe
there's a better way to do this already?

---------

Co-authored-by: spalger <[email protected]>
Co-authored-by: Harry Bowron <[email protected]>
  • Loading branch information
3 people authored Dec 7, 2023
1 parent 7cd90ad commit e39126c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ alias creds="eval \$(quikstrate credentials)"`,
func init() {
credentialsCmd.Flags().StringP("format", "f", "export", "substrate environment")
credentialsCmd.Flags().Bool("force", false, "always fetch new credentials")
credentialsCmd.Flags().Bool("check", false, "check if credentials are expired, exit 0 if up-to-date otherwise exit 1`")
credentialsCmd.MarkFlagsMutuallyExclusive("force", "check")
rootCmd.AddCommand(credentialsCmd)
}
14 changes: 14 additions & 0 deletions internal/creds/cmd_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package creds

import (
"log"
"os"

"github.com/spf13/cobra"
)

func CredentialsCmd(cmd *cobra.Command, args []string) {
format := cmd.Flag("format").Value.String()
force := cmd.Flag("force").Value.String()
check := cmd.Flag("check").Value.String()

if check == "true" {
checkCredentials()
}

var creds Credentials
var err error
Expand All @@ -26,3 +32,11 @@ func CredentialsCmd(cmd *cobra.Command, args []string) {
func getDefaultCredentials() (Credentials, error) {
return refreshCredentials(RoleData{}, DefaultCredsFile)
}

func checkCredentials() {
creds, err := getCredsFromFile(DefaultCredsFile)
if err != nil || creds.needsRefresh() {
os.Exit(1)
}
os.Exit(0)
}

0 comments on commit e39126c

Please sign in to comment.