version.go 2.46 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"
7
	"strings"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
8

9
	cmds "github.com/ipfs/go-ipfs/commands"
Jan Winkelmann's avatar
Jan Winkelmann committed
10
	e "github.com/ipfs/go-ipfs/core/commands/e"
11
	config "github.com/ipfs/go-ipfs/repo/config"
12
	fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
13
	"gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit"
14 15 16 17
)

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24
var VersionCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
25
	Helptext: cmdkit.HelpText{
26
		Tagline:          "Show ipfs version information.",
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27
		ShortDescription: "Returns the current version of ipfs and exits.",
28
	},
29

Jan Winkelmann's avatar
Jan Winkelmann committed
30
	Options: []cmdkit.Option{
31 32 33 34
		cmdkit.BoolOption("number", "n", "Only show the version number."),
		cmdkit.BoolOption("commit", "Show the commit hash."),
		cmdkit.BoolOption("repo", "Show repo version."),
		cmdkit.BoolOption("all", "Show all version information"),
35
	},
36 37
	Run: func(req cmds.Request, res cmds.Response) {
		res.SetOutput(&VersionOutput{
38
			Version: config.CurrentVersionNumber,
39
			Commit:  config.CurrentCommit,
40
			Repo:    fmt.Sprint(fsrepo.RepoVersion),
Jakub Sztandera's avatar
Jakub Sztandera committed
41 42
			System:  runtime.GOARCH + "/" + runtime.GOOS, //TODO: Precise version here
			Golang:  runtime.Version(),
43
		})
44
	},
45
	Marshalers: cmds.MarshalerMap{
46
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
Jan Winkelmann's avatar
Jan Winkelmann committed
47 48 49 50 51 52 53 54 55
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}

			version, ok := v.(*VersionOutput)
			if !ok {
				return nil, e.TypeErr(version, v)
			}
56

57 58 59 60 61 62
			repo, _, err := res.Request().Option("repo").Bool()
			if err != nil {
				return nil, err
			}

			if repo {
Jan Winkelmann's avatar
Jan Winkelmann committed
63
				return strings.NewReader(version.Repo + "\n"), nil
64 65
			}

66
			commit, _, err := res.Request().Option("commit").Bool()
67 68 69 70
			commitTxt := ""
			if err != nil {
				return nil, err
			}
71
			if commit {
Jan Winkelmann's avatar
Jan Winkelmann committed
72
				commitTxt = "-" + version.Commit
73 74
			}

75
			number, _, err := res.Request().Option("number").Bool()
76 77 78
			if err != nil {
				return nil, err
			}
79
			if number {
Jan Winkelmann's avatar
Jan Winkelmann committed
80
				return strings.NewReader(fmt.Sprintln(version.Version + commitTxt)), nil
81
			}
82

Jakub Sztandera's avatar
Jakub Sztandera committed
83 84 85 86 87 88 89
			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",
Jan Winkelmann's avatar
Jan Winkelmann committed
90
					version.Version, version.Commit, version.Repo, version.System, version.Golang)
Jakub Sztandera's avatar
Jakub Sztandera committed
91 92 93
				return strings.NewReader(out), nil
			}

Jan Winkelmann's avatar
Jan Winkelmann committed
94
			return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", version.Version, commitTxt)), nil
95 96
		},
	},
97
	Type: VersionOutput{},
98
}