forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Migration022.go
97 lines (79 loc) · 2.68 KB
/
Migration022.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package migrations
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
)
// Migration022 migrates the config file to set the IPNSExtra: APIRouter option
// in the config file. Also deletes IPNSExtra: FallbackAPI if it exists.
type Migration022 struct{}
func (Migration022) Up(repoPath, dbPassword string, testnet bool) error {
var (
configMap = map[string]interface{}{}
configBytes, err = ioutil.ReadFile(path.Join(repoPath, "config"))
)
if err != nil {
return fmt.Errorf("reading config: %s", err.Error())
}
if err = json.Unmarshal(configBytes, &configMap); err != nil {
return fmt.Errorf("unmarshal config: %s", err.Error())
}
iIPNSExtra, ok := configMap["IpnsExtra"]
if !ok {
return fmt.Errorf("unmarshal config: missing IpnsExtra key")
}
ipnsExtra, ok := iIPNSExtra.(map[string]interface{})
if !ok {
return fmt.Errorf("unmarshal config: error type asserting IpnsExtra")
}
ipnsExtra["APIRouter"] = "https://routing.api.openbazaar.org"
delete(ipnsExtra, "FallbackAPI")
configMap["IpnsExtra"] = ipnsExtra
newConfigBytes, err := json.MarshalIndent(configMap, "", " ")
if err != nil {
return fmt.Errorf("marshal migrated config: %s", err.Error())
}
if err := ioutil.WriteFile(path.Join(repoPath, "config"), newConfigBytes, os.ModePerm); err != nil {
return fmt.Errorf("writing migrated config: %s", err.Error())
}
if err := writeRepoVer(repoPath, 23); err != nil {
return fmt.Errorf("bumping repover to 23: %s", err.Error())
}
return nil
}
func (Migration022) Down(repoPath, dbPassword string, testnet bool) error {
var (
configMap = map[string]interface{}{}
configBytes, err = ioutil.ReadFile(path.Join(repoPath, "config"))
)
if err != nil {
return fmt.Errorf("reading config: %s", err.Error())
}
if err = json.Unmarshal(configBytes, &configMap); err != nil {
return fmt.Errorf("unmarshal config: %s", err.Error())
}
iIPNSExtra, ok := configMap["IpnsExtra"]
if !ok {
return fmt.Errorf("unmarshal config: missing IpnsExtra key")
}
ipnsExtra, ok := iIPNSExtra.(map[string]interface{})
if !ok {
return fmt.Errorf("unmarshal config: error type asserting IpnsExtra")
}
delete(ipnsExtra, "APIRouter")
ipnsExtra["FallbackAPI"] = "https://gateway.ob1.io"
configMap["IpnsExtra"] = ipnsExtra
newConfigBytes, err := json.MarshalIndent(configMap, "", " ")
if err != nil {
return fmt.Errorf("marshal migrated config: %s", err.Error())
}
if err := ioutil.WriteFile(path.Join(repoPath, "config"), newConfigBytes, os.ModePerm); err != nil {
return fmt.Errorf("writing migrated config: %s", err.Error())
}
if err := writeRepoVer(repoPath, 22); err != nil {
return fmt.Errorf("dropping repover to 22: %s", err.Error())
}
return nil
}