diff --git a/cmd/ipfs/add.go b/cmd/ipfs/add.go index 209c3f42cda..b5c475d6400 100644 --- a/cmd/ipfs/add.go +++ b/cmd/ipfs/add.go @@ -46,7 +46,11 @@ func addCmd(c *commander.Command, inp []string) error { err := daemon.SendCommand(cmd, "localhost:12345") if err != nil { // Do locally - n, err := localNode(false) + conf, err := getConfigDir(c.Parent) + if err != nil { + return err + } + n, err := localNode(conf, false) if err != nil { return err } diff --git a/cmd/ipfs/cat.go b/cmd/ipfs/cat.go index 2860c380b45..68e7daeaf93 100644 --- a/cmd/ipfs/cat.go +++ b/cmd/ipfs/cat.go @@ -28,18 +28,17 @@ 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 { - n, err := localNode(false) + conf, err := getConfigDir(c.Parent) + if err != nil { + return err + } + n, err := localNode(conf, false) if err != nil { return err } diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index a70e0eb819b..ef5b0d18c48 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -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)") } @@ -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.") } diff --git a/cmd/ipfs/ipfs.go b/cmd/ipfs/ipfs.go index b54766fca05..5e895175ba9 100644 --- a/cmd/ipfs/ipfs.go +++ b/cmd/ipfs/ipfs.go @@ -1,12 +1,13 @@ package main import ( + "errors" "fmt" "os" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" - config "github.com/jbenet/go-ipfs/config" + "github.com/jbenet/go-ipfs/config" core "github.com/jbenet/go-ipfs/core" u "github.com/jbenet/go-ipfs/util" ) @@ -51,6 +52,10 @@ Use "ipfs help " for more information about a command. Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError), } +func init() { + CmdIpfs.Flag.String("c", config.DefaultPathRoot, "specify config directory") +} + func ipfsCmd(c *commander.Command, args []string) error { u.POut(c.Long) return nil @@ -68,12 +73,25 @@ func main() { return } -func localNode(online bool) (*core.IpfsNode, error) { - //todo implement config file flag - cfg, err := config.Load("") +func localNode(confdir string, online bool) (*core.IpfsNode, error) { + cfg, err := config.Load(confdir + "/config") if err != nil { return nil, err } return core.NewIpfsNode(cfg, online) } + +// Gets the config "-c" flag from the command, or returns +// the empty 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 "", errors.New("failed to retrieve config flag value.") + } + return confStr, nil +} diff --git a/cmd/ipfs/ls.go b/cmd/ipfs/ls.go index 33e85d8e9af..59c7ee10cd7 100644 --- a/cmd/ipfs/ls.go +++ b/cmd/ipfs/ls.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag" @@ -37,8 +36,11 @@ func lsCmd(c *commander.Command, inp []string) error { com.Args = inp err := daemon.SendCommand(com, "localhost:12345") if err != nil { - fmt.Println(err) - n, err := localNode(false) + conf, err := getConfigDir(c.Parent) + if err != nil { + return err + } + n, err := localNode(conf, false) if err != nil { return err } diff --git a/cmd/ipfs/mount_unix.go b/cmd/ipfs/mount_unix.go index bf0bc19f061..92d445395f6 100644 --- a/cmd/ipfs/mount_unix.go +++ b/cmd/ipfs/mount_unix.go @@ -27,19 +27,21 @@ var cmdIpfsMount = &commander.Command{ } func mountCmd(c *commander.Command, inp []string) error { - u.Debug = true + u.Debug = false if len(inp) < 1 || len(inp[0]) == 0 { u.POut(c.Long) return nil } - fmt.Println("wtf.") - n, err := localNode(true) + conf, err := getConfigDir(c.Parent) + if err != nil { + return err + } + n, err := localNode(conf, true) if err != nil { return err } - fmt.Println("starting new daemon listener...") dl, err := daemon.NewDaemonListener(n, "localhost:12345") if err != nil { return err diff --git a/cmd/ipfs/refs.go b/cmd/ipfs/refs.go index 0acac37bd40..ce45b29f094 100644 --- a/cmd/ipfs/refs.go +++ b/cmd/ipfs/refs.go @@ -1,10 +1,12 @@ package main import ( + "os" + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" - mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" - mdag "github.com/jbenet/go-ipfs/merkledag" + commands "github.com/jbenet/go-ipfs/core/commands" + "github.com/jbenet/go-ipfs/daemon" u "github.com/jbenet/go-ipfs/util" ) @@ -36,52 +38,24 @@ func refCmd(c *commander.Command, inp []string) error { return nil } - n, err := localNode(false) + cmd := daemon.NewCommand() + cmd.Command = "refs" + cmd.Args = inp + cmd.Opts["r"] = c.Flag.Lookup("r").Value.Get() + cmd.Opts["u"] = c.Flag.Lookup("u").Value.Get() + err := daemon.SendCommand(cmd, "localhost:12345") if err != nil { - return err - } - - recursive := c.Flag.Lookup("r").Value.Get().(bool) - unique := c.Flag.Lookup("u").Value.Get().(bool) - refsSeen := map[u.Key]bool{} - - printRef := func(h mh.Multihash) { - if unique { - _, found := refsSeen[u.Key(h)] - if found { - return - } - refsSeen[u.Key(h)] = true - } - - u.POut("%s\n", h.B58String()) - } - - var printRefs func(nd *mdag.Node, recursive bool) - printRefs = func(nd *mdag.Node, recursive bool) { - - for _, link := range nd.Links { - printRef(link.Hash) - - if recursive { - nd, err := n.DAG.Get(u.Key(link.Hash)) - if err != nil { - u.PErr("error: cannot retrieve %s (%s)\n", link.Hash.B58String(), err) - return - } - - printRefs(nd, recursive) - } + // Do locally + conf, err := getConfigDir(c.Parent) + if err != nil { + return err } - } - - for _, fn := range inp { - nd, err := n.Resolver.ResolvePath(fn) + n, err := localNode(conf, false) if err != nil { return err } - printRefs(nd, recursive) + return commands.Refs(n, cmd.Args, cmd.Opts, os.Stdout) } return nil } diff --git a/core/commands/refs.go b/core/commands/refs.go new file mode 100644 index 00000000000..50b39bca00e --- /dev/null +++ b/core/commands/refs.go @@ -0,0 +1,65 @@ +package commands + +import ( + "io" + + mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" + "github.com/jbenet/go-ipfs/core" + mdag "github.com/jbenet/go-ipfs/merkledag" + u "github.com/jbenet/go-ipfs/util" +) + +func Refs(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error { + unique, ok := opts["u"].(bool) + if !ok { + unique = false + } + + recursive, ok := opts["r"].(bool) + if !ok { + recursive = false + } + + var refsSeen map[u.Key]bool + if unique { + refsSeen = make(map[u.Key]bool) + } + + for _, fn := range args { + nd, err := n.Resolver.ResolvePath(fn) + if err != nil { + return err + } + + printRefs(n, nd, refsSeen, recursive) + } + return nil +} + +func printRefs(n *core.IpfsNode, nd *mdag.Node, refSeen map[u.Key]bool, recursive bool) { + for _, link := range nd.Links { + printRef(link.Hash, refSeen) + + if recursive { + nd, err := n.DAG.Get(u.Key(link.Hash)) + if err != nil { + u.PErr("error: cannot retrieve %s (%s)\n", link.Hash.B58String(), err) + return + } + + printRefs(n, nd, refSeen, recursive) + } + } +} + +func printRef(h mh.Multihash, refsSeen map[u.Key]bool) { + if refsSeen != nil { + _, found := refsSeen[u.Key(h)] + if found { + return + } + refsSeen[u.Key(h)] = true + } + + u.POut("%s\n", h.B58String()) +}