feat(config): auto migrate to 1

This commit is contained in:
Jan De Dobbeleer 2022-02-02 12:24:31 +01:00 committed by Jan De Dobbeleer
parent a5162b0f19
commit 0185299263
3 changed files with 17 additions and 7 deletions

View file

@ -23,10 +23,13 @@ const (
JSON string = "json"
YAML string = "yaml"
TOML string = "toml"
configVersion = 1
)
// Config holds all the theme for rendering the prompt
type Config struct {
Version int `json:"version"`
FinalSpace bool `json:"final_space,omitempty"`
OSC99 bool `json:"osc99,omitempty"`
ConsoleTitle bool `json:"console_title,omitempty"`
@ -72,6 +75,10 @@ func (cfg *Config) exitWithError(err error) {
// LoadConfig returns the default configuration including possible user overrides
func LoadConfig(env environment.Environment) *Config {
cfg := loadConfig(env)
if cfg.Version != configVersion {
cfg.Migrate(env)
cfg.Write()
}
return cfg
}

View file

@ -17,21 +17,24 @@ const (
func (cfg *Config) Migrate(env environment.Environment) {
for _, block := range cfg.Blocks {
block.migrate(env)
for _, segment := range block.Segments {
segment.migrate(env, cfg.Version)
}
}
for _, segment := range cfg.Tooltips {
segment.migrate(env)
segment.migrate(env, cfg.Version)
}
cfg.updated = true
cfg.Version = configVersion
}
func (block *Block) migrate(env environment.Environment) {
for _, segment := range block.Segments {
segment.migrate(env)
func (segment *Segment) migrate(env environment.Environment, version int) {
if version < 1 {
segment.migrationOne(env)
}
}
func (segment *Segment) migrate(env environment.Environment) {
func (segment *Segment) migrationOne(env environment.Environment) {
if err := segment.mapSegmentWithWriter(env); err != nil {
return
}

View file

@ -295,7 +295,7 @@ func TestSegmentTemplateMigration(t *testing.T) {
Type: tc.Type,
Properties: tc.Props,
}
segment.migrate(&mock.MockedEnvironment{})
segment.migrationOne(&mock.MockedEnvironment{})
assert.Equal(t, tc.Expected, segment.Properties[properties.SegmentTemplate], tc.Case)
}
}