1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-03 02:56:09 +08:00

adds the option to see the current git commit with ipfs version --commit

License: MIT
Signed-off-by: Caio Alonso <caio@caioalonso.com>
This commit is contained in:
Caio Alonso
2015-10-17 19:48:39 -03:00
parent 4de5eaad5f
commit e05f2d3724
4 changed files with 23 additions and 4 deletions

View File

@ -5,6 +5,8 @@ else
go_test=go test go_test=go test
endif endif
commit = `git rev-parse --short HEAD`
ldflags = "-X "github.com/ipfs/go-ipfs/repo/config".CurrentCommit=$(commit)"
all: all:
# no-op. try: # no-op. try:
@ -24,7 +26,7 @@ install:
cd cmd/ipfs && go install cd cmd/ipfs && go install
build: build:
cd cmd/ipfs && go build -i cd cmd/ipfs && go build -i -ldflags=$(ldflags)
nofuse: nofuse:
cd cmd/ipfs && go install -tags nofuse cd cmd/ipfs && go install -tags nofuse

View File

@ -1,7 +1,9 @@
all: install all: install
commit = `git rev-parse --short HEAD`
ldflags = "-X "github.com/ipfs/go-ipfs/repo/config".CurrentCommit=$(commit)"
build: build:
go build go build -ldflags=$(ldflags)
install: build install: build
go install go install

View File

@ -11,6 +11,7 @@ import (
type VersionOutput struct { type VersionOutput struct {
Version string Version string
Commit string
} }
var VersionCmd = &cmds.Command{ var VersionCmd = &cmds.Command{
@ -21,24 +22,36 @@ var VersionCmd = &cmds.Command{
Options: []cmds.Option{ Options: []cmds.Option{
cmds.BoolOption("number", "n", "Only show the version number"), cmds.BoolOption("number", "n", "Only show the version number"),
cmds.BoolOption("commit", "Show the commit hash"),
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req cmds.Request, res cmds.Response) {
res.SetOutput(&VersionOutput{ res.SetOutput(&VersionOutput{
Version: config.CurrentVersionNumber, Version: config.CurrentVersionNumber,
Commit: config.CurrentCommit,
}) })
}, },
Marshalers: cmds.MarshalerMap{ Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) { cmds.Text: func(res cmds.Response) (io.Reader, error) {
v := res.Output().(*VersionOutput) v := res.Output().(*VersionOutput)
commit, found, err := res.Request().Option("commit").Bool()
commitTxt := ""
if err != nil {
return nil, err
}
if found && commit {
commitTxt = "-" + v.Commit
}
number, found, err := res.Request().Option("number").Bool() number, found, err := res.Request().Option("number").Bool()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if found && number { if found && number {
return strings.NewReader(fmt.Sprintln(v.Version)), nil return strings.NewReader(fmt.Sprintln(v.Version + commitTxt)), nil
} }
return strings.NewReader(fmt.Sprintf("ipfs version %s\n", v.Version)), nil
return strings.NewReader(fmt.Sprintf("ipfs version %s%s\n", v.Version, commitTxt)), nil
}, },
}, },
Type: VersionOutput{}, Type: VersionOutput{},

View File

@ -9,6 +9,8 @@ import (
// CurrentVersionNumber is the current application's version literal // CurrentVersionNumber is the current application's version literal
const CurrentVersionNumber = "0.3.8-dev" const CurrentVersionNumber = "0.3.8-dev"
// CurrentCommit is the current git commit, this is set as a ldflag in the Makefile
var CurrentCommit string
// Version regulates checking if the most recent version is run // Version regulates checking if the most recent version is run
type Version struct { type Version struct {