From c042b67f4002856c60cc8d1573400770b9bcf33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Jutteau?= Date: Tue, 15 Mar 2022 16:17:17 +0100 Subject: [PATCH] cli: split config loading MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Have one function to get config with default values and keep one function to get raw config from config.json Signed-off-by: Jérôme Jutteau --- cmd/frieza/cli_clean.go | 2 +- cmd/frieza/cli_nuke.go | 2 +- cmd/frieza/cli_profile.go | 10 +++++----- cmd/frieza/cli_snapshot.go | 8 ++++---- internal/common/config.go | 20 ++++++++++++++------ 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/cmd/frieza/cli_clean.go b/cmd/frieza/cli_clean.go index 2628eee..4df9517 100644 --- a/cmd/frieza/cli_clean.go +++ b/cmd/frieza/cli_clean.go @@ -27,7 +27,7 @@ func clean(customConfigPath string, snapshotName *string, plan bool, autoApprove if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatalf("Cannot load configuration: %s", err.Error()) } diff --git a/cmd/frieza/cli_nuke.go b/cmd/frieza/cli_nuke.go index 51474ce..11ad570 100644 --- a/cmd/frieza/cli_nuke.go +++ b/cmd/frieza/cli_nuke.go @@ -40,7 +40,7 @@ func nuke(customConfigPath string, profiles []string, plan bool, autoApprove boo uniqueProfiles[profile] = true } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatalf("Cannot load configuration: %s", err.Error()) } diff --git a/cmd/frieza/cli_profile.go b/cmd/frieza/cli_profile.go index 4c0e2f7..4a22d5f 100644 --- a/cmd/frieza/cli_profile.go +++ b/cmd/frieza/cli_profile.go @@ -94,7 +94,7 @@ func profileLs(customConfigPath string) { if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatalf("Cannot load configuration: %s", err.Error()) } @@ -108,7 +108,7 @@ func profileDescribe(customConfigPath string, profileName *string) { if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatalf("Cannot load configuration: %s", err.Error()) } @@ -127,7 +127,7 @@ func profileNew(customConfigPath string, newProfile Profile) { if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { config = ConfigNew() if GlobalCliOptions.debug { @@ -152,7 +152,7 @@ func profileRm(customConfigPath string, profileName *string) { if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatal("Cannot load configuration: " + err.Error()) } @@ -172,7 +172,7 @@ func profileTest(customConfigPath string, profileName *string) { if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatalf("Cannot load configuration: %s", err.Error()) } diff --git a/cmd/frieza/cli_snapshot.go b/cmd/frieza/cli_snapshot.go index ae83dd0..4da17b6 100644 --- a/cmd/frieza/cli_snapshot.go +++ b/cmd/frieza/cli_snapshot.go @@ -81,7 +81,7 @@ func snapshotNew(customConfigPath string, args []string) { if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatalf("Cannot load configuration: %s", err.Error()) } @@ -138,7 +138,7 @@ func snapshotLs(customConfigPath string) { if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatalf("Cannot load configuration: %s", err.Error()) } @@ -162,7 +162,7 @@ func snapshotDescribe(customConfigPath string, snapshotName *string) { if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatalf("Cannot load configuration: %s", err.Error()) } @@ -181,7 +181,7 @@ func snapshotRm(customConfigPath string, snapshotName *string) { if len(customConfigPath) > 0 { configPath = &customConfigPath } - config, err := ConfigLoad(configPath) + config, err := ConfigLoadWithDefault(configPath) if err != nil { log.Fatalf("Cannot load configuration: %s", err.Error()) } diff --git a/internal/common/config.go b/internal/common/config.go index 56e6409..5d511a3 100644 --- a/internal/common/config.go +++ b/internal/common/config.go @@ -64,6 +64,20 @@ func DefaultSnapshotFolderPath() (string, error) { return path.Join(home, ".frieza", "snapshots"), nil } +func ConfigLoadWithDefault(customConfigPath *string) (*Config, error) { + config, err := ConfigLoad(customConfigPath) + if err != nil { + return nil, err + } + if len(config.SnapshotFolderPath) == 0 { + config.SnapshotFolderPath, err = DefaultSnapshotFolderPath() + if err != nil { + return nil, err + } + } + return config, nil +} + func ConfigLoad(customConfigPath *string) (*Config, error) { var configPath string var err error @@ -86,12 +100,6 @@ func ConfigLoad(customConfigPath *string) (*Config, error) { if config.Version > ConfigVersion() { return nil, errors.New("configuration version not supported, please upgrade frieza") } - if len(config.SnapshotFolderPath) == 0 { - config.SnapshotFolderPath, err = DefaultSnapshotFolderPath() - if err != nil { - return nil, err - } - } return &config, nil }