version.go 2.35 KB
Newer Older
1 2 3
package commands

import (
Brian Tiger Chow's avatar
Brian Tiger Chow committed
4
	"fmt"
5
	"io"
Jakub Sztandera's avatar
Jakub Sztandera committed
6
	"runtime"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
7

8
	version "github.com/ipfs/go-ipfs"
9
	fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
10

11
	cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds"
12
	"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
13 14 15 16
)

type VersionOutput struct {
	Version string
17 18
	Commit  string
	Repo    string
Jakub Sztandera's avatar
Jakub Sztandera committed
19 20
	System  string
	Golang  string
21 22
}

Kejie Zhang's avatar
Kejie Zhang committed
23 24 25 26 27 28 29
const (
	versionNumberOptionName = "number"
	versionCommitOptionName = "commit"
	versionRepoOptionName   = "repo"
	versionAllOptionName    = "all"
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
var VersionCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
31
	Helptext: cmdkit.HelpText{
32
		Tagline:          "Show ipfs version information.",
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
		ShortDescription: "Returns the current version of ipfs and exits.",
34
	},
35

Jan Winkelmann's avatar
Jan Winkelmann committed
36
	Options: []cmdkit.Option{
Kejie Zhang's avatar
Kejie Zhang committed
37 38 39 40
		cmdkit.BoolOption(versionNumberOptionName, "n", "Only show the version number."),
		cmdkit.BoolOption(versionCommitOptionName, "Show the commit hash."),
		cmdkit.BoolOption(versionRepoOptionName, "Show repo version."),
		cmdkit.BoolOption(versionAllOptionName, "Show all version information"),
41
	},
42
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
43
		return cmds.EmitOnce(res, &VersionOutput{
44 45
			Version: version.CurrentVersionNumber,
			Commit:  version.CurrentCommit,
46
			Repo:    fmt.Sprint(fsrepo.RepoVersion),
Jakub Sztandera's avatar
Jakub Sztandera committed
47 48
			System:  runtime.GOARCH + "/" + runtime.GOOS, //TODO: Precise version here
			Golang:  runtime.Version(),
49
		})
50
	},
51 52 53
	Encoders: cmds.EncoderMap{
		cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, version *VersionOutput) error {
			repo, _ := req.Options[versionRepoOptionName].(bool)
54
			if repo {
55 56
				fmt.Fprintln(w, version.Repo)
				return nil
57 58
			}

59
			commit, _ := req.Options[versionCommitOptionName].(bool)
60
			commitTxt := ""
61
			if commit {
Jan Winkelmann's avatar
Jan Winkelmann committed
62
				commitTxt = "-" + version.Commit
63 64
			}

65
			number, _ := req.Options[versionNumberOptionName].(bool)
66
			if number {
67 68
				fmt.Fprintln(w, version.Version+commitTxt)
				return nil
69
			}
70

71
			all, _ := req.Options[versionAllOptionName].(bool)
Jakub Sztandera's avatar
Jakub Sztandera committed
72 73 74
			if all {
				out := fmt.Sprintf("go-ipfs version: %s-%s\n"+
					"Repo version: %s\nSystem version: %s\nGolang version: %s\n",
Jan Winkelmann's avatar
Jan Winkelmann committed
75
					version.Version, version.Commit, version.Repo, version.System, version.Golang)
76 77
				fmt.Fprint(w, out)
				return nil
Jakub Sztandera's avatar
Jakub Sztandera committed
78 79
			}

80 81 82
			fmt.Fprint(w, fmt.Sprintf("ipfs version %s%s\n", version.Version, commitTxt))
			return nil
		}),
83
	},
84
	Type: VersionOutput{},
85
}