@@ -17,29 +17,56 @@ package cmd
1717import (
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
2532var 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
4167func 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'" )
0 commit comments