Skip to content

Commit

Permalink
Change config flag to accept config dir instead of file path
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Sep 17, 2014
1 parent 9e4b858 commit 2607bee
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 23 deletions.
5 changes: 4 additions & 1 deletion cmd/ipfs/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func addCmd(c *commander.Command, inp []string) error {
err := daemon.SendCommand(cmd, "localhost:12345")
if err != nil {
// Do locally
conf := getConfig(c.Parent)
conf, err := getConfigDir(c.Parent)
if err != nil {
return err
}
n, err := localNode(conf, false)
if err != nil {
return err
Expand Down
14 changes: 6 additions & 8 deletions cmd/ipfs/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,16 @@ func catCmd(c *commander.Command, inp []string) error {
return nil
}

expanded, err := u.ExpandPathnames(inp)
if err != nil {
return err
}

com := daemon.NewCommand()
com.Command = "cat"
com.Args = expanded
com.Args = inp

err = daemon.SendCommand(com, "localhost:12345")
err := daemon.SendCommand(com, "localhost:12345")
if err != nil {
conf := getConfig(c.Parent)
conf, err := getConfigDir(c.Parent)
if err != nil {
return err
}
n, err := localNode(conf, false)
if err != nil {
return err
Expand Down
24 changes: 21 additions & 3 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,27 @@ func init() {
}

func initCmd(c *commander.Command, inp []string) error {
filename, err := config.Filename(config.DefaultConfigFilePath)
configpath, err := getConfigDir(c.Parent)
if err != nil {
return err
}
if configpath == "" {
configpath, err = u.TildeExpansion("~/.go-ipfs")
if err != nil {
return err
}
}

filename, err := config.Filename(configpath + "/config")
if err != nil {
return errors.New("Couldn't get home directory path")
}

fi, err := os.Lstat(filename)
force := c.Flag.Lookup("f").Value.Get().(bool)
force, ok := c.Flag.Lookup("f").Value.Get().(bool)
if !ok {
return errors.New("failed to parse force flag")
}
if fi != nil || (err != nil && !os.IsNotExist(err)) && !force {
return errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)")
}
Expand All @@ -55,7 +70,10 @@ func initCmd(c *commander.Command, inp []string) error {
// This needs thought
// cfg.Identity.Address = ""

nbits := c.Flag.Lookup("b").Value.Get().(int)
nbits, ok := c.Flag.Lookup("b").Value.Get().(int)
if !ok {
return errors.New("failed to get bits flag")
}
if nbits < 1024 {
return errors.New("Bitsize less than 1024 is considered unsafe.")
}
Expand Down
19 changes: 11 additions & 8 deletions cmd/ipfs/ipfs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"os"

Expand Down Expand Up @@ -52,7 +53,7 @@ Use "ipfs help <command>" for more information about a command.
}

func init() {
CmdIpfs.Flag.String("c", "~/.go-ipfs/config", "specify config file")
CmdIpfs.Flag.String("c", config.DefaultPathRoot, "specify config directory")
}

func ipfsCmd(c *commander.Command, args []string) error {
Expand All @@ -72,9 +73,8 @@ func main() {
return
}

func localNode(conf string, online bool) (*core.IpfsNode, error) {
//todo implement config file flag
cfg, err := config.Load(conf)
func localNode(confdir string, online bool) (*core.IpfsNode, error) {
cfg, err := config.Load(confdir + "/config")
if err != nil {
return nil, err
}
Expand All @@ -84,11 +84,14 @@ func localNode(conf string, online bool) (*core.IpfsNode, error) {

// Gets the config "-c" flag from the command, or returns
// the empty string
func getConfig(c *commander.Command) string {
func getConfigDir(c *commander.Command) (string, error) {
conf := c.Flag.Lookup("c").Value.Get()
if conf == nil {
return "", nil
}
confStr, ok := conf.(string)
if ok {
return confStr
if !ok {
return "", errors.New("failed to retrieve config flag value.")
}
return ""
return confStr, nil
}
5 changes: 4 additions & 1 deletion cmd/ipfs/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func lsCmd(c *commander.Command, inp []string) error {
com.Args = inp
err := daemon.SendCommand(com, "localhost:12345")
if err != nil {
conf := getConfig(c.Parent)
conf, err := getConfigDir(c.Parent)
if err != nil {
return err
}
n, err := localNode(conf, false)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion cmd/ipfs/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func mountCmd(c *commander.Command, inp []string) error {
return nil
}

conf := getConfig(c.Parent)
conf, err := getConfigDir(c.Parent)
if err != nil {
return err
}
n, err := localNode(conf, true)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion cmd/ipfs/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func refCmd(c *commander.Command, inp []string) error {
err := daemon.SendCommand(cmd, "localhost:12345")
if err != nil {
// Do locally
conf := getConfig(c.Parent)
conf, err := getConfigDir(c.Parent)
if err != nil {
return err
}
n, err := localNode(conf, false)
if err != nil {
return err
Expand Down

0 comments on commit 2607bee

Please sign in to comment.