Change config flag to accept config dir instead of file path
Jeromy committed
Sep 17, 2014 at 06:03 UTC
2607bee4d69bd8920f6afa37377b01d33397c81d
7 files changed
+54
-23
cmd/ipfs/add.go
+4
-1
@@ -46,7 +46,10 @@ func addCmd(c *commander.Command, inp []string) error {
46
err := daemon.SendCommand(cmd, "localhost:12345")
47
if err != nil {
48
// Do locally
49
- conf := getConfig(c.Parent)
49
+ conf, err := getConfigDir(c.Parent)
50
+ if err != nil {
51
+ return err
52
+ }
53
n, err := localNode(conf, false)
54
if err != nil {
55
return err
cmd/ipfs/cat.go
+6
-8
@@ -28,18 +28,16 @@ func catCmd(c *commander.Command, inp []string) error {
28
return nil
29
}
30
31
- expanded, err := u.ExpandPathnames(inp)
32
- if err != nil {
33
- return err
34
- }
35
-
31
com := daemon.NewCommand()
32
com.Command = "cat"
38
- com.Args = expanded
33
+ com.Args = inp
34
40
- err = daemon.SendCommand(com, "localhost:12345")
35
+ err := daemon.SendCommand(com, "localhost:12345")
36
if err != nil {
42
- conf := getConfig(c.Parent)
37
+ conf, err := getConfigDir(c.Parent)
38
+ if err != nil {
39
+ return err
40
+ }
41
n, err := localNode(conf, false)
42
if err != nil {
43
return err
cmd/ipfs/init.go
+21
-3
@@ -32,12 +32,27 @@ func init() {
32
}
33
34
func initCmd(c *commander.Command, inp []string) error {
35
- filename, err := config.Filename(config.DefaultConfigFilePath)
35
+ configpath, err := getConfigDir(c.Parent)
36
+ if err != nil {
37
+ return err
38
+ }
39
+ if configpath == "" {
40
+ configpath, err = u.TildeExpansion("~/.go-ipfs")
41
+ if err != nil {
42
+ return err
43
+ }
44
+ }
45
+
46
+ filename, err := config.Filename(configpath + "/config")
47
if err != nil {
48
return errors.New("Couldn't get home directory path")
49
}
50
+
51
fi, err := os.Lstat(filename)
40
- force := c.Flag.Lookup("f").Value.Get().(bool)
52
+ force, ok := c.Flag.Lookup("f").Value.Get().(bool)
53
+ if !ok {
54
+ return errors.New("failed to parse force flag")
55
+ }
56
if fi != nil || (err != nil && !os.IsNotExist(err)) && !force {
57
return errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)")
58
}
@@ -55,7 +70,10 @@ func initCmd(c *commander.Command, inp []string) error {
70
// This needs thought
71
// cfg.Identity.Address = ""
72
58
- nbits := c.Flag.Lookup("b").Value.Get().(int)
73
+ nbits, ok := c.Flag.Lookup("b").Value.Get().(int)
74
+ if !ok {
75
+ return errors.New("failed to get bits flag")
76
+ }
77
if nbits < 1024 {
78
return errors.New("Bitsize less than 1024 is considered unsafe.")
79
}
cmd/ipfs/ipfs.go
+11
-8
@@ -1,6 +1,7 @@
1
package main
2
3
import (
4
+ "errors"
5
"fmt"
6
"os"
7
@@ -52,7 +53,7 @@ Use "ipfs help <command>" for more information about a command.
53
}
54
55
func init() {
55
- CmdIpfs.Flag.String("c", "~/.go-ipfs/config", "specify config file")
56
+ CmdIpfs.Flag.String("c", config.DefaultPathRoot, "specify config directory")
57
}
58
59
func ipfsCmd(c *commander.Command, args []string) error {
@@ -72,9 +73,8 @@ func main() {
73
return
74
}
75
75
-func localNode(conf string, online bool) (*core.IpfsNode, error) {
76
- //todo implement config file flag
77
- cfg, err := config.Load(conf)
76
+func localNode(confdir string, online bool) (*core.IpfsNode, error) {
77
+ cfg, err := config.Load(confdir + "/config")
78
if err != nil {
79
return nil, err
80
}
@@ -84,11 +84,14 @@ func localNode(conf string, online bool) (*core.IpfsNode, error) {
84
85
// Gets the config "-c" flag from the command, or returns
86
// the empty string
87
-func getConfig(c *commander.Command) string {
87
+func getConfigDir(c *commander.Command) (string, error) {
88
conf := c.Flag.Lookup("c").Value.Get()
89
+ if conf == nil {
90
+ return "", nil
91
+ }
92
confStr, ok := conf.(string)
90
- if ok {
91
- return confStr
93
+ if !ok {
94
+ return "", errors.New("failed to retrieve config flag value.")
95
}
93
- return ""
96
+ return confStr, nil
97
}
cmd/ipfs/ls.go
+4
-1
@@ -36,7 +36,10 @@ func lsCmd(c *commander.Command, inp []string) error {
36
com.Args = inp
37
err := daemon.SendCommand(com, "localhost:12345")
38
if err != nil {
39
- conf := getConfig(c.Parent)
39
+ conf, err := getConfigDir(c.Parent)
40
+ if err != nil {
41
+ return err
42
+ }
43
n, err := localNode(conf, false)
44
if err != nil {
45
return err
cmd/ipfs/mount_unix.go
+4
-1
@@ -33,7 +33,10 @@ func mountCmd(c *commander.Command, inp []string) error {
33
return nil
34
}
35
36
- conf := getConfig(c.Parent)
36
+ conf, err := getConfigDir(c.Parent)
37
+ if err != nil {
38
+ return err
39
+ }
40
n, err := localNode(conf, true)
41
if err != nil {
42
return err
cmd/ipfs/refs.go
+4
-1
@@ -46,7 +46,10 @@ func refCmd(c *commander.Command, inp []string) error {
46
err := daemon.SendCommand(cmd, "localhost:12345")
47
if err != nil {
48
// Do locally
49
- conf := getConfig(c.Parent)
49
+ conf, err := getConfigDir(c.Parent)
50
+ if err != nil {
51
+ return err
52
+ }
53
n, err := localNode(conf, false)
54
if err != nil {
55
return err