/
step_git_validate.go
109 lines (99 loc) · 2.64 KB
/
step_git_validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package git
import (
"os"
"os/user"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/spf13/cobra"
)
// StepGitValidateOptions contains the command line flags
type StepGitValidateOptions struct {
step.StepOptions
}
var (
stepGitValidateLong = templates.LongDesc(`
This pipeline step validates that the .gitconfig is correctly configured
`)
stepGitValidateExample = templates.Examples(`
# validates the user.name & user.email values are set in the .gitconfig
jx step git validate
`)
)
// NewCmdStepGitValidate creates a command to validate gitconfig
func NewCmdStepGitValidate(commonOpts *opts.CommonOptions) *cobra.Command {
options := StepGitValidateOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "validate",
Short: "Validates the .gitconfig is correctly configured",
Long: stepGitValidateLong,
Example: stepGitValidateExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
return cmd
}
// Run validates git config
func (o *StepGitValidateOptions) Run() error {
// lets ignore errors which indicate no value set
userName, _ := o.Git().Username("")
userEmail, _ := o.Git().Email("")
var err error
if userName == "" {
// check the OS first
userName = os.Getenv("GIT_AUTHOR_NAME")
if userName == "" {
if !o.BatchMode {
userName, err = util.PickValue("Please enter the name you wish to use with git: ", "", true, "", o.GetIOFileHandles())
if err != nil {
return err
}
}
}
if userName == "" {
user, err := user.Current()
if err == nil && user != nil {
userName = user.Username
}
}
if userName == "" {
userName = util.DefaultGitUserName
}
err = o.Git().SetUsername("", userName)
if err != nil {
return err
}
}
if userEmail == "" {
// check the OS first
userEmail = os.Getenv("GIT_AUTHOR_EMAIL")
if userEmail == "" {
if !o.BatchMode {
userEmail, err = util.PickValue("Please enter the email address you wish to use with git: ", "", true, "", o.GetIOFileHandles())
if err != nil {
return err
}
}
}
if userEmail == "" {
userEmail = util.DefaultGitUserEmail
}
err = o.Git().SetEmail("", userEmail)
if err != nil {
return err
}
}
log.Logger().Infof("Git configured for user: %s and email %s", util.ColorInfo(userName), util.ColorInfo(userEmail))
return nil
}