Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact(cmd): version cmd from old cmd to new cmd #5655

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -189,7 +189,7 @@ var rootROSubcommands = map[string]*cmds.Command{
},
}),
"resolve": ResolveCmd,
"version": lgc.NewCommand(VersionCmd),
"version": VersionCmd,
}

func init() {
Expand Down
55 changes: 20 additions & 35 deletions core/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ 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"

"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)

type VersionOutput struct {
Expand Down Expand Up @@ -41,66 +40,52 @@ 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 cmds.EmitOnce(res, &VersionOutput{
Version: version.CurrentVersionNumber,
Commit: version.CurrentCommit,
Repo: fmt.Sprint(fsrepo.RepoVersion),
System: runtime.GOARCH + "/" + runtime.GOOS, //TODO: Precise version here
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
}

Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
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
return e.TypeErr(version, v)
}

repo, _ := req.Options[versionRepoOptionName].(bool)
if repo {
return strings.NewReader(version.Repo + "\n"), nil
fmt.Fprint(w, version.Repo+"\n")
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.Fprint(w, fmt.Sprintln(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{},
}