forked from razonyang/caddy-dnspodcn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.go
86 lines (79 loc) · 1.85 KB
/
module.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
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package caddydnspodcn
import (
"clevergo.tech/dnspodcn"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
var (
_ caddyfile.Unmarshaler = (*Module)(nil)
)
func init() {
caddy.RegisterModule(&Module{})
}
// Module wraps dnspodcn.Provider.
type Module struct {
*dnspodcn.Provider
}
// CaddyModule returns the Caddy module information.
func (m *Module) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "dns.providers.dnspodcn",
New: func() caddy.Module {
return &Module{
Provider: &dnspodcn.Provider{
Client: dnspodcn.NewClient("", ""),
},
}
},
}
}
// UnmarshalCaddyfile sets up the DNS provider from Caddyfile tokens. Syntax:
//
// dnspodcn [<app_id> <app_token>] {
// app_id <app_id>
// app_token <app_token>
// }
//
func (m *Module) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
repl := caddy.NewReplacer()
for d.Next() {
if d.NextArg() {
m.AppID = repl.ReplaceAll(d.Val(), "")
}
if d.NextArg() {
m.AppToken = repl.ReplaceAll(d.Val(), "")
}
if d.NextArg() {
return d.ArgErr()
}
for nesting := d.Nesting(); d.NextBlock(nesting); {
switch d.Val() {
case "app_id":
if m.AppID != "" {
return d.Err("APP ID already set")
}
m.AppID = repl.ReplaceAll(d.Val(), "")
if d.NextArg() {
return d.ArgErr()
}
case "app_token":
if m.AppToken != "" {
return d.Err("APP token already set")
}
m.AppToken = repl.ReplaceAll(d.Val(), "")
if d.NextArg() {
return d.ArgErr()
}
default:
return d.Errf("unrecognized subdirective '%s'", d.Val())
}
}
}
if m.AppID == "" || m.AppToken == "" {
return d.Err("missing APP ID or Token")
}
return nil
}