diff --git a/README.md b/README.md index 2e1c42f..56ae8d3 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,14 @@ What's the point? credentials-sync sync -c config.yml ``` +## Logging + +The log level can be set with either: + - The `--log-level` option + - The `SYNC_LOG_LEVEL` env variable + +Valid levels are `debug`, `info`, `warning` and `error` + ![example](https://raw.githubusercontent.com/coveooss/credentials-sync/master/example.png) ## Configuration file diff --git a/cli/root.go b/cli/root.go index 5d19c39..7fd93ca 100644 --- a/cli/root.go +++ b/cli/root.go @@ -33,9 +33,6 @@ var rootCmd = &cobra.Command{ targets that do not support external credentials. Support Jenkins only for now.`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - configuration = sync.NewConfiguration() - sourcesConfiguration := &credentials.SourcesConfiguration{} - targetsConfiguration := &targets.Configuration{} var ( configurationDict = map[string]interface{}{} configurationFile = viper.GetString("config") @@ -43,10 +40,20 @@ var rootCmd = &cobra.Command{ fileContent []byte ) + level, err := log.ParseLevel(viper.GetString("log-level")) + if err != nil { + return fmt.Errorf("Invalid log level: %s", err) + } + log.SetLevel(level) + if configurationFile == "" { return fmt.Errorf("A configuration file must be defined") } + configuration = sync.NewConfiguration() + sourcesConfiguration := &credentials.SourcesConfiguration{} + targetsConfiguration := &targets.Configuration{} + if strings.HasPrefix(configurationFile, "s3://") { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, @@ -104,11 +111,12 @@ func init() { viper.AutomaticEnv() viper.SetEnvPrefix("sync") + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) rootCmd.PersistentFlags().StringP("config", "c", "", "configuration file") viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")) - rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output") - viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose")) + rootCmd.PersistentFlags().StringP("log-level", "l", log.InfoLevel.String(), `"debug", "info", "warning" or "error"`) + viper.BindPFlag("log-level", rootCmd.PersistentFlags().Lookup("log-level")) initListCredentials() rootCmd.AddCommand(listTargetsCmd, syncCmd, validateCmd) diff --git a/credentials/sources.go b/credentials/sources.go index 20a09f2..a256519 100644 --- a/credentials/sources.go +++ b/credentials/sources.go @@ -106,14 +106,14 @@ func getCredentialsFromBytes(byteArray []byte) ([]Credentials, error) { log.Debug(err) } - return nil, fmt.Errorf("Failed to parse %v. See debug for more info", byteArray) + return nil, fmt.Errorf("Failed to parse %v. See debug for more info", string(byteArray)) } // Accept list of credentials func tryReadingList(bytes []byte) ([]map[string]interface{}, error) { var credentialsList []map[string]interface{} if err := yaml.Unmarshal(bytes, &credentialsList); err != nil { - return nil, fmt.Errorf("Error reading %v as credentials list: %v", bytes, err) + return nil, fmt.Errorf("Error reading as credentials list: %v", err) } return credentialsList, nil @@ -125,7 +125,7 @@ func tryReadingMapOfCredentials(bytes []byte) ([]map[string]interface{}, error) var credentialsMap map[string]map[string]interface{} if err := yaml.Unmarshal(bytes, &credentialsMap); err != nil { - return nil, fmt.Errorf("Error reading %v as credentials map: %v", bytes, err) + return nil, fmt.Errorf("Error reading as credentials map: %v", err) } for id, value := range credentialsMap { @@ -140,7 +140,7 @@ func tryReadingMapOfCredentials(bytes []byte) ([]map[string]interface{}, error) func tryReadingSingleCredential(bytes []byte) ([]map[string]interface{}, error) { var singleCredentials map[string]interface{} if err := yaml.Unmarshal(bytes, &singleCredentials); err != nil { - return nil, fmt.Errorf("Error reading %v as a map: %v", bytes, err) + return nil, fmt.Errorf("Error reading as a map: %v", err) } id, gotID := singleCredentials["id"]