From 87064b3bbea7c58fe63d29cb013f58428abb7814 Mon Sep 17 00:00:00 2001 From: "beeb@neb.one" Date: Wed, 5 Feb 2025 17:13:07 +0700 Subject: [PATCH 1/4] impv: weave config structure and migration --- cmd/gas_station_integration_test.go | 2 +- common/constants.go | 4 +- config/config.go | 76 ++++++++++++++++++++++++----- models/initialize.go | 2 +- models/minitia/launch.go | 2 +- models/minitia/launch_test.go | 2 +- 6 files changed, 71 insertions(+), 17 deletions(-) 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/common/constants.go b/common/constants.go index e4e6eb6..9cf3f1c 100644 --- a/common/constants.go +++ b/common/constants.go @@ -21,11 +21,11 @@ const ( OPinitDirectory = ".opinit" OPinitAppName = "opinitd" - OPinitKeyFileJson = "/weave.keys.json" + OPinitKeyFileJson = "/weave.keyfile.json" OpinitGeneratedKeyFilename = "weave.opinit.generated" HermesHome = ".hermes" HermesKeysDirectory = HermesHome + "/keys" - HermesKeyFileJson = HermesHome + "/weave.keys.json" + HermesKeyFileJson = HermesHome + "/weave.keyfile.json" HermesTempMnemonicFilename = "weave.mnemonic" ) diff --git a/config/config.go b/config/config.go index 07efe6d..d2248a0 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,49 @@ 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") + + // Clear the config + viper.Set("version", 1) + viper.Set("gas_station", map[string]interface{}{}) + viper.Set("analytics", map[string]interface{}{}) + viper.Set("common", nil) + + // 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) From 83ab2cabe24b3df2b5053bbd86f684b8a02a6435 Mon Sep 17 00:00:00 2001 From: "beeb@neb.one" Date: Wed, 5 Feb 2025 18:15:46 +0700 Subject: [PATCH 2/4] fix: as comment --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index d2248a0..5bc120e 100644 --- a/config/config.go +++ b/config/config.go @@ -177,7 +177,7 @@ func GetConfigVersion() int { if version == nil { return 0 } - return int(GetConfig("version").(float64)) + return GetConfig("version").(int) } func MigrateConfigV1() error { From c1a02ef1efa6d469ccd8ecad8429fad6a40499b3 Mon Sep 17 00:00:00 2001 From: "beeb@neb.one" Date: Wed, 5 Feb 2025 18:26:07 +0700 Subject: [PATCH 3/4] fix: GetConfigVersion --- config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 5bc120e..d2248a0 100644 --- a/config/config.go +++ b/config/config.go @@ -177,7 +177,7 @@ func GetConfigVersion() int { if version == nil { return 0 } - return GetConfig("version").(int) + return int(GetConfig("version").(float64)) } func MigrateConfigV1() error { From 233a90d76fede38abf885b46b3ac714d049d9fad Mon Sep 17 00:00:00 2001 From: "beeb@neb.one" Date: Wed, 5 Feb 2025 19:23:44 +0700 Subject: [PATCH 4/4] fix: remove common --- config/config.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index d2248a0..e46a3a8 100644 --- a/config/config.go +++ b/config/config.go @@ -193,11 +193,26 @@ func MigrateConfigV1() error { analyticsOptOut := GetConfig("common.analytics_opt_out") analyticsDeviceID := GetConfig("common.analytics_device_id") - // Clear the config - viper.Set("version", 1) - viper.Set("gas_station", map[string]interface{}{}) - viper.Set("analytics", map[string]interface{}{}) - viper.Set("common", nil) + // 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 {