提交 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{
"file": lgc.NewCommand(unixfs.UnixFSCmd),
"update": lgc.NewCommand(ExternalBinary()),
"urlstore": urlStoreCmd,
"version": lgc.NewCommand(VersionCmd),
"version": VersionCmd,
"shutdown": daemonShutdownCmd,
"cid": CidCmd,
}
......@@ -189,7 +189,7 @@ var rootROSubcommands = map[string]*cmds.Command{
},
}),
"resolve": ResolveCmd,
"version": lgc.NewCommand(VersionCmd),
"version": VersionCmd,
}
func init() {
......
......@@ -4,13 +4,11 @@ import (
"fmt"
"io"
"runtime"
"strings"
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"
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)
......@@ -41,8 +39,8 @@ var VersionCmd = &cmds.Command{
cmdkit.BoolOption(versionRepoOptionName, "Show repo version."),
cmdkit.BoolOption(versionAllOptionName, "Show all version information"),
},
Run: func(req cmds.Request, res cmds.Response) {
res.SetOutput(&VersionOutput{
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
return res.Emit(&VersionOutput{
Version: version.CurrentVersionNumber,
Commit: version.CurrentCommit,
Repo: fmt.Sprint(fsrepo.RepoVersion),
......@@ -50,57 +48,38 @@ var VersionCmd = &cmds.Command{
Golang: runtime.Version(),
})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
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
}
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, version *VersionOutput) error {
repo, _ := req.Options[versionRepoOptionName].(bool)
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 := ""
if err != nil {
return nil, err
}
if commit {
commitTxt = "-" + version.Commit
}
number, _, err := res.Request().Option(versionNumberOptionName).Bool()
if err != nil {
return nil, err
}
number, _ := req.Options[versionNumberOptionName].(bool)
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()
if err != nil {
return nil, err
}
all, _ := req.Options[versionAllOptionName].(bool)
if all {
out := fmt.Sprintf("go-ipfs version: %s-%s\n"+
"Repo version: %s\nSystem version: %s\nGolang version: %s\n",
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{},
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论