@@ -24,6 +24,10 @@ import (
2424 "github.com/spf13/cobra"
2525)
2626
27+ // Version can be set at build time using ldflags:
28+ // go build -ldflags "-X github.com/xbglowx/github-org-repos-sync/cmd.Version=1.0.0"
29+ var Version = "dev"
30+
2731var destPath string
2832var excludeRepoString string
2933var includeRepoString string
@@ -62,25 +66,50 @@ var rootCmd = &cobra.Command{
6266 Long : "Sync github org repos." ,
6367
6468 PreRunE : func (cmd * cobra.Command , args []string ) error {
69+ // Check if version flag is set
70+ if versionFlag , _ := cmd .Flags ().GetBool ("version" ); versionFlag {
71+ fmt .Printf ("github-org-repos-sync version %s\n " , Version )
72+ os .Exit (0 )
73+ }
6574 return checkRequirements ()
6675 },
6776 Run : func (cmd * cobra.Command , args []string ) {
6877 main (args )
6978 },
70- Args : cobra .ExactArgs (1 ),
79+ Args : func (cmd * cobra.Command , args []string ) error {
80+ // If version flag is set, don't require args
81+ if versionFlag , _ := cmd .Flags ().GetBool ("version" ); versionFlag {
82+ return nil
83+ }
84+ return cobra .ExactArgs (1 )(cmd , args )
85+ },
7186 Example : "github-org-repos-sync floorpunch" ,
7287}
7388
89+ // versionCmd represents the version command
90+ var versionCmd = & cobra.Command {
91+ Use : "version" ,
92+ Short : "Print the version number" ,
93+ Long : "Print the version number of github-org-repos-sync" ,
94+ Run : func (cmd * cobra.Command , args []string ) {
95+ fmt .Printf ("github-org-repos-sync version %s\n " , Version )
96+ },
97+ }
98+
7499// Execute adds all child commands to the root command and sets flags appropriately.
75100// This is called by main.main(). It only needs to happen once to the rootCmd.
76101func Execute () {
77102 cobra .CheckErr (rootCmd .Execute ())
78103}
79104
80105func init () {
106+ rootCmd .AddCommand (versionCmd )
81107 rootCmd .Flags ().BoolVar (& skipArchived , "skip-archived" , false , "Skip archived repos?" )
82108 rootCmd .Flags ().StringVar (& excludeRepoString , "exclude-repos" , "" , "Exclude repos that contain string" )
83109 rootCmd .Flags ().StringVarP (& destPath , "destination-path" , "d" , "." , "Destionation path for repos" )
84110 rootCmd .Flags ().StringVar (& includeRepoString , "include-repos" , "" , "Include only repos that contain string" )
85111 rootCmd .Flags ().IntVarP (& parallelism , "parallelism" , "p" , 1 , "Number of parallel git operations" )
112+
113+ // Add version flag that prints version and exits
114+ rootCmd .Flags ().BoolP ("version" , "v" , false , "Print version information" )
86115}
0 commit comments