1
0

style: format output

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-04-30 22:22:02 +08:00
parent bb3b879aa5
commit c4c872c01f

66
main.go
View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"os" "os"
"strings" "strings"
"time"
"github.com/denverdino/aliyungo/dns" "github.com/denverdino/aliyungo/dns"
) )
@ -18,52 +19,53 @@ var recordType string
var recordValue string var recordValue string
func main() { func main() {
fmt.Println("Check Time: " + time.Now().Format("2006-01-02 15:04:05"))
initClient()
domainRecord := parseArgs() domainRecord := parseArgs()
flag.Parse()
updateDNSRecord(domainRecord) updateDNSRecord(domainRecord)
} }
func initClient() {
id := *flag.String("id", os.Getenv("ALI_DNS_ID"), "AliId")
key := *flag.String("key", os.Getenv("ALI_DNS_KEY"), "AliKey")
client = dns.NewClient(id, key)
}
func parseArgs() *dns.AddDomainRecordArgs { func parseArgs() *dns.AddDomainRecordArgs {
id := flag.String("id", os.Getenv("ALI_DNS_ID"), "AliId")
key := flag.String("key", os.Getenv("ALI_DNS_KEY"), "AliKey")
client = dns.NewClient(*id, *key)
domain = *flag.String("domain", os.Getenv("ALI_DNS_DOMAIN"), "Domain")
sub = *flag.String("sub", os.Getenv("ALI_DNS_SUB_DOMAIN"), "RR")
recordType = *flag.String("type", "A", "RR")
recordValue = *flag.String("value", "127.0.0.1", "Redis Server Host")
flag.Parse()
recordValue = getLocalIP()
fmt.Println("Local IP: " + recordValue)
return &dns.AddDomainRecordArgs{ return &dns.AddDomainRecordArgs{
DomainName: domain, DomainName: *flag.String("domain", os.Getenv("ALI_DNS_DOMAIN"), "Domain"),
RR: sub, RR: *flag.String("sub", os.Getenv("ALI_DNS_SUB_DOMAIN"), "RR"),
Type: recordType, Type: *flag.String("type", "A", "RR"),
Value: recordValue, Value: *flag.String("value", getLocalIP(), "Value"),
} }
} }
func updateDNSRecord(domainRecord *dns.AddDomainRecordArgs) { func updateDNSRecord(domainRecord *dns.AddDomainRecordArgs) {
fmt.Println("Local IP: " + domainRecord.Value)
record, err := listRecord(domainRecord) record, err := listRecord(domainRecord)
if err != nil { if err != nil {
panic("List Record ERR: " + err.Error()) panic("List Record ERR: " + err.Error())
} }
if record != nil { if record != nil && record.Value == domainRecord.Value {
fmt.Println("Record IP: " + record.Value) fmt.Println("Record RR: " + domainRecord.RR + " Not Change")
if record.Value != domainRecord.Value { fmt.Println("Record ID: " + record.RecordId)
_, err = updateRecord(record.RecordId, domainRecord) fmt.Println("Record Value: " + domainRecord.Value)
fmt.Println("Update Record " + domainRecord.RR + " ID " + record.RecordId + " Value " + domainRecord.Value) return
} else {
fmt.Println("Record " + domainRecord.RR + " ID " + record.RecordId + " Value " + domainRecord.Value + " Not Change")
}
} else {
_, err = addRecord(domainRecord)
fmt.Println("Add Record " + domainRecord.RR + " Value " + domainRecord.Value)
} }
if record == nil {
_, err = addRecord(domainRecord)
fmt.Println("Record RR: " + domainRecord.RR + " Added!")
} else {
_, err = updateRecord(record.RecordId, domainRecord)
fmt.Println("Record RR: " + domainRecord.RR + " Updated!")
fmt.Println("Record ID: " + record.RecordId)
}
fmt.Println("Record Value: " + domainRecord.Value)
if err != nil { if err != nil {
fmt.Println("Update Or Add Record ERR: " + err.Error()) fmt.Println("Update Or Add Record ERR: " + err.Error())
} }
@ -91,9 +93,9 @@ func listRecord(domainRecord *dns.AddDomainRecordArgs) (*dns.RecordTypeNew, erro
if err != nil { if err != nil {
return nil, err return nil, err
} }
for r := range resp.DomainRecords.Record { for _, r := range resp.DomainRecords.Record {
if resp.DomainRecords.Record[r].RR == domainRecord.RR { if r.RR == domainRecord.RR {
return &resp.DomainRecords.Record[r], nil return &r, nil
} }
} }
return nil, err return nil, err