-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add common functions found in hollow root commands
Signed-off-by: Andrew Holtzmann <aholtzmann@equinix.com>
- Loading branch information
Showing
10 changed files
with
102 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package rootcmd | ||
|
||
import ( | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// ViperBindFlag provides a wrapper around the viper bindings that handles error checks | ||
func ViperBindFlag(name string, flag *pflag.Flag) { | ||
err := viper.BindPFlag(name, flag) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package rootcmd | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/mitchellh/go-homedir" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// InitConfig reads in config file and ENV variables if set. | ||
func InitConfig(app, cfgFile string) error { | ||
if cfgFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
// Find home directory. | ||
home, err := homedir.Dir() | ||
cobra.CheckErr(err) | ||
|
||
// Search config in home directory with name ".hollow" (without extension). | ||
viper.AddConfigPath(home) | ||
viper.SetConfigName("." + app) | ||
} | ||
|
||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
viper.SetEnvPrefix(app) | ||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
err := viper.ReadInConfig() | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package rootcmd provide common functions to hollow binaries and cli set up | ||
package rootcmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package rootcmd | ||
|
||
import ( | ||
"go.hollow.sh/toolbox/version" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
// SetupLogging is a common configuraion of a zap.SugaredLogger, set to the logger passed | ||
func SetupLogging(app string, pretty, debug bool, logger *zap.SugaredLogger) { | ||
defer logger.Sync() //nolint:errcheck | ||
|
||
cfg := zap.NewProductionConfig() | ||
if pretty { | ||
cfg = zap.NewDevelopmentConfig() | ||
} | ||
|
||
if debug { | ||
cfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel) | ||
} else { | ||
cfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel) | ||
} | ||
|
||
l, err := cfg.Build() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
logger = l.Sugar().With("app", app, "version", version.Version()) //nolint:staticcheck | ||
} |