Skip to content

Commit

Permalink
cli: split config loading
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jerome-jutteau committed Mar 16, 2022
1 parent 0c14182 commit c042b67
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/frieza/cli_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/frieza/cli_nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/frieza/cli_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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 {
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/frieza/cli_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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())
}
Expand Down
20 changes: 14 additions & 6 deletions internal/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit c042b67

Please sign in to comment.