Skip to content

Commit bb6fb4c

Browse files
committed
version: added version flag
Added a version flag '--version' which will output the current release and the git commit short sha. These are compiled into the binary when from goreleaser. Fixes: vishen#28
1 parent 5912b85 commit bb6fb4c

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

.goreleaser.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ before:
99
builds:
1010
- env:
1111
- CGO_ENABLED=0
12+
ldflags:
13+
- -s -w -X main.version={{.Version}} -X main.commit={{.ShortCommit}} -X main.date={{.Date}}
1214
archive:
1315
replacements:
1416
darwin: Darwin

cmd/root.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,56 @@ package cmd
1717
import (
1818
"fmt"
1919
"os"
20+
"time"
2021

2122
"github.com/spf13/cobra"
2223
)
2324

25+
var (
26+
Version = ""
27+
Commit = ""
28+
Date = ""
29+
)
30+
2431
// rootCmd represents the base command when called without any subcommands
2532
var rootCmd = &cobra.Command{
2633
Use: "go-chromecast",
2734
Short: "CLI for interacting with the Google Chromecast",
2835
Long: `Control your Google Chromecast or Google Home Mini from the
29-
command line.
30-
`}
36+
command line.`,
37+
RunE: func(cmd *cobra.Command, args []string) error {
38+
printVersion, _ := cmd.Flags().GetBool("version")
39+
if printVersion {
40+
if len(Version) > 0 && Version[0] != 'v' && Version != "dev" {
41+
Version = "v" + Version
42+
}
43+
fmt.Printf("go-chromecast %s (%s) %s\n", Version, Commit, Date)
44+
return nil
45+
}
46+
cmd.Help()
47+
return nil
48+
},
49+
}
3150

3251
// Execute adds all child commands to the root command and sets flags appropriately.
3352
// This is called by main.main(). It only needs to happen once to the rootCmd.
34-
func Execute() {
53+
func Execute(version, commit, date string) {
54+
Version = version
55+
Commit = commit
56+
if date != "" {
57+
Date = date
58+
} else {
59+
Date = time.Now().UTC().Format(time.RFC3339)
60+
}
3561
if err := rootCmd.Execute(); err != nil {
3662
fmt.Println(err)
3763
os.Exit(1)
3864
}
3965
}
4066

4167
func init() {
42-
rootCmd.PersistentFlags().Bool("debug", false, "debug logging")
68+
rootCmd.PersistentFlags().Bool("version", false, "display command version")
69+
rootCmd.PersistentFlags().BoolP("debug", "v", false, "debug logging")
4370
rootCmd.PersistentFlags().Bool("disable-cache", false, "disable the cache")
4471
rootCmd.PersistentFlags().Bool("with-ui", false, "run with a UI")
4572
rootCmd.PersistentFlags().StringP("device", "d", "", "chromecast device, ie: 'Chromecast' or 'Google Home Mini'")

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ require (
1818
github.com/spf13/pflag v1.0.3 // indirect
1919
google.golang.org/api v0.3.0
2020
google.golang.org/genproto v0.0.0-20190321212433-e79c0c59cdb5
21+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
22+
gopkg.in/yaml.v2 v2.2.2 // indirect
2123
)

main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ package main
1616

1717
import "github.com/vishen/go-chromecast/cmd"
1818

19+
var (
20+
// These are build-time variables that get set by goreleaser.
21+
version = "dev"
22+
commit = "master"
23+
date = ""
24+
)
25+
1926
func main() {
20-
cmd.Execute()
27+
cmd.Execute(version, commit, date)
2128
}

0 commit comments

Comments
 (0)