Commit 330ee8c7 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

added command struct for makeCommand

parent 2bd84312
......@@ -29,4 +29,9 @@ func init() {
cmdIpfsAdd.Flag.Bool("r", false, "add objects recursively")
}
var addCmd = MakeCommand("add", []string{"r"}, commands.Add)
var addCmd = makeCommand(command{
name: "add",
args: 1,
flags: []string{"r"},
cmdFn: commands.Add,
})
......@@ -18,7 +18,12 @@ var cmdIpfsCat = &commander.Command{
Flag: *flag.NewFlagSet("ipfs-cat", flag.ExitOnError),
}
var catCmd = MakeCommand("cat", nil, commands.Cat)
var catCmd = makeCommand(command{
name: "cat",
args: 1,
flags: nil,
cmdFn: commands.Cat,
})
/*
func catCmd(c *commander.Command, inp []string) error {
......
package main
import (
"errors"
"fmt"
"os"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
"github.com/jbenet/go-ipfs/config"
"github.com/jbenet/go-ipfs/core/commands"
"github.com/jbenet/go-ipfs/daemon"
u "github.com/jbenet/go-ipfs/util"
)
// CommanderFunc is a function that can be passed into the Commander library as
// command is the descriptor of an ipfs daemon command.
// Used with makeCommand to proxy over commands via the daemon.
type command struct {
name string
args int
flags []string
online bool
cmdFn commands.CmdFunc
}
// commanderFunc is a function that can be passed into the Commander library as
// a command handler. Defined here because commander lacks this definition.
type CommanderFunc func(*commander.Command, []string) error
type commanderFunc func(*commander.Command, []string) error
// MakeCommand Wraps a commands.CmdFunc so that it may be safely run by the
// makeCommand Wraps a commands.CmdFunc so that it may be safely run by the
// commander library
func MakeCommand(cmdName string, expargs []string, cmdFn commands.CmdFunc) CommanderFunc {
func makeCommand(cmdDesc command) commanderFunc {
return func(c *commander.Command, inp []string) error {
if len(inp) < 1 {
if len(inp) < cmdDesc.args {
u.POut(c.Long)
return nil
}
......@@ -29,34 +37,25 @@ func MakeCommand(cmdName string, expargs []string, cmdFn commands.CmdFunc) Comma
return err
}
confapi, err := config.ReadConfigKey(confdir+"/config", "Addresses.API")
if err != nil {
return err
}
apiaddr, ok := confapi.(string)
if !ok {
return errors.New("ApiAddress in config file was not a string")
}
cmd := daemon.NewCommand()
cmd.Command = cmdName
cmd.Command = cmdDesc.name
cmd.Args = inp
for _, a := range expargs {
for _, a := range cmdDesc.flags {
cmd.Opts[a] = c.Flag.Lookup(a).Value.Get()
}
err = daemon.SendCommand(cmd, apiaddr)
err = daemon.SendCommand(cmd, confdir)
if err != nil {
fmt.Printf("Executing command locally: %s", err)
// Do locally
n, err := localNode(confdir, false)
n, err := localNode(confdir, cmdDesc.online)
if err != nil {
fmt.Println("Local node creation failed.")
return err
}
return cmdFn(n, cmd.Args, cmd.Opts, os.Stdout)
return cmdDesc.cmdFn(n, cmd.Args, cmd.Opts, os.Stdout)
}
return nil
}
......
......@@ -21,4 +21,9 @@ var cmdIpfsLs = &commander.Command{
Flag: *flag.NewFlagSet("ipfs-ls", flag.ExitOnError),
}
var lsCmd = MakeCommand("ls", nil, commands.Ls)
var lsCmd = makeCommand(command{
name: "ls",
args: 1,
flags: nil,
cmdFn: commands.Ls,
})
......@@ -23,4 +23,9 @@ func init() {
cmdIpfsPin.Flag.Int("d", 1, "recursive depth")
}
var pinCmd = MakeCommand("pin", []string{"r", "d"}, commands.Pin)
var pinCmd = makeCommand(command{
name: "pin",
args: 1,
flags: []string{"r", "d"},
cmdFn: commands.Pin,
})
......@@ -28,4 +28,9 @@ func init() {
cmdIpfsRefs.Flag.Bool("u", false, "unique: list each ref only once")
}
var refCmd = MakeCommand("refs", []string{"r", "u"}, commands.Refs)
var refCmd = makeCommand(command{
name: "refs",
args: 1,
flags: []string{"r", "u"},
cmdFn: commands.Refs,
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment