@@ -3,40 +3,48 @@ package cmd
33import (
44"fmt"
55"os"
6+ "runtime"
67
78"github.com/gesquive/cli"
89"github.com/gesquive/git-user/git"
910"github.com/gesquive/git-user/user"
1011"github.com/spf13/cobra"
1112)
1213
14+ // current build info
15+ var (
16+ BuildVersion = "v0.1.0-dev"
17+ BuildCommit = ""
18+ BuildDate = ""
19+ )
20+
1321var userProfileConfig * git.UserProfileConfig
1422var gitRepo * git.Repo
1523var gitPath string
1624var cfgFilePath string
1725var projectPath string
18- var displayVersion string
1926var appName string
2027
2128var logDebug bool
2229var showVersion bool
2330
24- var global bool
31+ var useGlobal bool
2532
2633// RootCmd represents the base command when called without any subcommands
2734var RootCmd = & cobra.Command {
28- Use : "git-user" ,
29- Short : "Allows you to save multiple user profiles and set them as git project defaults" ,
30- Long : `git-user lets you quickly switch between multiple git user profiles` ,
31- Run : listRun ,
35+ Use : "git-user" ,
36+ Short : "Allows you to save multiple user profiles and set them as git project defaults" ,
37+ Long : `git-user lets you quickly switch between multiple git user profiles` ,
38+ PersistentPreRun : persistentPreRun ,
39+ Run : listRun ,
40+ Hidden : true ,
3241}
3342
3443// Execute adds all child commands to the root command sets flags appropriately.
3544// This is called by main.main(). It only needs to happen once to the rootCmd.
36- func Execute (version string ) {
37- displayVersion = version
38- RootCmd .SetHelpTemplate (fmt .Sprintf ("%s\n Version:\n github.com/gesquive/%s\n " ,
39- RootCmd .HelpTemplate (), displayVersion ))
45+ func Execute () {
46+ RootCmd .SetHelpTemplate (helpTemplate ())
47+ RootCmd .SetUsageTemplate (usageTemplate ())
4048if err := RootCmd .Execute (); err != nil {
4149fmt .Println (err )
4250os .Exit (- 1 )
@@ -56,22 +64,22 @@ func init() {
5664user .ShortenHomeDir (defaultProjectPath ), "The project to get/set the user" )
5765RootCmd .PersistentFlags ().StringVarP (& gitPath , "git-path" , "g" , "git" ,
5866"The git executable to use" )
67+
68+ RootCmd .PersistentFlags ().BoolVarP (& showVersion , "version" , "v" , false ,
69+ "Show the version and exit" )
5970RootCmd .PersistentFlags ().BoolVarP (& logDebug , "debug" , "D" , false ,
6071"Write debug messages to console" )
61- RootCmd .PersistentFlags ().BoolVarP (& showVersion , "version" , "V" , false ,
62- "Show the version and exit" )
63-
6472RootCmd .PersistentFlags ().MarkHidden ("debug" )
73+
74+ // TODO: activate viper for ENV vars
75+ // viper.SetEnvPrefix("git-user")
76+ // viper.AutomaticEnv()
6577}
6678
6779func initConfig () {
6880if logDebug {
6981cli .SetPrintLevel (cli .LevelDebug )
7082}
71- if showVersion {
72- cli .Info (displayVersion )
73- os .Exit (0 )
74- }
7583cli .Debug ("Running with debug turned on" )
7684
7785appName = os .Args [0 ]
@@ -94,8 +102,8 @@ func initConfig() {
94102cli .Debug ("gitPath=%s" , gitPath )
95103cli .Debug ("gitVersion=%s" , gitVersion )
96104
97- cfgFilePath = user .ExpandHomeDir (cfgFilePath )
98- projectPath = user .ExpandHomeDir (projectPath )
105+ cfgFilePath = os . ExpandEnv ( user .ExpandHomeDir (cfgFilePath ) )
106+ projectPath = os . ExpandEnv ( user .ExpandHomeDir (projectPath ) )
99107cli .Debug ("configPath='%s'" , cfgFilePath )
100108cli .Debug ("projectPath='%s'" , projectPath )
101109var err error
@@ -107,3 +115,52 @@ func initConfig() {
107115cli .Debug ("profileConfigPath=%s" , userProfileConfig .Path ())
108116gitRepo = git .NewGitRepo (projectPath )
109117}
118+
119+ func persistentPreRun (cmd * cobra.Command , args []string ) {
120+ if showVersion {
121+ fmt .Printf ("github.com/gesquive/git-user\n " )
122+ fmt .Printf (" Version: %s\n " , BuildVersion )
123+ if len (BuildCommit ) > 6 {
124+ fmt .Printf (" Git Commit: %s\n " , BuildCommit [:7 ])
125+ }
126+ if BuildDate != "" {
127+ fmt .Printf (" Build Date: %s\n " , BuildDate )
128+ }
129+ fmt .Printf (" Go Version: %s\n " , runtime .Version ())
130+ fmt .Printf (" OS/Arch: %s/%s\n " , runtime .GOOS , runtime .GOARCH )
131+ os .Exit (0 )
132+ }
133+ if logDebug {
134+ cli .SetPrintLevel (cli .LevelDebug )
135+ }
136+ }
137+
138+ func helpTemplate () string {
139+ return fmt .Sprintf ("%s\n Version:\n github.com/gesquive/git-user %s\n " ,
140+ RootCmd .HelpTemplate (), BuildVersion )
141+ }
142+
143+ func usageTemplate () string {
144+ return `Usage:{{if .Runnable}}
145+ {{.UseLine}}{{end}}{{if gt (len .Aliases) 0}}
146+ Aliases:
147+ {{.NameAndAliases}}{{end}}{{if .HasExample}}
148+
149+ Examples:
150+ {{.Example}}{{end}}{{if .HasAvailableSubCommands}}
151+
152+ Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
153+ {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
154+
155+ Flags:
156+ {{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
157+
158+ Global Flags:
159+ {{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
160+
161+ Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
162+ {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
163+
164+ Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
165+ `
166+ }
0 commit comments