Skip to content

Commit

Permalink
Add quiet mode (cache only)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdlx committed Oct 24, 2024
1 parent 29c47ac commit 3bc057a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/aws-iam-authenticator/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ var initCmd = &cobra.Command{
}

func init() {
viper.AutomaticEnv()
viper.SetEnvPrefix("aws_iam_authenticator")

initCmd.Flags().String(
"hostname",
"localhost",
Expand Down
3 changes: 3 additions & 0 deletions cmd/aws-iam-authenticator/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 7 additions & 2 deletions pkg/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3bc057a

Please sign in to comment.