From c05a30bbe415705786bf1c54efc1e997ceef9cf9 Mon Sep 17 00:00:00 2001 From: "honwen.chan" Date: Sun, 29 Jan 2017 00:10:47 +0800 Subject: [PATCH] Add REDO-Auto-Update --- main.go | 60 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 436b28a..c315b84 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,8 @@ import ( "regexp" + "time" + "github.com/denverdino/aliyungo/dns" "github.com/urfave/cli" ) @@ -148,43 +150,57 @@ func main() { Name: "domain, d", Usage: "specify `DomainName`. like ddns.aliyun.com", }, + cli.Int64Flag{ + Name: "redo, r", + Value: 0, + Usage: "redo Auto-Update, every N `Seconds`; Disable if N less than 10", + }, }, Action: func(c *cli.Context) error { if err := appInit(c); err != nil { return err } - // fmt.Println(c.Command.Name, "task: ", accessKey, c.String("domain")) + // fmt.Println(c.Command.Name, "task: ", accessKey, c.String("domain"), c.Int64("redo")) + redoDurtion := c.Int64("redo") rr := regexp.MustCompile(`\.[^\.]*`).ReplaceAllString(c.String("domain"), "") domain := regexp.MustCompile(`^[^\.]*\.`).ReplaceAllString(c.String("domain"), "") // fmt.Println(rr, domain) - dnsRecords, err := accessKey.list(domain) - var target *dns.RecordType - if err != nil { - fmt.Printf("%+v", err) - } else { - for i := range dnsRecords { - if dnsRecords[i].RR == rr { - target = &dnsRecords[i] - break + for { + dnsRecords, err := accessKey.list(domain) + var target *dns.RecordType + if err != nil { + fmt.Printf("%+v", err) + } else { + for i := range dnsRecords { + if dnsRecords[i].RR == rr { + target = &dnsRecords[i] + break + } } } - } - if target != nil { - ipaddr := getIP() - if ipaddr == target.Value { - fmt.Println(target.RR+`.`+target.DomainName, ipaddr) - return nil + if target != nil { + ipaddr := getIP() + if ipaddr == target.Value { + fmt.Println(target.RR+`.`+target.DomainName, ipaddr) + } else { + if err = accessKey.update(target.RecordId, target.RR, ipaddr); err != nil { + fmt.Printf("%+v", err) + } else { + fmt.Println(target.RR+`.`+target.DomainName, ipaddr) + } + } + + } else { + fmt.Println("Can't Find target") } - err = accessKey.update(target.RecordId, target.RR, ipaddr) - if err != nil { - fmt.Printf("%+v", err) + if redoDurtion > 9 { + time.Sleep(time.Duration(redoDurtion) * time.Second) } else { - fmt.Println(target.RR+`.`+target.DomainName, ipaddr) + break // Once } - } else { - fmt.Println("Can't Find target") } return nil + }, }, {