github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/cmd/ipfs/gen.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" 8 "github.com/jbenet/go-ipfs/core/commands" 9 "github.com/jbenet/go-ipfs/daemon" 10 u "github.com/jbenet/go-ipfs/util" 11 ) 12 13 // command is the descriptor of an ipfs daemon command. 14 // Used with makeCommand to proxy over commands via the daemon. 15 type command struct { 16 name string 17 args int 18 flags []string 19 online bool 20 cmdFn commands.CmdFunc 21 argFilter func(string) (string, error) 22 } 23 24 // commanderFunc is a function that can be passed into the Commander library as 25 // a command handler. Defined here because commander lacks this definition. 26 type commanderFunc func(*commander.Command, []string) error 27 28 // makeCommand Wraps a commands.CmdFunc so that it may be safely run by the 29 // commander library 30 func makeCommand(cmdDesc command) commanderFunc { 31 return func(c *commander.Command, inp []string) error { 32 if len(inp) < cmdDesc.args { 33 u.POut(c.Long) 34 return nil 35 } 36 confdir, err := getConfigDir(c) 37 if err != nil { 38 return err 39 } 40 41 cmd := daemon.NewCommand() 42 cmd.Command = cmdDesc.name 43 if cmdDesc.argFilter != nil { 44 for _, a := range inp { 45 s, err := cmdDesc.argFilter(a) 46 if err != nil { 47 return err 48 } 49 cmd.Args = append(cmd.Args, s) 50 } 51 } else { 52 cmd.Args = inp 53 } 54 55 for _, a := range cmdDesc.flags { 56 cmd.Opts[a] = c.Flag.Lookup(a).Value.Get() 57 } 58 59 err = daemon.SendCommand(cmd, confdir) 60 if err != nil { 61 log.Info("Executing command locally: %s", err) 62 // Do locally 63 n, err := localNode(confdir, cmdDesc.online) 64 if err != nil { 65 return fmt.Errorf("Local node creation failed: %v", err) 66 } 67 68 return cmdDesc.cmdFn(n, cmd.Args, cmd.Opts, os.Stdout) 69 } 70 return nil 71 } 72 }