Skip to content

Commit

Permalink
Add log-level option
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Sep 23, 2019
1 parent eff1efa commit 2ab03bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,27 @@ 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")
err error
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,
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions credentials/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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"]
Expand Down

0 comments on commit 2ab03bc

Please sign in to comment.