...
1
16
17 package mage
18
19 import (
20 "fmt"
21
22 "sigs.k8s.io/release-utils/command"
23 )
24
25 const (
26 gitConfigNameKey = "user.name"
27 gitConfigNameValue = "releng-ci-user"
28 gitConfigEmailKey = "user.email"
29 gitConfigEmailValue = "nobody@k8s.io"
30 )
31
32 func CheckGitConfigExists() bool {
33 userName := command.New(
34 "git",
35 "config",
36 "--global",
37 "--get",
38 gitConfigNameKey,
39 )
40
41 stream, err := userName.RunSilentSuccessOutput()
42 if err != nil || stream.OutputTrimNL() == "" {
43
44
45 return false
46 }
47
48 userEmail := command.New(
49 "git",
50 "config",
51 "--global",
52 "--get",
53 gitConfigEmailKey,
54 )
55
56 stream, err = userEmail.RunSilentSuccessOutput()
57 if err != nil || stream.OutputTrimNL() == "" {
58
59
60 return false
61 }
62
63 return true
64 }
65
66 func EnsureGitConfig() error {
67 exists := CheckGitConfigExists()
68 if exists {
69 return nil
70 }
71
72 if err := command.New(
73 "git",
74 "config",
75 "--global",
76 gitConfigNameKey,
77 gitConfigNameValue,
78 ).RunSuccess(); err != nil {
79 return fmt.Errorf("configuring git %s: %w", gitConfigNameKey, err)
80 }
81
82 if err := command.New(
83 "git",
84 "config",
85 "--global",
86 gitConfigEmailKey,
87 gitConfigEmailValue,
88 ).RunSuccess(); err != nil {
89 return fmt.Errorf("configuring git %s: %w", gitConfigEmailKey, err)
90 }
91
92 return nil
93 }
94
View as plain text