-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
configuration.go
134 lines (111 loc) · 3.46 KB
/
configuration.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package duet
import (
"bytes"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"time"
)
// Configuration represents package configuration (shared by commands)
type Configuration struct {
Namespace string
PairsFile string
EmailLookup string
CoAuthoredBy bool
Global bool
RotateAuthor bool
SetGitUserConfig bool
StaleCutoff time.Duration
IsCurrentWorkingDirGitRepo bool
DefaultUpdate bool
AllowMultipleCommitters bool
}
// NewConfiguration initializes Configuration from the environment
// Returns an error if it cannot parse the staleness timeout as an integer or
// the global var as a bool
func NewConfiguration() (config *Configuration, err error) {
config = &Configuration{
Namespace: getenvDefault("GIT_DUET_CONFIG_NAMESPACE", "duet.env"),
EmailLookup: os.Getenv("GIT_DUET_EMAIL_LOOKUP_COMMAND"),
}
if config.PairsFile, err = getPairsFile(); err != nil {
return nil, err
}
cutoff, err := strconv.Atoi(getenvDefault("GIT_DUET_SECONDS_AGO_STALE", "1200"))
if err != nil {
return nil, err
}
if config.Global, err = strconv.ParseBool(getenvDefault("GIT_DUET_GLOBAL", "0")); err != nil {
return nil, err
}
if config.RotateAuthor, err = strconv.ParseBool(getenvDefault("GIT_DUET_ROTATE_AUTHOR", "0")); err != nil {
return nil, err
}
if config.CoAuthoredBy, err = strconv.ParseBool(getenvDefault("GIT_DUET_CO_AUTHORED_BY", "0")); err != nil {
return nil, err
}
if config.DefaultUpdate, err = strconv.ParseBool(getenvDefault("GIT_DUET_DEFAULT_UPDATE", "0")); err != nil {
return nil, err
}
if config.AllowMultipleCommitters, err = strconv.ParseBool(getenvDefault("GIT_DUET_ALLOW_MULTIPLE_COMMITTERS", "0")); err != nil {
return nil, err
}
defaultSetGitUserConfig := "0"
if config.CoAuthoredBy {
defaultSetGitUserConfig = "1"
}
if config.SetGitUserConfig, err = strconv.ParseBool(getenvDefault("GIT_DUET_SET_GIT_USER_CONFIG", defaultSetGitUserConfig)); err != nil {
return nil, err
}
config.StaleCutoff = time.Duration(cutoff) * time.Second
config.IsCurrentWorkingDirGitRepo, err = checkCwdGitDir()
return config, nil
}
func getPairsFile() (value string, err error) {
authorsFile := ".git-authors"
defaultAuthorsFile := path.Join(os.Getenv("HOME"), authorsFile)
if os.Getenv("GIT_DUET_AUTHORS_FILE") != "" {
return os.Getenv("GIT_DUET_AUTHORS_FILE"), nil
}
gitDirectory, err := exec.Command("git", "rev-parse", "--show-toplevel").CombinedOutput()
if err != nil {
if bytes.Contains(gitDirectory, []byte("Not a git repository")) ||
bytes.Contains(gitDirectory, []byte("not a git repository")) {
return defaultAuthorsFile, nil
}
return "", err
}
gitDirectoryAuthors := path.Join(strings.TrimSpace(string(gitDirectory)), authorsFile)
if _, err := os.Stat(gitDirectoryAuthors); err == nil {
return gitDirectoryAuthors, nil
}
return defaultAuthorsFile, nil
}
func getenvDefault(key, defaultValue string) (value string) {
value = os.Getenv(key)
if value == "" {
value = defaultValue
}
return value
}
func checkCwdGitDir() (hasGitDir bool, err error) {
cwd, err := os.Getwd()
if err != nil {
return false, err
}
return checkGitDir(cwd), nil
}
func checkGitDir(path string) (hasGitDir bool) {
if _, err := os.Stat(path + "/.git"); os.IsNotExist(err) {
parent := filepath.Dir(path)
if parent == "/" {
return false
} else {
return checkGitDir(parent)
}
}
return true
}