提交 40a1c757 作者: Overbool

refactor(cmds): use new cmds lib in version

License: MIT
Signed-off-by: 's avatarOverbool <overbool.xu@gmail.com>
上级 2235472d
...@@ -145,7 +145,7 @@ var rootSubcommands = map[string]*cmds.Command{ ...@@ -145,7 +145,7 @@ var rootSubcommands = map[string]*cmds.Command{
"file": lgc.NewCommand(unixfs.UnixFSCmd), "file": lgc.NewCommand(unixfs.UnixFSCmd),
"update": lgc.NewCommand(ExternalBinary()), "update": lgc.NewCommand(ExternalBinary()),
"urlstore": urlStoreCmd, "urlstore": urlStoreCmd,
"version": lgc.NewCommand(VersionCmd), "version": VersionCmd,
"shutdown": daemonShutdownCmd, "shutdown": daemonShutdownCmd,
"cid": CidCmd, "cid": CidCmd,
} }
...@@ -189,7 +189,7 @@ var rootROSubcommands = map[string]*cmds.Command{ ...@@ -189,7 +189,7 @@ var rootROSubcommands = map[string]*cmds.Command{
}, },
}), }),
"resolve": ResolveCmd, "resolve": ResolveCmd,
"version": lgc.NewCommand(VersionCmd), "version": VersionCmd,
} }
func init() { func init() {
......
...@@ -4,13 +4,11 @@ import ( ...@@ -4,13 +4,11 @@ import (
"fmt" "fmt"
"io" "io"
"runtime" "runtime"
"strings"
version "github.com/ipfs/go-ipfs" version "github.com/ipfs/go-ipfs"
cmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
) )
...@@ -41,8 +39,8 @@ var VersionCmd = &cmds.Command{ ...@@ -41,8 +39,8 @@ var VersionCmd = &cmds.Command{
cmdkit.BoolOption(versionRepoOptionName, "Show repo version."), cmdkit.BoolOption(versionRepoOptionName, "Show repo version."),
cmdkit.BoolOption(versionAllOptionName, "Show all version information"), cmdkit.BoolOption(versionAllOptionName, "Show all version information"),
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
res.SetOutput(&VersionOutput{ return res.Emit(&VersionOutput{
Version: version.CurrentVersionNumber, Version: version.CurrentVersionNumber,
Commit: version.CurrentCommit, Commit: version.CurrentCommit,
Repo: fmt.Sprint(fsrepo.RepoVersion), Repo: fmt.Sprint(fsrepo.RepoVersion),
...@@ -50,57 +48,38 @@ var VersionCmd = &cmds.Command{ ...@@ -50,57 +48,38 @@ var VersionCmd = &cmds.Command{
Golang: runtime.Version(), Golang: runtime.Version(),
}) })
}, },
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, version *VersionOutput) error {
v, err := unwrapOutput(res.Output()) repo, _ := req.Options[versionRepoOptionName].(bool)
if err != nil {
return nil, err
}
version, ok := v.(*VersionOutput)
if !ok {
return nil, e.TypeErr(version, v)
}
repo, _, err := res.Request().Option(versionRepoOptionName).Bool()
if err != nil {
return nil, err
}
if repo { if repo {
return strings.NewReader(version.Repo + "\n"), nil fmt.Fprintln(w, version.Repo)
return nil
} }
commit, _, err := res.Request().Option(versionCommitOptionName).Bool() commit, _ := req.Options[versionCommitOptionName].(bool)
commitTxt := "" commitTxt := ""
if err != nil {
return nil, err
}
if commit { if commit {
commitTxt = "-" + version.Commit commitTxt = "-" + version.Commit
} }
number, _, err := res.Request().Option(versionNumberOptionName).Bool() number, _ := req.Options[versionNumberOptionName].(bool)
if err != nil {
return nil, err
}
if number { if number {
return strings.NewReader(fmt.Sprintln(version.Version + commitTxt)), nil fmt.Fprintln(w, version.Version+commitTxt)
return nil
} }
all, _, err := res.Request().Option(versionAllOptionName).Bool() all, _ := req.Options[versionAllOptionName].(bool)
if err != nil {
return nil, err
}
if all { if all {
out := fmt.Sprintf("go-ipfs version: %s-%s\n"+ out := fmt.Sprintf("go-ipfs version: %s-%s\n"+
"Repo version: %s\nSystem version: %s\nGolang version: %s\n", "Repo version: %s\nSystem version: %s\nGolang version: %s\n",
version.Version, version.Commit, version.Repo, version.System, version.Golang) version.Version, version.Commit, version.Repo, version.System, version.Golang)
return strings.NewReader(out), nil fmt.Fprint(w, out)
return nil
} }
return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", version.Version, commitTxt)), nil fmt.Fprint(w, fmt.Sprintf("ipfs version %s%s\n", version.Version, commitTxt))
}, return nil
}),
}, },
Type: VersionOutput{}, Type: VersionOutput{},
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论