forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
88 lines (77 loc) · 2.2 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package commands
import (
"fmt"
"io"
"runtime"
"strings"
cmds "github.com/ipfs/go-ipfs/commands"
config "github.com/ipfs/go-ipfs/repo/config"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
)
type VersionOutput struct {
Version string
Commit string
Repo string
System string
Golang string
}
var VersionCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Show ipfs version information.",
ShortDescription: "Returns the current version of ipfs and exits.",
},
Options: []cmds.Option{
cmds.BoolOption("number", "n", "Only show the version number.").Default(false),
cmds.BoolOption("commit", "Show the commit hash.").Default(false),
cmds.BoolOption("repo", "Show repo version.").Default(false),
cmds.BoolOption("all", "Show all version information").Default(false),
},
Run: func(req cmds.Request, res cmds.Response) {
res.SetOutput(&VersionOutput{
Version: config.CurrentVersionNumber,
Commit: config.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 := res.Output().(*VersionOutput)
repo, _, err := res.Request().Option("repo").Bool()
if err != nil {
return nil, err
}
if repo {
return strings.NewReader(v.Repo + "\n"), nil
}
commit, _, err := res.Request().Option("commit").Bool()
commitTxt := ""
if err != nil {
return nil, err
}
if commit {
commitTxt = "-" + v.Commit
}
number, _, err := res.Request().Option("number").Bool()
if err != nil {
return nil, err
}
if number {
return strings.NewReader(fmt.Sprintln(v.Version + commitTxt)), nil
}
all, _, err := res.Request().Option("all").Bool()
if err != nil {
return nil, err
}
if all {
out := fmt.Sprintf("go-ipfs version: %s-%s\n"+
"Repo version: %s\nSystem version: %s\nGolang version: %s\n",
v.Version, v.Commit, v.Repo, v.System, v.Golang)
return strings.NewReader(out), nil
}
return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", v.Version, commitTxt)), nil
},
},
Type: VersionOutput{},
}