This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (73 loc) · 1.6 KB
/
main.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
package main
import (
"fmt"
"io"
"log/slog"
"os"
"time"
"github.com/raphgl/syngit/clients"
"github.com/raphgl/syngit/config"
"github.com/raphgl/syngit/gitops"
)
const HELP_MSG = `usage: syngit <command>
Commands:
init create a configuration file
run synchronize configured repositories
daemon run syngit as a daemon, making it run every x amount of time
`
var cfg *config.Config
func synchronizeRepos() {
repos := clients.GetRepos(cfg)
err := gitops.CreateLocalMirrors(repos, cfg)
if err != nil {
slog.Error("Creating local mirrors", err)
return
}
err = gitops.SyncMirrors(repos, cfg)
if err != nil {
slog.Error("Synchronizing mirrors", err)
return
}
}
func main() {
if len(os.Args) < 2 {
fmt.Print(HELP_MSG)
return
}
// setup logging
logPath, err := config.GetLogFilePath()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0655)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer logFile.Close()
multiOut := io.MultiWriter(logFile, os.Stdout)
logger := slog.New(slog.NewTextHandler(multiOut, &slog.HandlerOptions{}))
slog.SetDefault(logger)
// load config
cfg, err = config.LoadConfig()
if err != nil {
slog.Error(err.Error())
return
}
// parse commands
switch os.Args[1] {
case "init":
fmt.Println("TODO: this functionality has not yet been implemented.")
case "run":
synchronizeRepos()
case "daemon":
for {
timer := time.NewTimer(1 * time.Hour)
synchronizeRepos()
<-timer.C
}
default:
fmt.Printf(HELP_MSG)
}
}