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

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

8 9
	cmds "github.com/ipfs/go-ipfs/commands"
	config "github.com/ipfs/go-ipfs/repo/config"
10
	fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
11 12 13 14
)

type VersionOutput struct {
	Version string
15 16
	Commit  string
	Repo    string
17 18
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19
var VersionCmd = &cmds.Command{
20
	Helptext: cmds.HelpText{
rht's avatar
rht committed
21
		Tagline:          "Shows ipfs version information.",
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
22
		ShortDescription: "Returns the current version of ipfs and exits.",
23
	},
24

25
	Options: []cmds.Option{
26 27 28
		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),
29
	},
30 31
	Run: func(req cmds.Request, res cmds.Response) {
		res.SetOutput(&VersionOutput{
32
			Version: config.CurrentVersionNumber,
33 34
			Commit:  config.CurrentCommit,
			Repo:    fsrepo.RepoVersion,
35
		})
36
	},
37
	Marshalers: cmds.MarshalerMap{
38
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
39 40
			v := res.Output().(*VersionOutput)

41 42 43 44 45 46 47 48 49
			repo, _, err := res.Request().Option("repo").Bool()
			if err != nil {
				return nil, err
			}

			if repo {
				return strings.NewReader(v.Repo + "\n"), nil
			}

50
			commit, _, err := res.Request().Option("commit").Bool()
51 52 53 54
			commitTxt := ""
			if err != nil {
				return nil, err
			}
55
			if commit {
56 57 58
				commitTxt = "-" + v.Commit
			}

59
			number, _, err := res.Request().Option("number").Bool()
60 61 62
			if err != nil {
				return nil, err
			}
63
			if number {
64
				return strings.NewReader(fmt.Sprintln(v.Version + commitTxt)), nil
65
			}
66 67

			return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", v.Version, commitTxt)), nil
68 69
		},
	},
70
	Type: VersionOutput{},
71
}