mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-20 10:42:11 +08:00
Add config flag to cmd line interface
This commit is contained in:
@ -46,7 +46,8 @@ func addCmd(c *commander.Command, inp []string) error {
|
|||||||
err := daemon.SendCommand(cmd, "localhost:12345")
|
err := daemon.SendCommand(cmd, "localhost:12345")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Do locally
|
// Do locally
|
||||||
n, err := localNode(false)
|
conf := getConfig(c.Parent)
|
||||||
|
n, err := localNode(conf, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,8 @@ func catCmd(c *commander.Command, inp []string) error {
|
|||||||
|
|
||||||
err = daemon.SendCommand(com, "localhost:12345")
|
err = daemon.SendCommand(com, "localhost:12345")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
n, err := localNode(false)
|
conf := getConfig(c.Parent)
|
||||||
|
n, err := localNode(conf, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
|
"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"
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
@ -51,6 +51,10 @@ Use "ipfs help <command>" for more information about a command.
|
|||||||
Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
|
Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
CmdIpfs.Flag.String("c", "~/.go-ipfs/config", "specify config file")
|
||||||
|
}
|
||||||
|
|
||||||
func ipfsCmd(c *commander.Command, args []string) error {
|
func ipfsCmd(c *commander.Command, args []string) error {
|
||||||
u.POut(c.Long)
|
u.POut(c.Long)
|
||||||
return nil
|
return nil
|
||||||
@ -68,12 +72,23 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func localNode(online bool) (*core.IpfsNode, error) {
|
func localNode(conf string, online bool) (*core.IpfsNode, error) {
|
||||||
//todo implement config file flag
|
//todo implement config file flag
|
||||||
cfg, err := config.Load("")
|
cfg, err := config.Load(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return core.NewIpfsNode(cfg, online)
|
return core.NewIpfsNode(cfg, online)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gets the config "-c" flag from the command, or returns
|
||||||
|
// the empty string
|
||||||
|
func getConfig(c *commander.Command) string {
|
||||||
|
conf := c.Flag.Lookup("c").Value.Get()
|
||||||
|
confStr, ok := conf.(string)
|
||||||
|
if ok {
|
||||||
|
return confStr
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
|
||||||
@ -37,8 +36,8 @@ func lsCmd(c *commander.Command, inp []string) error {
|
|||||||
com.Args = inp
|
com.Args = inp
|
||||||
err := daemon.SendCommand(com, "localhost:12345")
|
err := daemon.SendCommand(com, "localhost:12345")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
conf := getConfig(c.Parent)
|
||||||
n, err := localNode(false)
|
n, err := localNode(conf, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -27,19 +27,18 @@ var cmdIpfsMount = &commander.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mountCmd(c *commander.Command, inp []string) error {
|
func mountCmd(c *commander.Command, inp []string) error {
|
||||||
u.Debug = true
|
u.Debug = false
|
||||||
if len(inp) < 1 || len(inp[0]) == 0 {
|
if len(inp) < 1 || len(inp[0]) == 0 {
|
||||||
u.POut(c.Long)
|
u.POut(c.Long)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
fmt.Println("wtf.")
|
|
||||||
|
|
||||||
n, err := localNode(true)
|
conf := getConfig(c.Parent)
|
||||||
|
n, err := localNode(conf, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("starting new daemon listener...")
|
|
||||||
dl, err := daemon.NewDaemonListener(n, "localhost:12345")
|
dl, err := daemon.NewDaemonListener(n, "localhost:12345")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
|
"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"
|
commands "github.com/jbenet/go-ipfs/core/commands"
|
||||||
mdag "github.com/jbenet/go-ipfs/merkledag"
|
"github.com/jbenet/go-ipfs/daemon"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,52 +38,21 @@ func refCmd(c *commander.Command, inp []string) error {
|
|||||||
return nil
|
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 {
|
if err != nil {
|
||||||
return err
|
// Do locally
|
||||||
}
|
conf := getConfig(c.Parent)
|
||||||
|
n, err := localNode(conf, false)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, fn := range inp {
|
|
||||||
nd, err := n.Resolver.ResolvePath(fn)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
printRefs(nd, recursive)
|
return commands.Refs(n, cmd.Args, cmd.Opts, os.Stdout)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
65
core/commands/refs.go
Normal file
65
core/commands/refs.go
Normal file
@ -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())
|
||||||
|
}
|
Reference in New Issue
Block a user