-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
391 lines (338 loc) · 9.65 KB
/
main.go
File metadata and controls
391 lines (338 loc) · 9.65 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"bufio"
"context"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"text/tabwriter"
"time"
"gitgogit/config"
"gitgogit/daemon"
glog "gitgogit/log"
)
var version = "dev"
func main() {
// The --daemon-child sentinel is checked before subcommand dispatch so it
// never appears in help output or shell completions.
if len(os.Args) >= 2 && os.Args[1] == "--daemon-child" {
runDaemonChild(os.Args[2:])
return
}
if len(os.Args) < 2 {
printUsage()
os.Exit(1)
}
cmd := os.Args[1]
args := os.Args[2:]
switch cmd {
case "version", "--version":
fmt.Println(version)
case "sync":
runSync(args)
case "start":
runStart(args)
case "stop":
runStop(args)
case "status":
runStatus(args)
case "list":
runList(args)
case "add":
runAdd(args)
default:
fmt.Fprintf(os.Stderr, "unknown command %q\n\n", cmd)
printUsage()
os.Exit(1)
}
}
func printUsage() {
fmt.Fprintf(os.Stderr, `gitgogit - Git repository mirroring daemon
Usage:
gitgogit <command> [flags]
Commands:
start Start the background daemon
stop Stop the running daemon
status Show daemon status
sync Perform a one-shot mirror sync and exit
list List configured repos
add Interactively add a repo to the config
Flags:
--config Path to config file (default: ~/.config/gitgogit/config.yaml)
--interval Poll interval override (e.g. 30s, 5m)
--log-level Log level: debug, info, warn, error
--repo Sync only this repo (by name)
`)
}
func runStart(args []string) {
fs := flag.NewFlagSet("start", flag.ExitOnError)
configPath := fs.String("config", config.DefaultConfigPath(), "path to config file")
fs.Parse(args)
cfg, err := loadConfig(*configPath, config.CLIOverrides{})
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
pidPath := config.DefaultPIDPath()
if _, running, _ := daemon.IsRunning(pidPath); running {
pid, _, _ := daemon.IsRunning(pidPath)
fmt.Fprintf(os.Stderr, "daemon is already running (pid %d)\n", pid)
os.Exit(1)
}
logPath := cfg.Daemon.LogFile
if logPath == "" {
logPath = config.DefaultLogPath()
}
exe, err := os.Executable()
if err != nil {
fmt.Fprintf(os.Stderr, "find executable: %v\n", err)
os.Exit(1)
}
cmd := exec.Command(exe, "--daemon-child", "--config", *configPath)
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
if err := cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "start daemon: %v\n", err)
os.Exit(1)
}
fmt.Printf("daemon started (pid %d)\n", cmd.Process.Pid)
fmt.Printf("logging to %s\n", logPath)
}
func runDaemonChild(args []string) {
fs := flag.NewFlagSet("daemon-child", flag.ExitOnError)
configPath := fs.String("config", config.DefaultConfigPath(), "")
fs.Parse(args)
cfg, err := loadConfig(*configPath, config.CLIOverrides{})
if err != nil {
fmt.Fprintf(os.Stderr, "config error: %v\n", err)
os.Exit(1)
}
logPath := cfg.Daemon.LogFile
if logPath == "" {
logPath = config.DefaultLogPath()
}
logger, err := glog.Setup(cfg.Daemon.LogLevel, logPath, io.Discard)
if err != nil {
fmt.Fprintf(os.Stderr, "log setup: %v\n", err)
os.Exit(1)
}
pidPath := config.DefaultPIDPath()
if err := daemon.WritePID(pidPath); err != nil {
logger.Error("write pid file", slog.String("err", err.Error()))
os.Exit(1)
}
defer daemon.RemovePID(pidPath)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer stop()
logger.Info("daemon started", slog.Int("pid", os.Getpid()), slog.String("config", *configPath))
daemon.New(cfg, logger).Run(ctx, *configPath)
logger.Info("daemon stopped")
}
func runStop(args []string) {
fs := flag.NewFlagSet("stop", flag.ExitOnError)
fs.Parse(args)
pidPath := config.DefaultPIDPath()
pid, running, err := daemon.IsRunning(pidPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if !running {
fmt.Fprintln(os.Stderr, "daemon is not running")
os.Exit(1)
}
if err := syscall.Kill(pid, syscall.SIGTERM); err != nil {
fmt.Fprintf(os.Stderr, "send SIGTERM: %v\n", err)
os.Exit(1)
}
fmt.Printf("sent SIGTERM to daemon (pid %d)\n", pid)
}
func runStatus(args []string) {
fs := flag.NewFlagSet("status", flag.ExitOnError)
fs.Parse(args)
pidPath := config.DefaultPIDPath()
pid, running, err := daemon.IsRunning(pidPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if !running {
fmt.Println("not running")
return
}
started := "unknown"
if info, err := os.Stat(pidPath); err == nil {
started = info.ModTime().Format(time.RFC3339)
}
fmt.Printf("running (pid %d, started %s)\n", pid, started)
}
func runSync(args []string) {
fs := flag.NewFlagSet("sync", flag.ExitOnError)
configPath := fs.String("config", config.DefaultConfigPath(), "path to config file")
interval := fs.String("interval", "", "poll interval override")
logLevel := fs.String("log-level", "", "log level override")
repo := fs.String("repo", "", "sync only this repo by name")
fs.Parse(args)
cfg, err := loadConfig(*configPath, config.CLIOverrides{
Interval: *interval,
LogLevel: *logLevel,
Repo: *repo,
})
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
logger, err := glog.Setup(cfg.Daemon.LogLevel, cfg.Daemon.LogFile, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "log setup: %v\n", err)
os.Exit(1)
}
ctx := context.Background()
exitCode := 0
d := daemon.New(cfg, logger)
for _, r := range cfg.Repos {
if *repo != "" && r.Name != *repo {
continue
}
results := d.SyncRepo(ctx, r)
for _, res := range results {
if res.Err != nil {
logger.Error("sync failed",
slog.String("repo", res.Repo),
slog.String("mirror", res.MirrorURL),
slog.String("err", res.Err.Error()),
)
exitCode = 1
} else {
logger.Info("synced",
slog.String("repo", res.Repo),
slog.String("mirror", res.MirrorURL),
)
}
}
}
os.Exit(exitCode)
}
func runList(args []string) {
fs := flag.NewFlagSet("list", flag.ExitOnError)
configPath := fs.String("config", config.DefaultConfigPath(), "path to config file")
fs.Parse(args)
cfg, err := config.Load(*configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if len(cfg.Repos) == 0 {
fmt.Println("no repos configured")
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "NAME\tSOURCE\tMIRRORS")
for _, r := range cfg.Repos {
mirrorURLs := make([]string, len(r.Mirrors))
for i, m := range r.Mirrors {
mirrorURLs[i] = m.URL
}
fmt.Fprintf(w, "%s\t%s\t%s\n", r.Name, r.Source.URL, strings.Join(mirrorURLs, ", "))
}
w.Flush()
}
func runAdd(args []string) {
fs := flag.NewFlagSet("add", flag.ExitOnError)
configPath := fs.String("config", config.DefaultConfigPath(), "path to config file")
fs.Parse(args)
// Load existing config if present; otherwise start with an empty one.
cfg, err := config.Load(*configPath)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
fmt.Fprintf(os.Stderr, "warning: could not load config: %v\n", err)
fmt.Fprint(os.Stderr, "proceed with an empty config? [y/N] ")
line, _ := bufio.NewReader(os.Stdin).ReadString('\n')
if strings.TrimSpace(strings.ToLower(line)) != "y" {
os.Exit(1)
}
}
fmt.Fprintf(os.Stderr, "warning: problem loading config: %v\n", err)
fmt.Fprintf(os.Stderr, "proceeding with creating new empty config file at: %s\n", *configPath)
cfg = &config.Config{}
}
reader := bufio.NewReader(os.Stdin)
prompt := func(label string) string {
fmt.Printf("%s: ", label)
line, _ := reader.ReadString('\n')
return strings.TrimSpace(line)
}
name := prompt("Repo name")
if name == "" {
fmt.Fprintln(os.Stderr, "error: name is required")
os.Exit(1)
}
sourceURL := prompt("Source URL")
if sourceURL == "" {
fmt.Fprintln(os.Stderr, "error: source URL is required")
os.Exit(1)
}
sourceAuth := promptAuth(reader, "source")
var mirrors []config.MirrorTarget
for {
mirrorURL := prompt("Mirror URL (leave blank to finish)")
if mirrorURL == "" {
break
}
mirrorAuth := promptAuth(reader, "mirror")
mirrors = append(mirrors, config.MirrorTarget{URL: mirrorURL, Auth: mirrorAuth})
}
if len(mirrors) == 0 {
fmt.Fprintln(os.Stderr, "error: at least one mirror is required")
os.Exit(1)
}
cfg.Repos = append(cfg.Repos, config.RepoConfig{
Name: name,
Source: config.SourceConfig{URL: sourceURL, Auth: sourceAuth},
Mirrors: mirrors,
})
if err := config.Save(*configPath, cfg); err != nil {
fmt.Fprintf(os.Stderr, "error saving config: %v\n", err)
os.Exit(1)
}
fmt.Printf("added repo %q to %s\n", name, *configPath)
}
// promptAuth interactively asks for auth details and returns a config.AuthConfig.
func promptAuth(reader *bufio.Reader, context string) config.AuthConfig {
prompt := func(label string) string {
fmt.Printf("%s: ", label)
line, _ := reader.ReadString('\n')
return strings.TrimSpace(line)
}
authType := prompt(fmt.Sprintf("%s auth type (ssh/token, leave blank for none)", context))
switch authType {
case "ssh":
key := prompt("SSH key path")
return config.AuthConfig{Type: "ssh", Key: key}
case "token":
env := prompt("Token env var name")
return config.AuthConfig{Type: "token", Env: env}
default:
return config.AuthConfig{}
}
}
// loadConfig loads the config file and merges CLI overrides.
func loadConfig(path string, overrides config.CLIOverrides) (*config.Config, error) {
cfg, err := config.Load(path)
if err != nil {
return nil, fmt.Errorf("load config: %w", err)
}
if err := cfg.Merge(overrides); err != nil {
return nil, fmt.Errorf("apply overrides: %w", err)
}
if err := cfg.Validate(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
return cfg, nil
}