Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ BATS := $(BATS_DIR)/bin/bats
BINARY := pomodoro

GO_SOURCES := $(shell find . -name '*.go' -not -path './vendor/*')
VERSION := $(shell git describe --tags --exact-match 2>/dev/null || git rev-parse --short HEAD 2>/dev/null || echo "dev")
LDFLAGS := -X main.Version=$(VERSION)

.PHONY: test
test: $(BINARY) $(BATS)
$(BATS) test/

$(BINARY): $(GO_SOURCES) go.mod go.sum
go build -o $(BINARY) .
go build -ldflags "$(LDFLAGS)" -o $(BINARY) .

$(BATS):
@mkdir -p $(BATS_DIR)
Expand Down
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ func initConfig() {
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
// Customize built-in flag descriptions once at startup
if helpFlag := RootCmd.Flags().Lookup("help"); helpFlag != nil {
helpFlag.Usage = "Show help"
}
if versionFlag := RootCmd.Flags().Lookup("version"); versionFlag != nil {
versionFlag.Usage = "Show version info"
}

if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
Expand Down
19 changes: 19 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("pomodoro version %s\n", RootCmd.Version)
},
}

func init() {
RootCmd.AddCommand(versionCmd)
}
21 changes: 21 additions & 0 deletions test/version.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bats

load test_helper

@test "version flag shows version" {
run pomodoro --version
[ "$status" -eq 0 ]
[[ "$output" =~ "pomodoro version" ]]
}

@test "version subcommand shows detailed version info" {
run pomodoro version
[ "$status" -eq 0 ]
[[ "$output" =~ "pomodoro version" ]]
}

@test "version subcommand appears in help" {
run pomodoro --help
[ "$status" -eq 0 ]
[[ "$output" =~ "version Print version information" ]]
}