Skip to content

Commit fa6b0e0

Browse files
committed
update code for go1.18
1 parent 84ac440 commit fa6b0e0

File tree

8 files changed

+115
-36
lines changed

8 files changed

+115
-36
lines changed

Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM gesquive/go-builder:latest AS builder
2+
3+
ENV APP=git-user
4+
5+
# This requires that `make release-snapshot` be called first
6+
COPY dist/ /dist/
7+
RUN copy-release
8+
RUN chmod +x /app/git-user
9+
10+
# =============================================================================
11+
FROM gesquive/docker-base:latest
12+
LABEL maintainer="Gus Esquivel <gesquive@gmail.com>"
13+
14+
COPY --from=builder /app/${APP} /app/
15+
16+
ENTRYPOINT ["/app/git-user"]

cmd/list.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package cmd
22

33
import (
4+
"path/filepath"
5+
46
"github.com/gesquive/cli"
57
"github.com/gesquive/git-user/git"
68
"github.com/spf13/cobra"
7-
"path/filepath"
89
)
910

1011
var listCmd = &cobra.Command{
1112
Use: "list",
12-
Aliases: []string{"l"},
13+
Aliases: []string{"l", "ls"},
1314
Short: "List all saved profiles",
1415
Long: `List all of the saved user profiles found in your config.`,
1516
Run: listRun,

cmd/rem.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var remCmd = &cobra.Command{
1717

1818
func init() {
1919
RootCmd.AddCommand(remCmd)
20-
remCmd.PersistentFlags().BoolVarP(&global, "global", "G", false,
20+
remCmd.PersistentFlags().BoolVarP(&useGlobal, "global", "G", false,
2121
"Remove the profile from the global config")
2222
}
2323

@@ -31,7 +31,7 @@ func remRun(cmd *cobra.Command, args []string) {
3131
os.Exit(3)
3232
}
3333

34-
if global {
34+
if useGlobal {
3535
cli.Debug("Removing global user")
3636
git.RemoveGlobalUser()
3737
cli.Info("Removed user info from the global config")

cmd/root.go

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,48 @@ package cmd
33
import (
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+
1321
var userProfileConfig *git.UserProfileConfig
1422
var gitRepo *git.Repo
1523
var gitPath string
1624
var cfgFilePath string
1725
var projectPath string
18-
var displayVersion string
1926
var appName string
2027

2128
var logDebug bool
2229
var showVersion bool
2330

24-
var global bool
31+
var useGlobal bool
2532

2633
// RootCmd represents the base command when called without any subcommands
2734
var 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\nVersion:\n github.com/gesquive/%s\n",
39-
RootCmd.HelpTemplate(), displayVersion))
45+
func Execute() {
46+
RootCmd.SetHelpTemplate(helpTemplate())
47+
RootCmd.SetUsageTemplate(usageTemplate())
4048
if err := RootCmd.Execute(); err != nil {
4149
fmt.Println(err)
4250
os.Exit(-1)
@@ -56,22 +64,22 @@ func init() {
5664
user.ShortenHomeDir(defaultProjectPath), "The project to get/set the user")
5765
RootCmd.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")
5970
RootCmd.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-
6472
RootCmd.PersistentFlags().MarkHidden("debug")
73+
74+
// TODO: activate viper for ENV vars
75+
// viper.SetEnvPrefix("git-user")
76+
// viper.AutomaticEnv()
6577
}
6678

6779
func initConfig() {
6880
if logDebug {
6981
cli.SetPrintLevel(cli.LevelDebug)
7082
}
71-
if showVersion {
72-
cli.Info(displayVersion)
73-
os.Exit(0)
74-
}
7583
cli.Debug("Running with debug turned on")
7684

7785
appName = os.Args[0]
@@ -94,8 +102,8 @@ func initConfig() {
94102
cli.Debug("gitPath=%s", gitPath)
95103
cli.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))
99107
cli.Debug("configPath='%s'", cfgFilePath)
100108
cli.Debug("projectPath='%s'", projectPath)
101109
var err error
@@ -107,3 +115,52 @@ func initConfig() {
107115
cli.Debug("profileConfigPath=%s", userProfileConfig.Path())
108116
gitRepo = 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\nVersion:\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+
}

cmd/set.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package cmd
22

33
import (
4+
"os"
5+
46
"github.com/gesquive/cli"
57
"github.com/gesquive/git-user/git"
68
"github.com/spf13/cobra"
7-
"os"
89
)
910

1011
// setCmd represents the set command
@@ -19,7 +20,7 @@ var setCmd = &cobra.Command{
1920

2021
func init() {
2122
RootCmd.AddCommand(setCmd)
22-
setCmd.PersistentFlags().BoolVarP(&global, "global", "G", false,
23+
setCmd.PersistentFlags().BoolVarP(&useGlobal, "global", "G", false,
2324
"Apply the profile to the global config")
2425

2526
}
@@ -37,7 +38,7 @@ func setRun(cmd *cobra.Command, args []string) {
3738
cli.Info("You can add the profile with:")
3839
cli.Info(" '%s add %s NAME EMAIL'", appName, name)
3940
} else {
40-
if global {
41+
if useGlobal {
4142
git.SetGlobalUser(profile.User, profile.Email)
4243
cli.Info("The global user has been set to '%s <%s>'",
4344
profile.User, profile.Email)

git/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "path/filepath"
44
import "strings"
55
import "github.com/codeskyblue/go-sh"
66

7-
// Repo allows you to exectue actions on a specific git project
7+
// Repo allows you to execute actions on a specific git project
88
type Repo struct {
99
path string
1010
}

main.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package main
22

3-
import "fmt"
43
import "github.com/gesquive/git-user/cmd"
54

6-
var version = "v2.0.5"
7-
var dirty = ""
5+
var (
6+
buildVersion = "v2.0.5-dev"
7+
buildCommit = ""
8+
buildDate = ""
9+
)
810

911
func main() {
10-
displayVersion := fmt.Sprintf("git-user %s%s",
11-
version,
12-
dirty)
13-
cmd.Execute(displayVersion)
12+
cmd.BuildVersion = buildVersion
13+
cmd.BuildCommit = buildCommit
14+
cmd.BuildDate = buildDate
15+
cmd.Execute()
1416
}

user/user.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package user
22

3-
import "strings"
4-
import "os/user"
3+
import (
4+
"os/user"
5+
"strings"
6+
)
57

68
// ExpandHomeDir expands ~ in a path to the users home directory
79
func ExpandHomeDir(path string) (expandedPath string) {

0 commit comments

Comments
 (0)