refactor(cmd/ipfs2) rename to match cmd/ipfs1
Brian Tiger Chow committed
Oct 29, 2014 at 01:44 UTC
db7a42e73c87272ef50683f19511f0f75f78ea33
3 files changed
+183
-183
cmd/ipfs2/ipfs.go
+6
-169
@@ -1,177 +1,14 @@
1
package main
2
3
import (
4
- "fmt"
5
- "io"
6
- "os"
7
- "runtime/pprof"
8
-
4
cmds "github.com/jbenet/go-ipfs/commands"
10
- cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
11
- cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
12
- "github.com/jbenet/go-ipfs/config"
13
- "github.com/jbenet/go-ipfs/core"
5
commands "github.com/jbenet/go-ipfs/core/commands2"
15
- daemon "github.com/jbenet/go-ipfs/daemon2"
16
- u "github.com/jbenet/go-ipfs/util"
6
)
7
19
-// log is the command logger
20
-var log = u.Logger("cmd/ipfs")
21
-
22
-func main() {
23
- args := os.Args[1:]
24
- root := Root
25
-
26
- req, err := cmdsCli.Parse(args, root)
27
- if err != nil {
28
- fmt.Println(err)
29
- os.Exit(1)
30
- }
31
-
32
- // if the CLI-specific root doesn't contain the command, use the general root
33
- if len(req.Path()) == 0 {
34
- root = commands.Root
35
- req, err = cmdsCli.Parse(args, root)
36
- if err != nil {
37
- fmt.Println(err)
38
- os.Exit(1)
39
- }
40
- }
41
-
42
- cmd, err := root.Get(req.Path())
43
- if err != nil {
44
- fmt.Println(err)
45
- os.Exit(1)
46
- }
47
-
48
- options, err := getOptions(req, root)
49
- if err != nil {
50
- fmt.Println(err)
51
- os.Exit(1)
52
- }
53
-
54
- if help, found := options.Option("help"); found && help.(bool) {
55
- fmt.Println(cmd.Help)
56
- os.Exit(0)
57
- }
58
-
59
- if debug, found := options.Option("debug"); found && debug.(bool) {
60
- u.Debug = true
61
-
62
- // if debugging, setup profiling.
63
- if u.Debug {
64
- ofi, err := os.Create("cpu.prof")
65
- if err != nil {
66
- fmt.Println(err)
67
- return
68
- }
69
- pprof.StartCPUProfile(ofi)
70
- defer ofi.Close()
71
- defer pprof.StopCPUProfile()
72
- }
73
- }
74
-
75
- configPath, err := getConfigRoot(options)
76
- if err != nil {
77
- fmt.Println(err)
78
- os.Exit(1)
79
- }
80
-
81
- conf, err := getConfig(configPath)
82
- if err != nil {
83
- fmt.Println(err)
84
- os.Exit(1)
85
- }
86
-
87
- ctx := req.Context()
88
- ctx.ConfigRoot = configPath
89
- ctx.Config = conf
90
-
91
- if _, found := options.Option("encoding"); !found {
92
- if req.Command().Format != nil {
93
- req.SetOption("encoding", cmds.Text)
94
- } else {
95
- req.SetOption("encoding", cmds.JSON)
96
- }
97
- }
98
-
99
- var res cmds.Response
100
- if root == Root {
101
- res = root.Call(req)
102
-
103
- } else {
104
- local, found := options.Option("local")
105
-
106
- if (!found || !local.(bool)) && daemon.Locked(configPath) {
107
- res, err = cmdsHttp.Send(req)
108
- if err != nil {
109
- fmt.Println(err)
110
- os.Exit(1)
111
- }
112
-
113
- } else {
114
- node, err := core.NewIpfsNode(conf, false)
115
- if err != nil {
116
- fmt.Println(err)
117
- os.Exit(1)
118
- }
119
- ctx.Node = node
120
-
121
- res = root.Call(req)
122
- }
123
- }
124
-
125
- if res.Error() != nil {
126
- fmt.Println(res.Error().Error())
127
-
128
- if cmd.Help != "" && res.Error().Code == cmds.ErrClient {
129
- // TODO: convert from markdown to ANSI terminal format?
130
- fmt.Println(cmd.Help)
131
- }
132
-
133
- os.Exit(1)
134
- }
135
-
136
- _, err = io.Copy(os.Stdout, res)
137
- if err != nil {
138
- fmt.Println(err.Error())
139
- }
140
-}
141
-
142
-func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
143
- tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil, nil)
144
-
145
- options, err := root.GetOptions(tempReq.Path())
146
- if err != nil {
147
- return nil, err
148
- }
149
-
150
- err = tempReq.ConvertOptions(options)
151
- if err != nil {
152
- return nil, err
153
- }
154
-
155
- return tempReq, nil
156
-}
157
-
158
-func getConfigRoot(req cmds.Request) (string, error) {
159
- if opt, found := req.Option("config"); found {
160
- return opt.(string), nil
161
- }
162
-
163
- configPath, err := config.PathRoot()
164
- if err != nil {
165
- return "", err
166
- }
167
- return configPath, nil
168
-}
169
-
170
-func getConfig(path string) (*config.Config, error) {
171
- configFile, err := config.Filename(path)
172
- if err != nil {
173
- return nil, err
174
- }
175
-
176
- return config.Load(configFile)
8
+var Root = &cmds.Command{
9
+ Options: commands.Root.Options,
10
+ Help: commands.Root.Help,
11
+ Subcommands: map[string]*cmds.Command{
12
+ "daemon": Daemon,
13
+ },
14
}
cmd/ipfs2/main.go
new
+177
@@ -0,0 +1,177 @@
1
+package main
2
+
3
+import (
4
+ "fmt"
5
+ "io"
6
+ "os"
7
+ "runtime/pprof"
8
+
9
+ cmds "github.com/jbenet/go-ipfs/commands"
10
+ cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
11
+ cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
12
+ "github.com/jbenet/go-ipfs/config"
13
+ "github.com/jbenet/go-ipfs/core"
14
+ commands "github.com/jbenet/go-ipfs/core/commands2"
15
+ daemon "github.com/jbenet/go-ipfs/daemon2"
16
+ u "github.com/jbenet/go-ipfs/util"
17
+)
18
+
19
+// log is the command logger
20
+var log = u.Logger("cmd/ipfs")
21
+
22
+func main() {
23
+ args := os.Args[1:]
24
+ root := Root
25
+
26
+ req, err := cmdsCli.Parse(args, root)
27
+ if err != nil {
28
+ fmt.Println(err)
29
+ os.Exit(1)
30
+ }
31
+
32
+ // if the CLI-specific root doesn't contain the command, use the general root
33
+ if len(req.Path()) == 0 {
34
+ root = commands.Root
35
+ req, err = cmdsCli.Parse(args, root)
36
+ if err != nil {
37
+ fmt.Println(err)
38
+ os.Exit(1)
39
+ }
40
+ }
41
+
42
+ cmd, err := root.Get(req.Path())
43
+ if err != nil {
44
+ fmt.Println(err)
45
+ os.Exit(1)
46
+ }
47
+
48
+ options, err := getOptions(req, root)
49
+ if err != nil {
50
+ fmt.Println(err)
51
+ os.Exit(1)
52
+ }
53
+
54
+ if help, found := options.Option("help"); found && help.(bool) {
55
+ fmt.Println(cmd.Help)
56
+ os.Exit(0)
57
+ }
58
+
59
+ if debug, found := options.Option("debug"); found && debug.(bool) {
60
+ u.Debug = true
61
+
62
+ // if debugging, setup profiling.
63
+ if u.Debug {
64
+ ofi, err := os.Create("cpu.prof")
65
+ if err != nil {
66
+ fmt.Println(err)
67
+ return
68
+ }
69
+ pprof.StartCPUProfile(ofi)
70
+ defer ofi.Close()
71
+ defer pprof.StopCPUProfile()
72
+ }
73
+ }
74
+
75
+ configPath, err := getConfigRoot(options)
76
+ if err != nil {
77
+ fmt.Println(err)
78
+ os.Exit(1)
79
+ }
80
+
81
+ conf, err := getConfig(configPath)
82
+ if err != nil {
83
+ fmt.Println(err)
84
+ os.Exit(1)
85
+ }
86
+
87
+ ctx := req.Context()
88
+ ctx.ConfigRoot = configPath
89
+ ctx.Config = conf
90
+
91
+ if _, found := options.Option("encoding"); !found {
92
+ if req.Command().Format != nil {
93
+ req.SetOption("encoding", cmds.Text)
94
+ } else {
95
+ req.SetOption("encoding", cmds.JSON)
96
+ }
97
+ }
98
+
99
+ var res cmds.Response
100
+ if root == Root {
101
+ res = root.Call(req)
102
+
103
+ } else {
104
+ local, found := options.Option("local")
105
+
106
+ if (!found || !local.(bool)) && daemon.Locked(configPath) {
107
+ res, err = cmdsHttp.Send(req)
108
+ if err != nil {
109
+ fmt.Println(err)
110
+ os.Exit(1)
111
+ }
112
+
113
+ } else {
114
+ node, err := core.NewIpfsNode(conf, false)
115
+ if err != nil {
116
+ fmt.Println(err)
117
+ os.Exit(1)
118
+ }
119
+ ctx.Node = node
120
+
121
+ res = root.Call(req)
122
+ }
123
+ }
124
+
125
+ if res.Error() != nil {
126
+ fmt.Println(res.Error().Error())
127
+
128
+ if cmd.Help != "" && res.Error().Code == cmds.ErrClient {
129
+ // TODO: convert from markdown to ANSI terminal format?
130
+ fmt.Println(cmd.Help)
131
+ }
132
+
133
+ os.Exit(1)
134
+ }
135
+
136
+ _, err = io.Copy(os.Stdout, res)
137
+ if err != nil {
138
+ fmt.Println(err.Error())
139
+ }
140
+}
141
+
142
+func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
143
+ tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil, nil)
144
+
145
+ options, err := root.GetOptions(tempReq.Path())
146
+ if err != nil {
147
+ return nil, err
148
+ }
149
+
150
+ err = tempReq.ConvertOptions(options)
151
+ if err != nil {
152
+ return nil, err
153
+ }
154
+
155
+ return tempReq, nil
156
+}
157
+
158
+func getConfigRoot(req cmds.Request) (string, error) {
159
+ if opt, found := req.Option("config"); found {
160
+ return opt.(string), nil
161
+ }
162
+
163
+ configPath, err := config.PathRoot()
164
+ if err != nil {
165
+ return "", err
166
+ }
167
+ return configPath, nil
168
+}
169
+
170
+func getConfig(path string) (*config.Config, error) {
171
+ configFile, err := config.Filename(path)
172
+ if err != nil {
173
+ return nil, err
174
+ }
175
+
176
+ return config.Load(configFile)
177
+}
cmd/ipfs2/root.go
deleted
-14
@@ -1,14 +0,0 @@
1
-package main
2
-
3
-import (
4
- cmds "github.com/jbenet/go-ipfs/commands"
5
- commands "github.com/jbenet/go-ipfs/core/commands2"
6
-)
7
-
8
-var Root = &cmds.Command{
9
- Options: commands.Root.Options,
10
- Help: commands.Root.Help,
11
- Subcommands: map[string]*cmds.Command{
12
- "daemon": Daemon,
13
- },
14
-}