-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
299 lines (259 loc) · 7.44 KB
/
data.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/mitchellh/go-homedir"
)
type GitProvider struct {
Name string `json:"name,omitempty"`
Url string `json:"url,omitempty"`
Properties map[string]string `json:"properties,omitempty"`
}
type GitRemote struct {
Name string
Url string
Command string
}
var defaultProviders []GitProvider = []GitProvider{
{Name: "github", Url: "https://github.com/"},
{Name: "bitbucket", Url: "https://bitbucket.org/"},
{Name: "gitlab", Url: "https://gitlab.com/"},
}
var currentProviders []GitProvider
//Check if the provider is already in the list, if not, add into the list and save to the file.
func AddProvider(providerName string, providerUrl string) {
provider, index := GetProviderByName(providerName)
//if index is -1 get provider by url
if index == -1 {
provider, index = GetProviderByUrl(providerUrl)
}
//return provider if index is not -1
if index != -1 {
currentProviders[index] = provider
return
} else {
//create a git provider with providerName and providerUrl, keep properties blank
provider := GitProvider{Name: providerName, Url: providerUrl, Properties: make(map[string]string)}
currentProviders = append(currentProviders, provider)
writeGitProviders(currentProviders)
}
}
func AddProviderPropertyFromName(providerName string, propertyKey string, propertyValue string) {
provider, index := GetProviderByName(providerName)
//throw error if index is -1
if index == -1 {
fmt.Println("Error: Provider not found. Run gitconfig-provider listProviders to get available git providers")
os.Exit(1)
}
if provider.Properties == nil || len(provider.Properties) == 0 {
provider.Properties = make(map[string]string)
}
provider.Properties[propertyKey] = propertyValue
updateProvider(provider, index)
runUpdateCommand(propertyKey, propertyValue)
}
func getRemotes() ([]GitRemote, error) {
mainCommand := "bash"
commandFlag := "-c"
if runtime.GOOS == "windows" {
mainCommand = "cmd"
commandFlag = "/c"
}
configCmd := "git remote -v"
command := exec.Command(mainCommand, commandFlag, configCmd)
var stderr bytes.Buffer
command.Stderr = &stderr
output, commandError := command.Output()
if commandError != nil {
return nil, commandError
}
remoteData := string(output)
remotes := strings.Split(remoteData, "\n")
var gitRemotes []GitRemote
for _, remote := range remotes {
if remote != "" {
name := ""
url := ""
command := ""
fmt.Sscanf(remote, "%s %s (%s)", &name, &url, &command)
gitRemotes = append(gitRemotes, GitRemote{Name: name, Url: url, Command: command})
}
}
return gitRemotes, nil
}
func runUpdateCommand(key string, value string) {
mainCommand := "bash"
commandFlag := "-c"
if runtime.GOOS == "windows" {
mainCommand = "cmd"
commandFlag = "/c"
}
//check if key has space
if strings.Contains(key, " ") {
key = "\"" + key + "\""
}
if strings.Contains(value, " ") {
value = "\"" + value + "\""
}
configCmd := fmt.Sprintf("git config %s %s", key, value)
command := exec.Command(mainCommand, commandFlag, configCmd)
var stderr bytes.Buffer
command.Stderr = &stderr
_, commandError := command.Output()
if commandError != nil {
fmt.Println(command)
fmt.Println(fmt.Sprint(commandError) + ": " + stderr.String())
}
}
func AddProviderPropertyFromUrl(providerUrl string, propertyKey string, propertyValue string) {
provider, index := GetProviderByUrl(providerUrl)
//throw error if index is -1
if index == -1 {
fmt.Println("Error: Provider not found. Run gitconfig-provider listProviders to get available git providers")
os.Exit(1)
}
if provider.Properties == nil || len(provider.Properties) == 0 {
provider.Properties = make(map[string]string)
}
provider.Properties[propertyKey] = propertyValue
updateProvider(provider, index)
runUpdateCommand(propertyKey, propertyValue)
}
func RemoveProviderProperty(providerName string, propertyKey string) {
provider, index := GetProviderByName(providerName)
//throw error if index is -1
if index == -1 {
fmt.Println("Error: Provider not found. Run gitconfig-provider listProviders to get available git providers")
os.Exit(1)
}
if provider.Properties == nil || len(provider.Properties) == 0 {
provider.Properties = make(map[string]string)
}
delete(provider.Properties, propertyKey)
updateProvider(provider, index)
runUpdateCommand(propertyKey, "")
}
func GetProviderByName(providerName string) (GitProvider, int) {
indexToReturn := -1
var providerToReturn GitProvider
if len(currentProviders) == 0 {
readGitProviders()
}
for index, provider := range currentProviders {
if strings.EqualFold(provider.Name, providerName) {
providerToReturn = provider
indexToReturn = index
break
}
}
return providerToReturn, indexToReturn
}
func GetProviderByUrl(providerUrl string) (GitProvider, int) {
indexToReturn := -1
var providerToReturn GitProvider
if len(currentProviders) == 0 {
readGitProviders()
}
for index, provider := range currentProviders {
if strings.HasPrefix(providerUrl, provider.Url) {
providerToReturn = provider
indexToReturn = index
break
}
}
return providerToReturn, indexToReturn
}
func GetProviders() ([]GitProvider, error) {
if len(currentProviders) > 0 {
return currentProviders, nil
}
return readGitProviders()
}
func ApplyPropertiesForRemote() {
remotes, err := getRemotes()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
firstRemote := remotes[0]
for _, remote := range remotes {
if remote.Name == "origin" {
firstRemote = remote
break
}
}
provider, index := GetProviderByUrl(firstRemote.Url)
//if index is -1 then no provider found
if index == -1 {
fmt.Println("Error: Provider not found")
os.Exit(1)
}
applyCommandFromProvider(provider)
}
func applyCommandFromProvider(provider GitProvider) {
for key, value := range provider.Properties {
runUpdateCommand(key, value)
}
}
func updateProvider(provider GitProvider, index int) {
currentProviders[index] = provider
writeGitProviders(currentProviders)
}
func readGitProviders() ([]GitProvider, error) {
var gitProviders []GitProvider
home, _ := homedir.Dir()
dbPath := filepath.Join(home, "gitProviders.json")
fileBytes, err := ioutil.ReadFile(dbPath)
if err != nil {
return nil, err
}
if err := json.Unmarshal(fileBytes, &gitProviders); err != nil {
return nil, err
}
currentProviders = gitProviders
return gitProviders, nil
}
func ResetProviders() {
home, _ := os.UserHomeDir()
dbPath := filepath.Join(home, "gitProviders.json")
err := os.Remove(dbPath)
if err != nil {
fmt.Println("Error in processing file:", err.Error())
os.Exit(1)
}
}
func init() {
if len(currentProviders) == 0 {
currentProviders, err := readGitProviders()
if err != nil || len(currentProviders) == 0 {
writeGitProviders(defaultProviders)
currentProviders = defaultProviders
}
}
}
func writeGitProviders(providers []GitProvider) {
home, _ := os.UserHomeDir()
dbPath := filepath.Join(home, "gitProviders.json")
defaultFilesData, err := json.Marshal(providers)
if err != nil {
fmt.Println("Error in writing to file:", err.Error())
os.Exit(1)
}
dbFile, errorData := os.OpenFile(dbPath, os.O_RDWR|os.O_CREATE, 0600)
if errorData != nil {
fmt.Println("Error in processing file:", errorData.Error())
os.Exit(1)
}
err = ioutil.WriteFile(dbFile.Name(), defaultFilesData, 0600)
if err != nil {
fmt.Println("Error in writing to file:", err.Error())
os.Exit(1)
}
}