-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-puller.go
176 lines (143 loc) · 3.57 KB
/
git-puller.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type GitPullCommand struct {
rootCmd *cobra.Command
debug bool
logLevel string
logger *logrus.Logger
summary [][]string
wg sync.WaitGroup
mu sync.Mutex
}
func NewGitPullCommand() *GitPullCommand {
g := &GitPullCommand{
logger: logrus.New(),
summary: [][]string{},
}
g.rootCmd = &cobra.Command{
Use: "gitpull",
Short: "Traverse directories and perform git pull",
Args: cobra.ExactArgs(1),
Run: g.run,
}
g.rootCmd.PersistentFlags().BoolVar(&g.debug, "debug", false, "Enable debug logging")
g.rootCmd.PersistentFlags().StringVar(&g.logLevel, "log-level", "error", "Logging level (options: debug, info, warning, error, fatal, panic)")
g.rootCmd.ParseFlags(os.Args)
g.setupLogger()
return g
}
func (g *GitPullCommand) setupLogger() {
g.logger.SetOutput(os.Stdout)
g.logger.SetFormatter(&logrus.TextFormatter{
DisableTimestamp: true,
})
level, err := logrus.ParseLevel(g.logLevel)
if err != nil {
fmt.Printf("Invalid log level: %v\n", err)
os.Exit(1)
}
if g.debug {
level = logrus.DebugLevel
}
g.logger.SetLevel(level)
}
func (g *GitPullCommand) run(cmd *cobra.Command, args []string) {
dir := args[0]
err := filepath.Walk(dir, g.visit)
if err != nil {
g.logger.Errorf("Error: %v", err)
}
g.wait()
g.printSummary()
}
func (g *GitPullCommand) visit(path string, info os.FileInfo, err error) error {
if err != nil {
g.logger.Errorf("Error accessing path: %v", err)
return nil
}
if info.IsDir() && info.Name() == ".git" {
repoDir := filepath.Dir(path)
g.wg.Add(1)
go g.pullRepository(repoDir)
// Skip traversing subdirectories within repositories
return filepath.SkipDir
}
return nil
}
func (g *GitPullCommand) pullRepository(dir string) {
defer g.wg.Done()
remote, status := g.getGitStatus(dir)
g.mu.Lock()
g.summary = append(g.summary, []string{dir, remote, status})
g.mu.Unlock()
// Perform git pull
g.logger.Infof("Performing git pull for repository: %s", dir)
cmd := exec.Command("git", "-C", dir, "pull")
err := cmd.Run()
if err != nil {
g.logger.Errorf("Error executing git pull: %v", err)
g.mu.Lock()
g.updateStatus(dir, "Failed")
g.mu.Unlock()
} else {
g.mu.Lock()
g.updateStatus(dir, "Success")
g.mu.Unlock()
}
}
func (g *GitPullCommand) updateStatus(dir, status string) {
for i, row := range g.summary {
if row[0] == dir {
g.summary[i][2] = status
break
}
}
}
func (g *GitPullCommand) getGitStatus(dir string) (string, string) {
cmd := exec.Command("git", "-C", dir, "remote", "-v")
output, err := cmd.Output()
if err != nil {
g.logger.Errorf("Error executing git remote: %v", err)
return "", "Unknown"
}
lines := strings.Split(string(output), "\n")
if len(lines) < 1 {
return "", "Unknown"
}
remoteLine := strings.TrimSpace(lines[0])
remoteParts := strings.Fields(remoteLine)
if len(remoteParts) != 3 {
return "", "Unknown"
}
remote := remoteParts[1]
return remote, "Pending"
}
func (g *GitPullCommand) wait() {
g.wg.Wait()
}
func (g *GitPullCommand) printSummary() {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Directory", "Remote", "Status"})
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetAutoWrapText(false)
for _, row := range g.summary {
table.Append(row)
}
table.Render()
}
func main() {
cmd := NewGitPullCommand()
if err := cmd.rootCmd.Execute(); err != nil {
os.Exit(1)
}
}