mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-20 02:21:48 +08:00
refactor(cmds): use new cmds lib in tar, dns
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
This commit is contained in:
@ -1,16 +1,15 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
|
||||||
|
|
||||||
cmds "github.com/ipfs/go-ipfs/commands"
|
|
||||||
e "github.com/ipfs/go-ipfs/core/commands/e"
|
|
||||||
ncmd "github.com/ipfs/go-ipfs/core/commands/name"
|
ncmd "github.com/ipfs/go-ipfs/core/commands/name"
|
||||||
namesys "github.com/ipfs/go-ipfs/namesys"
|
namesys "github.com/ipfs/go-ipfs/namesys"
|
||||||
nsopts "github.com/ipfs/go-ipfs/namesys/opts"
|
nsopts "github.com/ipfs/go-ipfs/namesys/opts"
|
||||||
|
|
||||||
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds"
|
||||||
|
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -61,10 +60,9 @@ The resolver can recursively resolve:
|
|||||||
Options: []cmdkit.Option{
|
Options: []cmdkit.Option{
|
||||||
cmdkit.BoolOption(dnsRecursiveOptionName, "r", "Resolve until the result is not a DNS link."),
|
cmdkit.BoolOption(dnsRecursiveOptionName, "r", "Resolve until the result is not a DNS link."),
|
||||||
},
|
},
|
||||||
Run: func(req cmds.Request, res cmds.Response) {
|
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||||
|
recursive, _ := req.Options[dnsRecursiveOptionName].(bool)
|
||||||
recursive, _, _ := req.Option(dnsRecursiveOptionName).Bool()
|
name := req.Arguments[0]
|
||||||
name := req.Arguments()[0]
|
|
||||||
resolver := namesys.NewDNSResolver()
|
resolver := namesys.NewDNSResolver()
|
||||||
|
|
||||||
var ropts []nsopts.ResolveOpt
|
var ropts []nsopts.ResolveOpt
|
||||||
@ -72,30 +70,20 @@ The resolver can recursively resolve:
|
|||||||
ropts = append(ropts, nsopts.Depth(1))
|
ropts = append(ropts, nsopts.Depth(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := resolver.Resolve(req.Context(), name, ropts...)
|
output, err := resolver.Resolve(req.Context, name, ropts...)
|
||||||
if err == namesys.ErrResolveFailed {
|
if err == namesys.ErrResolveFailed {
|
||||||
res.SetError(err, cmdkit.ErrNotFound)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
res.SetOutput(&ncmd.ResolvedPath{Path: output})
|
return res.Emit(&ncmd.ResolvedPath{Path: output})
|
||||||
},
|
|
||||||
Marshalers: cmds.MarshalerMap{
|
|
||||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
|
||||||
v, err := unwrapOutput(res.Output())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
output, ok := v.(*ncmd.ResolvedPath)
|
|
||||||
if !ok {
|
|
||||||
return nil, e.TypeErr(output, v)
|
|
||||||
}
|
|
||||||
return strings.NewReader(output.Path.String() + "\n"), nil
|
|
||||||
},
|
},
|
||||||
|
Encoders: cmds.EncoderMap{
|
||||||
|
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ncmd.ResolvedPath) error {
|
||||||
|
fmt.Fprintln(w, out.Path.String())
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
Type: ncmd.ResolvedPath{},
|
Type: ncmd.ResolvedPath{},
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,7 @@ var rootSubcommands = map[string]*cmds.Command{
|
|||||||
"dag": lgc.NewCommand(dag.DagCmd),
|
"dag": lgc.NewCommand(dag.DagCmd),
|
||||||
"dht": lgc.NewCommand(DhtCmd),
|
"dht": lgc.NewCommand(DhtCmd),
|
||||||
"diag": lgc.NewCommand(DiagCmd),
|
"diag": lgc.NewCommand(DiagCmd),
|
||||||
"dns": lgc.NewCommand(DNSCmd),
|
"dns": DNSCmd,
|
||||||
"id": IDCmd,
|
"id": IDCmd,
|
||||||
"key": KeyCmd,
|
"key": KeyCmd,
|
||||||
"log": lgc.NewCommand(LogCmd),
|
"log": lgc.NewCommand(LogCmd),
|
||||||
@ -141,7 +141,7 @@ var rootSubcommands = map[string]*cmds.Command{
|
|||||||
"refs": lgc.NewCommand(RefsCmd),
|
"refs": lgc.NewCommand(RefsCmd),
|
||||||
"resolve": ResolveCmd,
|
"resolve": ResolveCmd,
|
||||||
"swarm": SwarmCmd,
|
"swarm": SwarmCmd,
|
||||||
"tar": lgc.NewCommand(TarCmd),
|
"tar": TarCmd,
|
||||||
"file": lgc.NewCommand(unixfs.UnixFSCmd),
|
"file": lgc.NewCommand(unixfs.UnixFSCmd),
|
||||||
"update": lgc.NewCommand(ExternalBinary()),
|
"update": lgc.NewCommand(ExternalBinary()),
|
||||||
"urlstore": urlStoreCmd,
|
"urlstore": urlStoreCmd,
|
||||||
@ -167,7 +167,7 @@ var rootROSubcommands = map[string]*cmds.Command{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"get": GetCmd,
|
"get": GetCmd,
|
||||||
"dns": lgc.NewCommand(DNSCmd),
|
"dns": DNSCmd,
|
||||||
"ls": lgc.NewCommand(LsCmd),
|
"ls": lgc.NewCommand(LsCmd),
|
||||||
"name": &cmds.Command{
|
"name": &cmds.Command{
|
||||||
Subcommands: map[string]*cmds.Command{
|
Subcommands: map[string]*cmds.Command{
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
|
||||||
|
|
||||||
cmds "github.com/ipfs/go-ipfs/commands"
|
|
||||||
core "github.com/ipfs/go-ipfs/core"
|
core "github.com/ipfs/go-ipfs/core"
|
||||||
e "github.com/ipfs/go-ipfs/core/commands/e"
|
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
|
||||||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
|
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
|
||||||
tar "github.com/ipfs/go-ipfs/tar"
|
tar "github.com/ipfs/go-ipfs/tar"
|
||||||
path "gx/ipfs/QmRKuTyCzg7HFBcV1YUhzStroGtJSb8iWgyxfsDCwFhWTS/go-path"
|
|
||||||
dag "gx/ipfs/QmY8BMUSpCwNiTmFhACmC9Bt1qT63cHP35AoQAus4x14qH/go-merkledag"
|
|
||||||
|
|
||||||
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
"gx/ipfs/QmRKuTyCzg7HFBcV1YUhzStroGtJSb8iWgyxfsDCwFhWTS/go-path"
|
||||||
|
dag "gx/ipfs/QmY8BMUSpCwNiTmFhACmC9Bt1qT63cHP35AoQAus4x14qH/go-merkledag"
|
||||||
|
cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds"
|
||||||
|
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TarCmd = &cmds.Command{
|
var TarCmd = &cmds.Command{
|
||||||
@ -38,47 +38,36 @@ represent it.
|
|||||||
Arguments: []cmdkit.Argument{
|
Arguments: []cmdkit.Argument{
|
||||||
cmdkit.FileArg("file", true, false, "Tar file to add.").EnableStdin(),
|
cmdkit.FileArg("file", true, false, "Tar file to add.").EnableStdin(),
|
||||||
},
|
},
|
||||||
Run: func(req cmds.Request, res cmds.Response) {
|
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||||
nd, err := req.InvocContext().GetNode()
|
nd, err := cmdenv.GetNode(env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fi, err := req.Files().NextFile()
|
fi, err := req.Files.NextFile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := tar.ImportTar(req.Context(), fi, nd.DAG)
|
node, err := tar.ImportTar(req.Context, fi, nd.DAG)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c := node.Cid()
|
c := node.Cid()
|
||||||
|
|
||||||
fi.FileName()
|
fi.FileName()
|
||||||
res.SetOutput(&coreiface.AddEvent{
|
return res.Emit(&coreiface.AddEvent{
|
||||||
Name: fi.FileName(),
|
Name: fi.FileName(),
|
||||||
Hash: c.String(),
|
Hash: c.String(),
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
Type: coreiface.AddEvent{},
|
Type: coreiface.AddEvent{},
|
||||||
Marshalers: cmds.MarshalerMap{
|
Encoders: cmds.EncoderMap{
|
||||||
cmds.Text: func(res cmds.Response) (io.Reader, error) {
|
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *coreiface.AddEvent) error {
|
||||||
v, err := unwrapOutput(res.Output())
|
fmt.Fprintln(w, out.Hash)
|
||||||
if err != nil {
|
return nil
|
||||||
return nil, err
|
}),
|
||||||
}
|
|
||||||
|
|
||||||
o, ok := v.(*coreiface.AddEvent)
|
|
||||||
if !ok {
|
|
||||||
return nil, e.TypeErr(o, v)
|
|
||||||
}
|
|
||||||
return strings.NewReader(o.Hash + "\n"), nil
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,37 +82,32 @@ var tarCatCmd = &cmds.Command{
|
|||||||
Arguments: []cmdkit.Argument{
|
Arguments: []cmdkit.Argument{
|
||||||
cmdkit.StringArg("path", true, false, "ipfs path of archive to export.").EnableStdin(),
|
cmdkit.StringArg("path", true, false, "ipfs path of archive to export.").EnableStdin(),
|
||||||
},
|
},
|
||||||
Run: func(req cmds.Request, res cmds.Response) {
|
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
|
||||||
nd, err := req.InvocContext().GetNode()
|
nd, err := cmdenv.GetNode(env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := path.ParsePath(req.Arguments()[0])
|
p, err := path.ParsePath(req.Arguments[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
root, err := core.Resolve(req.Context(), nd.Namesys, nd.Resolver, p)
|
root, err := core.Resolve(req.Context, nd.Namesys, nd.Resolver, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rootpb, ok := root.(*dag.ProtoNode)
|
rootpb, ok := root.(*dag.ProtoNode)
|
||||||
if !ok {
|
if !ok {
|
||||||
res.SetError(dag.ErrNotProtobuf, cmdkit.ErrNormal)
|
return dag.ErrNotProtobuf
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := tar.ExportTar(req.Context(), rootpb, nd.DAG)
|
r, err := tar.ExportTar(req.Context, rootpb, nd.DAG)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmdkit.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.SetOutput(r)
|
return res.Emit(r)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
version "github.com/ipfs/go-ipfs"
|
version "github.com/ipfs/go-ipfs"
|
||||||
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
|
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
|
||||||
|
|
||||||
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
|
cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds"
|
||||||
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user