Files
gocryptotrader/config/versions/v1.go
Gareth Kirwan 219ed903bc Config: Add versioning (#1671)
* Config: Version Management

* Engine: Improve visibility of TestConfigAllJsonResponse failures

* Config: Update cmd/config to allow upgrades

* Config: Add Version2 to rename GDAX

* Config: Restructure versioning to share types

This restructure allows us to share types between versions, avoids
needing to import the versions, and puts the test fixtures in same
package.
It's a win on all fronts

* Config: Fix SetNTPCheck using log

Called from engine before logger is inited, and also just wrong to use
log to communicate with user

* Config: Improve TestMigrateConfig

* Config: Drop requirement for versions to be registered in sequence

Checking the versions at Deploy is much saner.

* Config: Fix file encrypted but flag not set

* Config: Add -edit and encryption upgrade to cmd/config

This simplifies the handling for encryption prompts by moving it to a
field on config, allowing us to simplify all the places were were
passing around config

Also moves password entry to being secure (echo-off)

* Tests: Fix inconsistent should/must assertions
2024-12-09 15:04:16 +11:00

60 lines
1.6 KiB
Go

package versions
import (
"context"
"encoding/json"
"github.com/buger/jsonparser"
v0 "github.com/thrasher-corp/gocryptotrader/config/versions/v0"
v1 "github.com/thrasher-corp/gocryptotrader/config/versions/v1"
)
// Version1 is an ExchangeVersion to upgrade currency pair format for exchanges
type Version1 struct {
}
func init() {
Manager.registerVersion(1, &Version1{})
}
// Exchanges returns all exchanges: "*"
func (v *Version1) Exchanges() []string { return []string{"*"} }
// UpgradeExchange will upgrade currency pair format
func (v *Version1) UpgradeExchange(_ context.Context, e []byte) ([]byte, error) {
if _, d, _, err := jsonparser.Get(e, "currencyPairs"); err == nil && d == jsonparser.Object {
return e, nil
}
d := &v0.Exchange{}
if err := json.Unmarshal(e, d); err != nil {
return e, err
}
p := &v1.PairsManager{
UseGlobalFormat: true,
LastUpdated: d.PairsLastUpdated,
ConfigFormat: d.ConfigCurrencyPairFormat,
RequestFormat: d.RequestCurrencyPairFormat,
Pairs: v1.FullStore{
"spot": {
Available: d.AvailablePairs,
Enabled: d.EnabledPairs,
},
},
}
j, err := json.Marshal(p)
if err != nil {
return e, err
}
for _, f := range []string{"pairsLastUpdated", "configCurrencyPairFormat", "requestCurrencyPairFormat", "assetTypes", "availablePairs", "enabledPairs"} {
e = jsonparser.Delete(e, f)
}
return jsonparser.Set(e, j, "currencyPairs")
}
// DowngradeExchange doesn't do anything for v1; There's no downgrade path since the original state is lossy and v1 was before versioning
func (v *Version1) DowngradeExchange(_ context.Context, e []byte) ([]byte, error) {
return e, nil
}