-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(configuration): add initial support for Viper command initializa…
…tion (#607) * feat(config): initial common package for cmds with viper * feat(viper): begin integrating viper into commands * feat(viper): print config used * feat(template): initial templating of a validation * feat(template): testing functions in templating * fix(e2e): update function use for proper arguments * chore(docs): update docs and cleanup * fix(dev): cleanup proto templating logic * fix(pkg): cleanup the templating proto work * fix(cli): update hooks for nested commands * fix(docs): Update src/cmd/common/viper.go Co-authored-by: Megan Wolf <97549300+meganwolf0@users.noreply.github.com> * fix(dev): update alias for dev to d * chore(deps): run go mod tidy to resolve updates to deps --------- Co-authored-by: Megan Wolf <97549300+meganwolf0@users.noreply.github.com>
- Loading branch information
1 parent
aa4e122
commit 2c94c83
Showing
21 changed files
with
501 additions
and
297 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Config File | ||
|
||
Lula allows the use and specification of a config file in the following ways: | ||
- Checking current working directory for a `lula-config.yaml` file | ||
- Specification with environment variable `LULA_CONFIG=<path>` | ||
|
||
## Identification | ||
|
||
If identified, Lula will log which configuration file is used to stdout: | ||
```bash | ||
Using config file /home/dev/work/lula/lula-config.yaml | ||
``` | ||
## Support | ||
|
||
Modification of `log level` can be set in the configuration file by specifying: | ||
|
||
lula-config.yaml | ||
```yaml | ||
log_level: debug | ||
``` |
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 @@ | ||
log_level: debug |
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,42 @@ | ||
package common | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/defenseunicorns/lula/src/config" | ||
"github.com/defenseunicorns/lula/src/pkg/message" | ||
) | ||
|
||
func SetupClI(logLevel string) error { | ||
|
||
match := map[string]message.LogLevel{ | ||
"warn": message.WarnLevel, | ||
"info": message.InfoLevel, | ||
"debug": message.DebugLevel, | ||
"trace": message.TraceLevel, | ||
} | ||
|
||
// No log level set, so use the default | ||
if logLevel != "" { | ||
if lvl, ok := match[logLevel]; ok { | ||
message.SetLogLevel(lvl) | ||
message.Info("Log level set to " + logLevel) | ||
} else { | ||
message.Warn("Invalid log level. Valid options are: warn, info, debug, trace.") | ||
} | ||
} | ||
|
||
// Disable progress bars for CI envs | ||
if os.Getenv("CI") == "true" { | ||
message.Debug("CI environment detected, disabling progress bars") | ||
message.NoProgress = true | ||
} | ||
|
||
if !config.SkipLogFile { | ||
message.UseLogFile() | ||
} | ||
|
||
printViperConfigUsed() | ||
|
||
return nil | ||
} |
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,95 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"strings" | ||
|
||
"github.com/defenseunicorns/lula/src/pkg/message" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
VLogLevel = "log_level" | ||
) | ||
|
||
var ( | ||
// Viper instance used by commands | ||
v *viper.Viper | ||
|
||
// Viper configuration error | ||
vConfigError error | ||
) | ||
|
||
// InitViper initializes the viper singleton for the CLI | ||
func InitViper() *viper.Viper { | ||
// Already initialized by some other command | ||
if v != nil { | ||
return v | ||
} | ||
|
||
v = viper.New() | ||
|
||
// Skip for the version command | ||
if isVersionCmd() { | ||
return v | ||
} | ||
|
||
// Specify an alternate config file | ||
cfgFile := os.Getenv("LULA_CONFIG") | ||
|
||
// Don't forget to read config either from cfgFile or from home directory! | ||
if cfgFile != "" { | ||
// Use config file from the flag. | ||
v.SetConfigFile(cfgFile) | ||
} else { | ||
// Search config paths in the current directory and $HOME/.lula. | ||
v.AddConfigPath(".") | ||
v.AddConfigPath("$HOME/.lula") | ||
v.SetConfigName("lula-config") | ||
} | ||
|
||
// E.g. LULA_LOG_LEVEL=debug | ||
v.SetEnvPrefix("lula") | ||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) | ||
v.AutomaticEnv() | ||
|
||
// Optional, so ignore errors | ||
vConfigError = v.ReadInConfig() | ||
|
||
// Set default values for viper | ||
setDefaults() | ||
|
||
return v | ||
} | ||
|
||
// GetViper returns the viper singleton | ||
func GetViper() *viper.Viper { | ||
return v | ||
} | ||
|
||
func isVersionCmd() bool { | ||
args := os.Args | ||
return len(args) > 1 && (args[1] == "version" || args[1] == "v") | ||
} | ||
|
||
func setDefaults() { | ||
v.SetDefault(VLogLevel, "info") | ||
} | ||
|
||
func printViperConfigUsed() { | ||
// Only print config info if viper is initialized. | ||
vInitialized := v != nil | ||
if !vInitialized { | ||
return | ||
} | ||
var notFoundErr viper.ConfigFileNotFoundError | ||
if errors.As(vConfigError, ¬FoundErr) { | ||
return | ||
} | ||
if vConfigError != nil { | ||
message.WarnErrf(vConfigError, "failed to load config file: %s", vConfigError.Error()) | ||
return | ||
} | ||
message.Notef("Using config file %s", v.ConfigFileUsed()) | ||
} |
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
Oops, something went wrong.