From 4f1e829c6289885c864ce85d0917ab4e4e2c333a Mon Sep 17 00:00:00 2001 From: Gauthier Delacroix Date: Thu, 24 Oct 2024 14:15:08 +0200 Subject: [PATCH] Add quiet mode (cache only) --- cmd/aws-iam-authenticator/init.go | 3 +++ cmd/aws-iam-authenticator/root.go | 3 +++ pkg/token/filecache.go | 9 +++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cmd/aws-iam-authenticator/init.go b/cmd/aws-iam-authenticator/init.go index 7c4b5dc02..cd79d67b5 100644 --- a/cmd/aws-iam-authenticator/init.go +++ b/cmd/aws-iam-authenticator/init.go @@ -73,6 +73,9 @@ var initCmd = &cobra.Command{ } func init() { + viper.AutomaticEnv() + viper.SetEnvPrefix("aws_iam_authenticator") + initCmd.Flags().String( "hostname", "localhost", diff --git a/cmd/aws-iam-authenticator/root.go b/cmd/aws-iam-authenticator/root.go index 77b33f509..3ed6be9bc 100644 --- a/cmd/aws-iam-authenticator/root.go +++ b/cmd/aws-iam-authenticator/root.go @@ -57,6 +57,9 @@ func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Load configuration from `filename`") + rootCmd.PersistentFlags().BoolP("quiet", "q", false, "Reduce output verbosity") + viper.BindPFlag("quiet", rootCmd.PersistentFlags().Lookup("quiet")) + rootCmd.PersistentFlags().StringP("log-format", "l", "text", "Specify log format to use when logging to stderr [text or json]") rootCmd.PersistentFlags().StringP( diff --git a/pkg/token/filecache.go b/pkg/token/filecache.go index f1e893c2f..ae34f938d 100644 --- a/pkg/token/filecache.go +++ b/pkg/token/filecache.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/gofrs/flock" + "github.com/spf13/viper" "gopkg.in/yaml.v2" "io/fs" "io/ioutil" @@ -231,11 +232,15 @@ func NewFileCacheProvider(clusterID, profile, roleARN string, creds *credentials // otherwise fetching the credential from the underlying Provider and caching the results on disk // with an expiration time. func (f *FileCacheProvider) Retrieve() (credentials.Value, error) { + var quiet bool = viper.GetBool("quiet") + if !f.cachedCredential.IsExpired() { // use the cached credential return f.cachedCredential.Credential, nil } else { - _, _ = fmt.Fprintf(os.Stderr, "No cached credential available. Refreshing...\n") + if !quiet { + _, _ = fmt.Fprintf(os.Stderr, "No cached credential available. Refreshing...\n") + } // fetch the credentials from the underlying Provider credential, err := f.credentials.Get() if err != nil { @@ -269,7 +274,7 @@ func (f *FileCacheProvider) Retrieve() (credentials.Value, error) { // can't write cache, but still return the credential _, _ = fmt.Fprintf(os.Stderr, "Unable to update credential cache %s: %v\n", filename, err) err = nil - } else { + } else if !quiet { _, _ = fmt.Fprintf(os.Stderr, "Updated cached credential\n") } } else {