@cryptotaxi247 / kubo / commits / 40a1c7575

refactor(cmds): use new cmds lib in version

License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>

Overbool committed Oct 26, 2018 at 15:52 UTC 40a1c7575983bdf25651ab449933f5676f9ebc48
2 files changed +20 -41
core/commands/root.go
+2 -2
@@ -145,7 +145,7 @@ var rootSubcommands = map[string]*cmds.Command{
145 "file": lgc.NewCommand(unixfs.UnixFSCmd),
146 "update": lgc.NewCommand(ExternalBinary()),
147 "urlstore": urlStoreCmd,
148 - "version": lgc.NewCommand(VersionCmd),
148 + "version": VersionCmd,
149 "shutdown": daemonShutdownCmd,
150 "cid": CidCmd,
151 }
@@ -189,7 +189,7 @@ var rootROSubcommands = map[string]*cmds.Command{
189 },
190 }),
191 "resolve": ResolveCmd,
192 - "version": lgc.NewCommand(VersionCmd),
192 + "version": VersionCmd,
193 }
194
195 func init() {
core/commands/version.go
+18 -39
@@ -4,13 +4,11 @@ import (
4 "fmt"
5 "io"
6 "runtime"
7 - "strings"
7
8 version "github.com/ipfs/go-ipfs"
10 - cmds "github.com/ipfs/go-ipfs/commands"
11 - e "github.com/ipfs/go-ipfs/core/commands/e"
9 fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
10
11 + cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
12 "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
13 )
14
@@ -41,8 +39,8 @@ var VersionCmd = &cmds.Command{
39 cmdkit.BoolOption(versionRepoOptionName, "Show repo version."),
40 cmdkit.BoolOption(versionAllOptionName, "Show all version information"),
41 },
44 - Run: func(req cmds.Request, res cmds.Response) {
45 - res.SetOutput(&VersionOutput{
42 + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
43 + return res.Emit(&VersionOutput{
44 Version: version.CurrentVersionNumber,
45 Commit: version.CurrentCommit,
46 Repo: fmt.Sprint(fsrepo.RepoVersion),
@@ -50,57 +48,38 @@ var VersionCmd = &cmds.Command{
48 Golang: runtime.Version(),
49 })
50 },
53 - Marshalers: cmds.MarshalerMap{
54 - cmds.Text: func(res cmds.Response) (io.Reader, error) {
55 - v, err := unwrapOutput(res.Output())
56 - if err != nil {
57 - return nil, err
58 - }
59 -
60 - version, ok := v.(*VersionOutput)
61 - if !ok {
62 - return nil, e.TypeErr(version, v)
63 - }
64 -
65 - repo, _, err := res.Request().Option(versionRepoOptionName).Bool()
66 - if err != nil {
67 - return nil, err
68 - }
69 -
51 + Encoders: cmds.EncoderMap{
52 + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, version *VersionOutput) error {
53 + repo, _ := req.Options[versionRepoOptionName].(bool)
54 if repo {
71 - return strings.NewReader(version.Repo + "\n"), nil
55 + fmt.Fprintln(w, version.Repo)
56 + return nil
57 }
58
74 - commit, _, err := res.Request().Option(versionCommitOptionName).Bool()
59 + commit, _ := req.Options[versionCommitOptionName].(bool)
60 commitTxt := ""
76 - if err != nil {
77 - return nil, err
78 - }
61 if commit {
62 commitTxt = "-" + version.Commit
63 }
64
83 - number, _, err := res.Request().Option(versionNumberOptionName).Bool()
84 - if err != nil {
85 - return nil, err
86 - }
65 + number, _ := req.Options[versionNumberOptionName].(bool)
66 if number {
88 - return strings.NewReader(fmt.Sprintln(version.Version + commitTxt)), nil
67 + fmt.Fprintln(w, version.Version+commitTxt)
68 + return nil
69 }
70
91 - all, _, err := res.Request().Option(versionAllOptionName).Bool()
92 - if err != nil {
93 - return nil, err
94 - }
71 + all, _ := req.Options[versionAllOptionName].(bool)
72 if all {
73 out := fmt.Sprintf("go-ipfs version: %s-%s\n"+
74 "Repo version: %s\nSystem version: %s\nGolang version: %s\n",
75 version.Version, version.Commit, version.Repo, version.System, version.Golang)
99 - return strings.NewReader(out), nil
76 + fmt.Fprint(w, out)
77 + return nil
78 }
79
102 - return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", version.Version, commitTxt)), nil
103 - },
80 + fmt.Fprint(w, fmt.Sprintf("ipfs version %s%s\n", version.Version, commitTxt))
81 + return nil
82 + }),
83 },
84 Type: VersionOutput{},
85 }