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 f234e61e4..b32576af3 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/filecache/filecache.go b/pkg/filecache/filecache.go index 64092b9f4..bbcc10361 100644 --- a/pkg/filecache/filecache.go +++ b/pkg/filecache/filecache.go @@ -14,6 +14,7 @@ import ( "github.com/aws/aws-sdk-go/aws/credentials" "github.com/gofrs/flock" "github.com/spf13/afero" + "github.com/spf13/viper" "gopkg.in/yaml.v2" ) @@ -211,11 +212,15 @@ func (f *FileCacheProvider) Retrieve() (credentials.Value, error) { // otherwise fetching the credential from the underlying Provider and caching the results on disk // with an expiration time. func (f *FileCacheProvider) RetrieveWithContext(ctx context.Context) (credentials.Value, error) { + var quiet bool = viper.GetBool("quiet") + if !f.cachedCredential.Expired() && f.cachedCredential.HasKeys() { // use the cached credential return V2CredentialToV1Value(f.cachedCredential), 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.provider.Retrieve(ctx) if err != nil { @@ -246,7 +251,7 @@ func (f *FileCacheProvider) RetrieveWithContext(ctx context.Context) (credential // can't write cache, but still return the credential _, _ = fmt.Fprintf(os.Stderr, "Unable to update credential cache %s: %v\n", f.filename, err) err = nil - } else { + } else if !quiet { _, _ = fmt.Fprintf(os.Stderr, "Updated cached credential\n") } } else {