From 1bd84f900a0d20b2064d4787875a2901d81ef0f4 Mon Sep 17 00:00:00 2001 From: orvice L Date: Sat, 26 Oct 2024 13:46:06 +0800 Subject: [PATCH] code clane --- cmd/ddns/main.go | 7 +------ dns/cloudflare.go | 11 ----------- dns/dns.go | 17 +++++++++++++++++ dns/{cloudflate_test.go => dns_test.go} | 0 internal/config/config.go | 6 +++--- 5 files changed, 21 insertions(+), 20 deletions(-) delete mode 100644 dns/cloudflare.go rename dns/{cloudflate_test.go => dns_test.go} (100%) diff --git a/cmd/ddns/main.go b/cmd/ddns/main.go index 65bcd26..be32a66 100644 --- a/cmd/ddns/main.go +++ b/cmd/ddns/main.go @@ -43,12 +43,7 @@ func Init() error { notify.AddNotifier(notifier) } - switch config.GetConfig().DNSProvider { - case "aliyun": - dnsProvider = dns.NewAliyun() - default: - dnsProvider = dns.NewCloudFlare() - } + dnsProvider = dns.New(config.GetConfig()) return nil } diff --git a/dns/cloudflare.go b/dns/cloudflare.go deleted file mode 100644 index 8816434..0000000 --- a/dns/cloudflare.go +++ /dev/null @@ -1,11 +0,0 @@ -package dns - -import ( - "github.com/libdns/cloudflare" - "github.com/orvice/ddns/internal/config" -) - -func NewCloudFlare() LibDNS { - provider := cloudflare.Provider{APIToken: config.GetConfig().CFToken} - return &provider -} diff --git a/dns/dns.go b/dns/dns.go index bd1b5d7..6d9cb0d 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -2,6 +2,7 @@ package dns import ( "github.com/libdns/alidns" + "github.com/libdns/cloudflare" "github.com/libdns/libdns" "github.com/orvice/ddns/internal/config" ) @@ -12,6 +13,22 @@ type LibDNS interface { libdns.RecordSetter } +func New(conf *config.Config) LibDNS { + switch conf.DNSProvider { + case "cloudflare": + return NewCloudFlare() + case "aliyun": + return NewAliyun() + } + return nil +} + +// cloudflare +func NewCloudFlare() LibDNS { + provider := cloudflare.Provider{APIToken: config.GetConfig().CFToken} + return &provider +} + // aliyun func NewAliyun() LibDNS { provider := alidns.Provider{ diff --git a/dns/cloudflate_test.go b/dns/dns_test.go similarity index 100% rename from dns/cloudflate_test.go rename to dns/dns_test.go diff --git a/internal/config/config.go b/internal/config/config.go index 894af35..5c9eca7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -17,7 +17,7 @@ type Config struct { } var ( - config Config + config = &Config{} ) func Init() (err error) { @@ -28,7 +28,7 @@ func Init() (err error) { return } -func GetConfig() Config { +func GetConfig() *Config { return config } @@ -39,6 +39,6 @@ func LoadConfig(path string) (err error) { viper.SetConfigType("env") viper.AutomaticEnv() // viper.ReadInConfig() - err = viper.Unmarshal(&config) + err = viper.Unmarshal(config) return }