Skip to content

Commit

Permalink
fix: make upgrade function optional
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy committed Feb 18, 2023
1 parent b036958 commit f60cba5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type (
type options struct {
readFileFunc ReadFileFunc
writeFileFunc WriteFileFunc
UpgradeFunc UpgradeFunc
getLanguageDefaultFunc GetLanguageDefaultFunc
}

Expand All @@ -33,13 +34,19 @@ func WithFileSystemFuncs(rf ReadFileFunc, wf WriteFileFunc) Option {
}
}

func WithUpgradeFunc(f UpgradeFunc) Option {
return func(o *options) {
o.UpgradeFunc = f
}
}

func WithLanguageDefaultFunc(f GetLanguageDefaultFunc) Option {
return func(o *options) {
o.getLanguageDefaultFunc = f
}
}

func Load(dir string, lang string, uf UpgradeFunc, opts ...Option) (*Config, error) {
func Load(dir string, lang string, opts ...Option) (*Config, error) {
o := &options{
readFileFunc: os.ReadFile,
writeFileFunc: os.WriteFile,
Expand Down Expand Up @@ -84,8 +91,12 @@ func Load(dir string, lang string, uf UpgradeFunc, opts ...Option) (*Config, err
}

if version != Version {
if o.UpgradeFunc == nil {
return nil, fmt.Errorf("config is version %s but upgrades not available", version)
}

// Upgrade config file if version is different and write it
cfgMap, err = upgrade(version, cfgMap, uf)
cfgMap, err = upgrade(version, cfgMap, o.UpgradeFunc)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestLoad_Success(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir)

cfg, err := Load(filepath.Join(os.TempDir(), testDir), "go", testUpdateLang)
cfg, err := Load(filepath.Join(os.TempDir(), testDir), "go", WithUpgradeFunc(testUpdateLang))
assert.NoError(t, err)
assert.Equal(t, tt.want, cfg)
_, err = os.Stat(filepath.Join(dir, "gen.yaml"))
Expand Down

0 comments on commit f60cba5

Please sign in to comment.