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 5f0f6e1 commit 4f1e829
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/token/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4f1e829

Please sign in to comment.