-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
262 lines (253 loc) · 8.57 KB
/
main.go
File metadata and controls
262 lines (253 loc) · 8.57 KB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2018,2019 Marcus Soll
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path"
"github.com/Top-Ranger/gogitui/helper"
)
func main() {
defer func() {
r := recover()
if r != nil {
helper.ShowError(fmt.Sprint(r))
log.Println(r)
os.Exit(1)
}
}()
fmt.Println("== gogitui ==")
fmt.Println("A simple git ui managing multiple repositories")
fmt.Println("Copyright 2018,2019 Marcus Soll")
fmt.Println("License: Apache 2.0")
fmt.Println()
config, err := helper.LoadConfig()
if err != nil {
panic(err)
}
exit := false
for !exit {
option, _ := helper.Menu("Select action", []string{"git pull", "git push", "git status", "git difftool", "git commit", "Add repository", "Remove repository", "Exit"})
fmt.Println(option)
switch option {
case "git pull":
targets, _ := helper.Checklist("Select repositories for pull:", config.Repositories, "on")
if len(targets) == 0 {
break
}
handle, _ := helper.CreateProgressbar("git pull", len(targets))
for i := range targets {
helper.SetProgressbarValue(handle, i)
helper.SetProgressbarHeader(handle, targets[i])
gitpull := exec.Command("/usr/bin/git", "pull")
gitpull.Dir = targets[i]
output, err := gitpull.CombinedOutput()
fmt.Println(string(output))
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git pull at", targets[i], ":\n", string(output), "\n\nError:", err))
}
}
helper.CloseProgressbar(handle)
case "git push":
targets, _ := helper.Checklist("Select repositories for push:", config.Repositories, "on")
if len(targets) == 0 {
break
}
handle, _ := helper.CreateProgressbar("git push", len(targets))
for i := range targets {
helper.SetProgressbarValue(handle, i)
helper.SetProgressbarHeader(handle, targets[i])
gitpush := exec.Command("/usr/bin/git", "push")
gitpush.Dir = targets[i]
output, err := gitpush.CombinedOutput()
fmt.Println(string(output))
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git push at", targets[i], ":\n", string(output), "\n\nError:", err))
}
}
helper.CloseProgressbar(handle)
case "git status":
targets, _ := helper.Checklist("Select repositories for push:", config.Repositories, "off")
if len(targets) == 0 {
break
}
for i := range targets {
gitstatus := exec.Command("/usr/bin/git", "status")
gitstatus.Dir = targets[i]
output, err := gitstatus.CombinedOutput()
fmt.Println(string(output))
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git status at", targets[i], ":\n", string(output), "\n\nError:", err))
}
helper.ShowMessage(fmt.Sprint("Repository ", targets[i], ":\n\n", string(output)))
}
case "git difftool":
targets, _ := helper.Checklist("Select repositories for push:", config.Repositories, "off")
if len(targets) == 0 {
break
}
for i := range targets {
gitdifftool := exec.Command("/usr/bin/git", "difftool", "--dir-diff")
gitdifftool.Dir = targets[i]
output, err := gitdifftool.CombinedOutput()
fmt.Println(string(output))
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git difftool at", targets[i], ":\n", string(output), "\n\nError:", err))
}
if len(output) == 0 {
helper.ShowMessage(fmt.Sprintln("No difference for", targets[i]))
}
}
case "git commit":
targets, _ := helper.Checklist("Select repositories for commit:", config.Repositories, "off")
if len(targets) == 0 {
break
}
for i := range targets {
// Test if there are changes
gitstatus := exec.Command("/usr/bin/git", "status", "--short")
gitstatus.Dir = targets[i]
output, err := gitstatus.CombinedOutput()
fmt.Println(string(output))
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git status at", targets[i], ":\n", string(output), "\n\nError:", err))
}
if len(output) == 0 {
helper.ShowMessage(fmt.Sprintln("Nothing to commit for", targets[i]))
continue
}
gitstatus = exec.Command("/usr/bin/git", "status")
gitstatus.Dir = targets[i]
outputStatus, err := gitstatus.CombinedOutput()
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git status at", targets[i], ":\n", string(outputStatus), "\n\nError:", err))
}
commitExit := false
for !commitExit {
operator, _ := helper.Menu(fmt.Sprintln("Repository:", targets[i], "\n\n", string(outputStatus)), []string{"git commit -a", "git add -A && git commit", "git difftool", "Do nothing"})
switch operator {
case "git commit -a":
message, _ := helper.TextInput(fmt.Sprintln("Commit message for", targets[i]))
if message == "" {
helper.ShowMessage("Aborting due to empty message")
break
}
gitcommand := exec.Command("/usr/bin/git", "commit", "-a", "--message", message)
gitcommand.Dir = targets[i]
output, err := gitcommand.CombinedOutput()
fmt.Println(string(output))
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git commit -a at", targets[i], ":\n", string(output), "\n\nError:", err))
break
}
commitExit = true
case "git add -A && git commit":
gitcommand := exec.Command("/usr/bin/git", "add", "-A")
gitcommand.Dir = targets[i]
output, err := gitcommand.CombinedOutput()
fmt.Println(string(output))
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git add -A at", targets[i], ":\n", string(output), "\n\nError:", err))
break
}
message, _ := helper.TextInput(fmt.Sprintln("Commit message for", targets[i]))
if message == "" {
helper.ShowMessage("Aborting due to empty message")
break
}
gitcommand = exec.Command("/usr/bin/git", "commit", "--message", message)
gitcommand.Dir = targets[i]
output, err = gitcommand.CombinedOutput()
fmt.Println(string(output))
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git commit at", targets[i], ":\n", string(output), "\n\nError:", err))
break
}
commitExit = true
case "git difftool":
gitdifftool := exec.Command("/usr/bin/git", "difftool", "--dir-diff")
gitdifftool.Dir = targets[i]
output, err := gitdifftool.CombinedOutput()
fmt.Println(string(output))
if err != nil {
helper.ShowError(fmt.Sprintln("Error while git difftool at", targets[i], ":\n", string(output), "\n\nError:", err))
}
if len(output) == 0 {
helper.ShowMessage(fmt.Sprintln("No difference for", targets[i]))
}
case "Do nothing":
fallthrough
case "":
commitExit = true
break
default:
helper.ShowError(fmt.Sprintln("Unknown operator:", operator))
}
}
}
case "Add repository":
repository, _ := helper.GetDir()
if repository == "" {
break
}
_, err := os.Stat(path.Join(repository, ".git"))
if err != nil {
helper.ShowError(fmt.Sprintln("Folder", repository, "does not contain a git repository."))
break
}
alreadyAdded := false
for i := range config.Repositories {
if config.Repositories[i] == repository {
alreadyAdded = true
break
}
}
if alreadyAdded {
helper.ShowMessage(fmt.Sprintln("Repository", repository, "already added."))
} else {
config.Repositories = append(config.Repositories, repository)
err := config.SaveConfig()
if err != nil {
panic(err)
}
}
case "Remove repository":
if len(config.Repositories) == 0 {
helper.ShowMessage("No repositories are currently registered.")
break
}
target, _ := helper.Menu("Select repository for removal:", config.Repositories)
if target == "" {
break
}
// Assume each repository is only once registered
for i := range config.Repositories {
if config.Repositories[i] == target {
config.Repositories = append(config.Repositories[:i], config.Repositories[i+1:]...)
config.SaveConfig()
break
}
}
case "Exit":
fallthrough
case "":
exit = true
default:
panic(fmt.Sprintln("Unknown option:\n", option))
}
}
}