-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
107 lines (90 loc) · 2.4 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
package main
import (
"flag"
"fmt"
"log/slog"
"os"
"time"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"github.com/spf13/viper"
"github.com/we11adam/uddns/app"
"github.com/we11adam/uddns/notifier"
"github.com/we11adam/uddns/provider"
"github.com/we11adam/uddns/updater"
_ "github.com/joho/godotenv/autoload"
_ "github.com/we11adam/uddns/notifier/telegram"
_ "github.com/we11adam/uddns/provider/ip_service"
_ "github.com/we11adam/uddns/provider/netif"
_ "github.com/we11adam/uddns/provider/routeros"
_ "github.com/we11adam/uddns/updater/aliyun"
_ "github.com/we11adam/uddns/updater/cloudflare"
_ "github.com/we11adam/uddns/updater/duckdns"
_ "github.com/we11adam/uddns/updater/lightdns"
)
func init() {
slog.SetDefault(slog.New(
tint.NewHandler(os.Stdout, &tint.Options{
NoColor: !isatty.IsTerminal(os.Stdout.Fd()),
Level: slog.LevelDebug,
TimeFormat: time.DateTime,
}),
))
}
func main() {
var configPath string
flag.StringVar(&configPath, "c", "", "Path to the configuration file")
flag.Parse()
config, err := getConfigFile(configPath)
if err != nil {
slog.Error("fatal error config file", "error", err)
os.Exit(1)
}
v := viper.New()
slog.Info("[UDDNS] using config:", "config", config)
v.SetConfigFile(config)
if err = v.ReadInConfig(); err != nil {
slog.Error("[UDDNS] failed to read config file:", "error", err)
os.Exit(1)
}
name, p, err := provider.GetProvider(v)
if err != nil {
slog.Error("[UDDNS] no provider found.")
os.Exit(1)
} else {
slog.Info("[UDDNS] provider selected:", "name", name)
}
name, u, err := updater.GetUpdater(v)
if err != nil {
slog.Error("[UDDNS] no updater found.")
os.Exit(1)
} else {
slog.Info("[UDDNS] updater selected:", "updater", name)
}
name, n := notifier.GetNotifier(v)
slog.Info("[UDDNS] notifier selected:", "notifier", name)
app.NewApp(&p, &u, &n).Run()
}
func getConfigFile(providedPath string) (string, error) {
if providedPath != "" && isReadable(providedPath) {
return providedPath, nil
}
locations := []string{
os.Getenv("UDDNS_CONFIG"),
"./uddns.yaml",
os.Getenv("HOME") + "/.config/uddns.yaml",
"/etc/uddns.yaml",
}
for _, p := range locations {
if isReadable(p) {
return p, nil
}
}
return "", fmt.Errorf("[UDDNS] no readable config file found in %v", locations)
}
func isReadable(p string) bool {
if _, err := os.Stat(p); err == nil {
return true
}
return false
}