This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
173 lines (143 loc) · 3.75 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"os"
"path"
"github.com/ipfs/ipfs-ds-convert/convert"
"github.com/ipfs/ipfs-ds-convert/repo"
"github.com/ipfs/ipfs-ds-convert/revert"
homedir "github.com/mitchellh/go-homedir"
cli "github.com/urfave/cli"
)
const (
DefaultPathName = ".ipfs"
DefaultPathRoot = "~/" + DefaultPathName
DefaultConfigFile = "config"
EnvDir = "IPFS_PATH"
)
func main() {
run(os.Args)
}
func run(args []string) {
app := cli.NewApp()
app.Version = repo.ToolVersion
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Usage: "print verbose logging information",
},
}
app.Before = func(c *cli.Context) error {
return nil
}
app.Commands = []cli.Command{
ConvertCommand,
RevertCommand,
CleanupCommand,
}
if err := app.Run(args); err != nil {
convert.Log.Fatal(err)
}
}
var ConvertCommand = cli.Command{
Name: "convert",
Usage: "convert datastore ",
Description: `'convert' converts existing ipfs datastore setup to another based on the
ipfs configuration and repo specs.
Note that depending on configuration you are converting to up to double the
disk space may be required.
If you have any doubts about your configuration, run the tool conversion with
--keep option enabled
IPFS_PATH environmental variable is respected
`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "keep",
Usage: "don't remove backup files after successful conversion",
},
},
Action: func(c *cli.Context) error {
baseDir, err := getBaseDir()
if err != nil {
convert.Log.Fatal(err)
}
err = convert.Convert(baseDir, c.Bool("keep"))
if err != nil {
convert.Log.Fatal(err)
}
return err
},
}
var RevertCommand = cli.Command{
Name: "revert",
Usage: "revert conversion steps",
Description: `'reverts' attempts to revert changes done to ipfs repo by 'convert'.
It's possible to run revert when conversion failed in middle of the process or
if it was run with --keep option enabled.
Note that in some cases revert may fail in a non-graceful way. When running
revert after other programs used the datastore (like ipfs daemon), changes made
by it between 'convert' and 'revert' may be lost. This may lead to repo
corruption in extreme cases.
Use this command with care, make sure you have some free disk space.
If you have any important data in the repo it's highly recommended to backup the
repo before running this command if you haven't already.
IPFS_PATH environmental variable is respected
`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force",
Usage: "revert even if last conversion was successful",
},
cli.BoolFlag{
Name: "fix-config",
Usage: "revert repo config from datastore_spec",
},
},
Action: func(c *cli.Context) error {
baseDir, err := getBaseDir()
if err != nil {
convert.Log.Fatal(err)
}
err = revert.Revert(baseDir, c.Bool("force"), c.Bool("fix-config"), false)
if err != nil {
convert.Log.Fatal(err)
}
return err
},
}
var CleanupCommand = cli.Command{
Name: "cleanup",
Usage: "remove leftover backup files",
Description: `'cleanup' removes backup files left after successful convert --keep
was run.
IPFS_PATH environmental variable is respected
`,
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
baseDir, err := getBaseDir()
if err != nil {
convert.Log.Fatal(err)
}
err = revert.Revert(baseDir, c.Bool("force"), false, true)
if err != nil {
convert.Log.Fatal(err)
}
return err
},
}
//TODO: Patch config util command
func getBaseDir() (string, error) {
baseDir := os.Getenv(EnvDir)
if baseDir == "" {
baseDir = DefaultPathRoot
}
baseDir, err := homedir.Expand(baseDir)
if err != nil {
return "", err
}
configFile := path.Join(baseDir, DefaultConfigFile)
_, err = os.Stat(configFile)
if err != nil {
return "", err
}
return baseDir, nil
}