version.go 2.45 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
	version "github.com/ipfs/go-ipfs"
10
	cmds "github.com/ipfs/go-ipfs/commands"
Jan Winkelmann's avatar
Jan Winkelmann committed
11
	e "github.com/ipfs/go-ipfs/core/commands/e"
12
	fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
13

14
	"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
15 16 17 18
)

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

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

Jan Winkelmann's avatar
Jan Winkelmann committed
31
	Options: []cmdkit.Option{
32 33 34 35
		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"),
36
	},
37 38
	Run: func(req cmds.Request, res cmds.Response) {
		res.SetOutput(&VersionOutput{
39 40
			Version: version.CurrentVersionNumber,
			Commit:  version.CurrentCommit,
41
			Repo:    fmt.Sprint(fsrepo.RepoVersion),
Jakub Sztandera's avatar
Jakub Sztandera committed
42 43
			System:  runtime.GOARCH + "/" + runtime.GOOS, //TODO: Precise version here
			Golang:  runtime.Version(),
44
		})
45
	},
46
	Marshalers: cmds.MarshalerMap{
47
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
Jan Winkelmann's avatar
Jan Winkelmann committed
48 49 50 51 52 53 54 55 56
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}

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

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

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

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

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

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

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