Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config flag to cmd line interface #86

Merged
merged 2 commits into from
Sep 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/ipfs/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 7 additions & 8 deletions cmd/ipfs/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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
26 changes: 22 additions & 4 deletions cmd/ipfs/ipfs.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -51,6 +52,10 @@ Use "ipfs help <command>" 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
Expand All @@ -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
}
8 changes: 5 additions & 3 deletions cmd/ipfs/ls.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"

"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/ipfs/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 16 additions & 42 deletions cmd/ipfs/refs.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
}
65 changes: 65 additions & 0 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
@@ -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())
}