version.go 2.28 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

Jakub Sztandera's avatar
Jakub Sztandera committed
11 12
	"github.com/ipfs/go-ipfs-cmdkit"
	cmds "github.com/ipfs/go-ipfs-cmds"
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 {
			commit, _ := req.Options[versionCommitOptionName].(bool)
54
			commitTxt := ""
55
			if commit {
Jan Winkelmann's avatar
Jan Winkelmann committed
56
				commitTxt = "-" + version.Commit
57 58
			}

59
			all, _ := req.Options[versionAllOptionName].(bool)
Jakub Sztandera's avatar
Jakub Sztandera committed
60 61 62
			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
63
					version.Version, version.Commit, version.Repo, version.System, version.Golang)
64 65
				fmt.Fprint(w, out)
				return nil
Jakub Sztandera's avatar
Jakub Sztandera committed
66 67
			}

68 69 70 71 72 73 74 75 76 77 78 79
			repo, _ := req.Options[versionRepoOptionName].(bool)
			if repo {
				fmt.Fprintln(w, version.Repo)
				return nil
			}

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

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