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

type VersionOutput struct {
	Version string
}

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

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

34 35 36 37
			number, found, err := res.Request().Option("number").Bool()
			if err != nil {
				return nil, err
			}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
38
			if found && number {
39
				return strings.NewReader(fmt.Sprintln(v.Version)), nil
40
			}
41
			return strings.NewReader(fmt.Sprintf("ipfs version %s\n", v.Version)), nil
42 43
		},
	},
44
	Type: VersionOutput{},
45
}