refactor(cli): Use Viper to manage Keptn config#5694
Conversation
29cd494 to
15966bf
Compare
d891e1a to
4c17d6d
Compare
Codecov Report
@@ Coverage Diff @@
## master #5694 +/- ##
==========================================
- Coverage 54.33% 53.23% -1.11%
==========================================
Files 399 326 -73
Lines 23436 22351 -1085
Branches 1204 1054 -150
==========================================
- Hits 12735 11899 -836
+ Misses 9788 9553 -235
+ Partials 913 899 -14
|
ac615c9 to
1fe2872
Compare
cli/cmd/root.go
Outdated
There was a problem hiding this comment.
cli/cmd/set_config.go
Outdated
There was a problem hiding this comment.
Function signature NewCLIConfigManager is changed to support custom config file path. I have done a global replace NewCLIConfigManager() -> NewCLIConfigManager("")
cli/go.mod
Outdated
There was a problem hiding this comment.
Latest version of Viper at the time of this PR

https://pkg.go.dev/github.com/spf13/viper?tab=versions
cli/go.sum
Outdated
There was a problem hiding this comment.
All the changes in this file are because of two reasons:
- I added viper as a dependency (CI was failing) with
# In the keptn root directory
$ go get cli/pkg/config- I updated the dependency from viper 1.8.1 -> 1.9.0 in the
go.modbecause 1.9.0 is the latest stable version at the time of this comment. 1.8.1 was added automatically on running thego getcommand above. After updating thego.modfile I ran the same command again
# In the keptn root directory
$ go get cli/pkg/config- Did
git pull upstream master --rebaseand resolved merge conflicts. I accepted both the changes (from upstreammasterand my branch)
cli/pkg/config/cli_config.go
Outdated
There was a problem hiding this comment.
Maintain an internal CLIConfigManager struct to avoid re-initializing new viper instances.
cli/pkg/config/cli_config.go
Outdated
There was a problem hiding this comment.
StringToTimeHookFunc decode hook was added because viper.Unmarshal does not know how to unmarshal json time string into *time.Time (it throws an error if we don't add the decode hook)
cli/pkg/config/cli_config.go
Outdated
There was a problem hiding this comment.
Many places in the code use LoadCLIConfig which reads in the config file every time LoadCLIConfig function is called. GetCLIConfig is lighter version of the function. It only returns the config already loaded in the Viper.
cli/pkg/config/cli_config.go
Outdated
There was a problem hiding this comment.
config passed as parameter overwrites config stored in the Viper
cli/pkg/config/cli_config.go
Outdated
There was a problem hiding this comment.
Helper function to get default keptn config file path
cli/pkg/config/cli_config_test.go
Outdated
There was a problem hiding this comment.
Viper stores the config like this
{
"automatic_version_check":true,
"current-context":"",
"kube_context_check":true,
"last_version_check":"2020-02-20T00:00:00Z"
}But we want to flatten it out (remove indentations) so that we can compare it with the testConfig
d6e4051 to
8e9701a
Compare
cli/pkg/config/cli_config.go
Outdated
There was a problem hiding this comment.
cli/pkg/config/cli_config.go
Outdated
There was a problem hiding this comment.
I have kept the field private for now. We can make this public in the future if required.
cli/pkg/config/cli_config.go
Outdated
There was a problem hiding this comment.
please use logging.PrintLog() with VerboseLevel instead of fmt.Printf
There was a problem hiding this comment.
I will change this. Thanks for the review 🙏
428afea to
ef509e0
Compare
- custom config file path - this is an overwrite of yesterday's PR - use decode hook to unmarshal time - move viper to CLIConfigMgr - try out writing config using viper - add string param to `NewCLIConfigManager` - use json marshal with viper to write the config -- add a new func to get already loaded cli config -- this would prevent reading in the cli config from the file every time - add viper to go.mod - clean up -- remove shorthand for `config-file` -- shorthand `c` and `f` are already used in subcommands (go test fails) - rename `KeptnConfigPath` -> `GetKeptnDefaultConfigPath` - fix spacing - add viper to go.sum - move cli config path check to NewCLIConfigManager - use specified config path in LoadCLIConfig - print msg about using default config path if none is specified -- print `data` and `testConfig` to analyse the difference -- CI is failing because of ^ - remove indentation added by viper in the tests - add `\n` to log msg - use the same config mgr everywhere -- make viper private (can make it public later if needed) - use viper 1.9.0 -- latest version at the time of this commit - revert cli/main.go (changes not needed) - replace fmt.Printf with verbose level logging.PrintLog - update go.sum Signed-off-by: Suraj Banakar (vadasambar) <[email protected]>
ef509e0 to
5c5a7be
Compare
|
Kudos, SonarCloud Quality Gate passed!
|








Fixes #5490
This PR uses Viper internally to manage the Keptn Config.
Why
How to test
keptn <any-command> --cli-config ~/.foo/my-keptn-configto use a config stored at path other than~/.keptn/configThings to note
go.sum. Adding viper 1.9.0 lead to many changes ingo.sum(additive. I have not removed any dependency)