cmd/ipfs: Use lock to check if daemon is running, if not then run command locally
Matt Bell committed
Oct 27, 2014 at 16:21 UTC
e2ed10b85bf70be7d1c321e32d136a6c334a9710
1 file changed
+46
-5
cmd/ipfs/ipfs.go
+46
-5
@@ -6,9 +6,12 @@ import (
6
"os"
7
"runtime/pprof"
8
9
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/camlistore/lock"
10
+
11
cmds "github.com/jbenet/go-ipfs/commands"
12
cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
13
cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
14
+ "github.com/jbenet/go-ipfs/config"
15
"github.com/jbenet/go-ipfs/core/commands"
16
u "github.com/jbenet/go-ipfs/util"
17
)
@@ -48,7 +51,7 @@ func main() {
51
os.Exit(1)
52
}
53
51
- if debug, ok := options["debug"]; ok && debug.(bool) {
54
+ if debug, found := options.Option("debug"); found && debug.(bool) {
55
u.Debug = true
56
57
// if debugging, setup profiling.
@@ -66,14 +69,40 @@ func main() {
69
70
var res cmds.Response
71
if root == Root {
69
- // TODO: spin up node
72
res = root.Call(req)
73
+
74
} else {
72
- res, err = cmdsHttp.Send(req)
75
+ local := true
76
+
77
+ configPath, err := getConfigPath(options)
78
if err != nil {
79
fmt.Println(err)
80
os.Exit(1)
81
}
82
+
83
+ lockFilePath, err := config.Path(configPath, DaemonLockFile)
84
+ if err != nil {
85
+ fmt.Println(err)
86
+ os.Exit(1)
87
+ }
88
+
89
+ if lk, err := lock.Lock(lockFilePath); err != nil {
90
+ local = false
91
+ } else {
92
+ lk.Close()
93
+ }
94
+
95
+ if !local {
96
+ res, err = cmdsHttp.Send(req)
97
+ if err != nil {
98
+ fmt.Println(err)
99
+ os.Exit(1)
100
+ }
101
+
102
+ } else {
103
+ // TODO: spin up node
104
+ res = root.Call(req)
105
+ }
106
}
107
108
if res.Error() != nil {
@@ -93,7 +122,7 @@ func main() {
122
}
123
}
124
96
-func getOptions(req cmds.Request, root *cmds.Command) (map[string]interface{}, error) {
125
+func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
126
tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil)
127
128
options, err := root.GetOptions(tempReq.Path())
@@ -106,5 +135,17 @@ func getOptions(req cmds.Request, root *cmds.Command) (map[string]interface{}, e
135
return nil, err
136
}
137
109
- return tempReq.Options(), nil
138
+ return tempReq, nil
139
+}
140
+
141
+func getConfigPath(req cmds.Request) (string, error) {
142
+ if opt, found := req.Option("config"); found {
143
+ return opt.(string), nil
144
+ }
145
+
146
+ configPath, err := config.PathRoot()
147
+ if err != nil {
148
+ return "", err
149
+ }
150
+ return configPath, nil
151
}