version.go 2.2 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 10
	cmds "github.com/ipfs/go-ipfs/commands"
	config "github.com/ipfs/go-ipfs/repo/config"
11
	fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
12 13 14 15
)

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

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

28
	Options: []cmds.Option{
29 30 31
		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),
Jakub Sztandera's avatar
Jakub Sztandera committed
32
		cmds.BoolOption("all", "Show all version information").Default(false),
33
	},
34 35
	Run: func(req cmds.Request, res cmds.Response) {
		res.SetOutput(&VersionOutput{
36
			Version: config.CurrentVersionNumber,
37
			Commit:  config.CurrentCommit,
38
			Repo:    fmt.Sprint(fsrepo.RepoVersion),
Jakub Sztandera's avatar
Jakub Sztandera committed
39 40
			System:  runtime.GOARCH + "/" + runtime.GOOS, //TODO: Precise version here
			Golang:  runtime.Version(),
41
		})
42
	},
43
	Marshalers: cmds.MarshalerMap{
44
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
45 46
			v := res.Output().(*VersionOutput)

47 48 49 50 51 52 53 54 55
			repo, _, err := res.Request().Option("repo").Bool()
			if err != nil {
				return nil, err
			}

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

56
			commit, _, err := res.Request().Option("commit").Bool()
57 58 59 60
			commitTxt := ""
			if err != nil {
				return nil, err
			}
61
			if commit {
62 63 64
				commitTxt = "-" + v.Commit
			}

65
			number, _, err := res.Request().Option("number").Bool()
66 67 68
			if err != nil {
				return nil, err
			}
69
			if number {
70
				return strings.NewReader(fmt.Sprintln(v.Version + commitTxt)), nil
71
			}
72

Jakub Sztandera's avatar
Jakub Sztandera committed
73 74 75 76 77 78 79 80 81 82 83
			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",
					v.Version, v.Commit, v.Repo, v.System, v.Golang)
				return strings.NewReader(out), nil
			}

84
			return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", v.Version, commitTxt)), nil
85 86
		},
	},
87
	Type: VersionOutput{},
88
}