version.go 1.32 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 11 12 13
)

type VersionOutput struct {
	Version string
14
	Commit string
15 16
}

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

23
	Options: []cmds.Option{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
24
		cmds.BoolOption("number", "n", "Only show the version number"),
25
		cmds.BoolOption("commit", "Show the commit hash"),
26
	},
27 28
	Run: func(req cmds.Request, res cmds.Response) {
		res.SetOutput(&VersionOutput{
29
			Version: config.CurrentVersionNumber,
30
			Commit: config.CurrentCommit,
31
		})
32
	},
33
	Marshalers: cmds.MarshalerMap{
34
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
35 36
			v := res.Output().(*VersionOutput)

37 38 39 40 41 42 43 44 45
			commit, found, err := res.Request().Option("commit").Bool()
			commitTxt := ""
			if err != nil {
				return nil, err
			}
			if found && commit {
				commitTxt = "-" + v.Commit
			}

46 47 48 49
			number, found, err := res.Request().Option("number").Bool()
			if err != nil {
				return nil, err
			}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
50
			if found && number {
51
				return strings.NewReader(fmt.Sprintln(v.Version + commitTxt)), nil
52
			}
53 54

			return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", v.Version, commitTxt)), nil
55 56
		},
	},
57
	Type: VersionOutput{},
58
}