Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impv: weave config structure and migration #136

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/gas_station_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
76 changes: 65 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package config
import (
"encoding/json"
"fmt"
"github.com/initia-labs/weave/io"
"os"
"path/filepath"

"github.com/google/uuid"
"github.com/spf13/viper"

"github.com/initia-labs/weave/common"
"github.com/initia-labs/weave/io"
)

var DevMode string
Expand Down Expand Up @@ -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
}

Expand All @@ -87,15 +92,15 @@ func WriteConfig() error {
}

func IsFirstTimeSetup() bool {
return viper.Get("common.gas_station") == nil
return viper.Get("gas_station") == nil
}

func GetGasStationKey() (*GasStationKey, error) {
if IsFirstTimeSetup() {
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)
Expand All @@ -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)
Benzbeeb marked this conversation as resolved.
Show resolved Hide resolved
}

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)
Benzbeeb marked this conversation as resolved.
Show resolved Hide resolved
}

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"`
Expand All @@ -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))
Benzbeeb marked this conversation as resolved.
Show resolved Hide resolved
}
traviolus marked this conversation as resolved.
Show resolved Hide resolved

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
}
2 changes: 1 addition & 1 deletion models/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
}
Expand Down
2 changes: 1 addition & 1 deletion models/minitia/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion models/minitia/launch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Benzbeeb marked this conversation as resolved.
Show resolved Hide resolved
ctx := weavecontext.NewAppContext(*NewLaunchState())

cmd := waitExistingGasStationChecker(ctx)
Expand Down