-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
112 lines (95 loc) · 1.99 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"time"
"github.com/YoRyan/dsddns/updater"
"gopkg.in/yaml.v3"
)
const (
progName = "dsddns"
sleepTime = 5 * time.Minute
)
type mode int
const (
runRepeating mode = iota
runOnce
dryRun
)
// Version contains the program version number. Set it at build time with -ldflags "-X 'main.Version=x.x.x'".
var Version string = "development"
func main() {
flag.Usage = func() {
out := flag.CommandLine.Output()
fmt.Fprintf(out, "Usage of %s: [flag] config\n", os.Args[0])
flag.PrintDefaults()
}
var opDryRun bool
flag.BoolVar(&opDryRun, "dryrun", false, "read the configuration file, but do not push any updates")
var opRunOnce bool
flag.BoolVar(&opRunOnce, "oneshot", false, "run a single update")
var showVersion bool
flag.BoolVar(&showVersion, "version", false, "show the version number")
flag.Parse()
if showVersion {
fmt.Println(Version)
return
}
var op mode
if opDryRun {
op = dryRun
} else if opRunOnce {
op = runOnce
} else {
op = runRepeating
}
logger := log.New(os.Stdout, progName+": ", log.LstdFlags)
if err := run(context.Background(), logger, op); err != nil {
logger.Fatalln(err)
os.Exit(2)
}
}
func run(ctx context.Context, logger *log.Logger, op mode) error {
path := flag.Arg(0)
if path == "" {
return errors.New("missing path to a configuration file")
}
file, err := os.Open(path)
if err != nil {
return err
}
updaters, err := loadConfig(file)
if err != nil {
return err
}
if op == dryRun {
updaters.DryRun(ctx, logger)
} else if op == runOnce {
updaters.Update(ctx, logger)
} else if op == runRepeating {
for {
updaters.Update(ctx, logger)
time.Sleep(sleepTime)
}
}
return nil
}
func loadConfig(r io.Reader) (updater.Updaters, error) {
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
var config struct {
Records updater.Updaters
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return config.Records, nil
}