diff --git a/cmd/gas_station_integration_test.go b/cmd/gas_station_integration_test.go index f80cd67..0231d66 100644 --- a/cmd/gas_station_integration_test.go +++ b/cmd/gas_station_integration_test.go @@ -38,5 +38,5 @@ func TestGasStationSetup(t *testing.T) { // Assert values weaveConfig := filepath.Join(weaveDir, "config.json") - testutil.CompareJsonValue(t, weaveConfig, "common.gas_station.mnemonic", testutil.GasStationMnemonic) + testutil.CompareJsonValue(t, weaveConfig, "gas_station.mnemonic", testutil.GasStationMnemonic) } diff --git a/config/config.go b/config/config.go index 07efe6d..e46a3a8 100644 --- a/config/config.go +++ b/config/config.go @@ -3,7 +3,6 @@ package config import ( "encoding/json" "fmt" - "github.com/initia-labs/weave/io" "os" "path/filepath" @@ -11,6 +10,7 @@ import ( "github.com/spf13/viper" "github.com/initia-labs/weave/common" + "github.com/initia-labs/weave/io" ) var DevMode string @@ -67,6 +67,11 @@ func LoadConfig() error { if err := viper.ReadInConfig(); err != nil { return fmt.Errorf("failed to read config file: %v", err) } + + if err := MigrateConfigV1(); err != nil { + return err + } + return nil } @@ -87,7 +92,7 @@ func WriteConfig() error { } func IsFirstTimeSetup() bool { - return viper.Get("common.gas_station") == nil + return viper.Get("gas_station") == nil } func GetGasStationKey() (*GasStationKey, error) { @@ -95,7 +100,7 @@ func GetGasStationKey() (*GasStationKey, error) { return nil, fmt.Errorf("gas station key not exists") } - data := GetConfig("common.gas_station") + data := GetConfig("gas_station") jsonData, err := json.Marshal(data) if err != nil { return nil, fmt.Errorf("failed to marshal json: %v", err) @@ -116,29 +121,32 @@ func AnalyticsOptOut() bool { return true } - if GetConfig("common.analytics_opt_out") == nil { - _ = SetConfig("common.analytics_opt_out", false) + if GetConfig("analytics.opt_out") == nil { + _ = SetConfig("analytics.opt_out", false) return false } - return GetConfig("common.analytics_opt_out").(bool) + return GetConfig("analytics.opt_out").(bool) } func GetAnalyticsDeviceID() string { - if GetConfig("common.analytics_device_id") == nil { + if GetConfig("analytics.device_id") == nil { deviceID := uuid.New().String() - _ = SetConfig("common.analytics_device_id", deviceID) + _ = SetConfig("analytics.device_id", deviceID) return deviceID } - return GetConfig("common.analytics_device_id").(string) + return GetConfig("analytics.device_id").(string) } func SetAnalyticsOptOut(optOut bool) error { - return SetConfig("common.analytics_opt_out", optOut) + return SetConfig("analytics.opt_out", optOut) } -const DefaultConfigTemplate = `{}` +const DefaultConfigTemplate = `{ + "version": 1, + "analytics": {} +}` type GasStationKey struct { InitiaAddress string `json:"initia_address"` @@ -163,3 +171,64 @@ func RecoverGasStationKey(mnemonic string) (*GasStationKey, error) { Mnemonic: mnemonic, }, nil } + +func GetConfigVersion() int { + version := GetConfig("version") + if version == nil { + return 0 + } + return int(GetConfig("version").(float64)) +} + +func MigrateConfigV1() error { + version := GetConfigVersion() + if version == 1 { + return nil // Already at latest version + } + + // Migrate from version 0 to 1 + if version == 0 { + // Preserve existing data + gasStation := GetConfig("common.gas_station") + analyticsOptOut := GetConfig("common.analytics_opt_out") + analyticsDeviceID := GetConfig("common.analytics_device_id") + + // Reset all viper settings + viper.Reset() + + // Reinitialize viper with the config file + homeDir, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("failed to get user home directory: %v", err) + } + configPath := filepath.Join(homeDir, common.WeaveConfigFile) + viper.SetConfigFile(configPath) + + // Create new clean config structure + newConfig := map[string]interface{}{ + "version": 1, + "gas_station": map[string]interface{}{}, + "analytics": map[string]interface{}{}, + } + + // Set the new base config + viper.Set("", newConfig) + + // Restore the data + if gasStation != nil { + viper.Set("gas_station", gasStation) + } + if analyticsOptOut != nil { + viper.Set("analytics.opt_out", analyticsOptOut) + } + if analyticsDeviceID != nil { + viper.Set("analytics.device_id", analyticsDeviceID) + } + + if err := WriteConfig(); err != nil { + return fmt.Errorf("failed to migrate config to version 1: %v", err) + } + } + + return nil +} diff --git a/models/initialize.go b/models/initialize.go index f86146c..fad547a 100644 --- a/models/initialize.go +++ b/models/initialize.go @@ -322,7 +322,7 @@ func WaitSetGasStation(mnemonic string) tea.Cmd { return ui.ErrorLoading{Err: fmt.Errorf("failed to recover gas station key: %w", err)} } - err = config.SetConfig("common.gas_station", gasStationKey) + err = config.SetConfig("gas_station", gasStationKey) if err != nil { return ui.ErrorLoading{Err: fmt.Errorf("failed to set gas station in config: %w", err)} } diff --git a/models/minitia/launch.go b/models/minitia/launch.go index f40befb..c5c7cd2 100644 --- a/models/minitia/launch.go +++ b/models/minitia/launch.go @@ -1291,7 +1291,7 @@ func (m *GasStationMnemonicInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if err != nil { return m, m.HandlePanic(err) } - err = config.SetConfig("common.gas_station", gasStationKey) + err = config.SetConfig("gas_station", gasStationKey) if err != nil { return m, m.HandlePanic(err) } diff --git a/models/minitia/launch_test.go b/models/minitia/launch_test.go index 8b11d5c..98f7377 100644 --- a/models/minitia/launch_test.go +++ b/models/minitia/launch_test.go @@ -1035,7 +1035,7 @@ func TestWaitExistingGasStationChecker_ExistingSetup(t *testing.T) { func TestWaitExistingGasStationChecker_NonExistingSetup(t *testing.T) { InitializeViperForTest(t) key, _ := config.RecoverGasStationKey("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon") - viper.Set("common.gas_station", key) + viper.Set("gas_station", key) ctx := weavecontext.NewAppContext(*NewLaunchState()) cmd := waitExistingGasStationChecker(ctx)