diff --git a/api/api.go b/api/api.go index 14387a1..36e1c13 100644 --- a/api/api.go +++ b/api/api.go @@ -13,24 +13,24 @@ import ( ) const ( - productionUrl = "https://api.name.com/" - devUrl = "https://api.dev.name.com/" + productionURL = "https://api.name.com/" + devURL = "https://api.dev.name.com/" ) -// Contains details required to access the Name.com API. +// API Contains details required to access the Name.com API. type API struct { - baseUrl string + baseURL string username string token string } -// A Name.com DNS record. +// DNSRecord contains information about a Name.com DNS record. type DNSRecord struct { - RecordId string `json:"record_id"` + RecordID string `json:"record_id"` Name string `json:"name"` Type string `json:"type"` Content string `json:"content"` - Ttl string `json:"ttl"` + TTL string `json:"ttl"` CreateDate string `json:"create_date"` } @@ -39,21 +39,21 @@ type resultResponse struct { Message string `json:"message"` } -// Constructs a new Name.com API. If dev is true, then +// NewNameAPI constructs a new Name.com API. If dev is true, then // the API uses the development API, instead of the production API. func NewNameAPI(username, token string, dev bool) API { a := API{username: username, token: token} if dev { - a.baseUrl = devUrl + a.baseURL = devURL } else { - a.baseUrl = productionUrl + a.baseURL = productionURL } return a } -// Constructs a new Name.com API from a configuration. +// NewAPIFromConfig constructs a new Name.com API from a configuration. func NewAPIFromConfig(c Config) API { return NewNameAPI(c.Username, c.Token, c.Dev) } @@ -77,7 +77,7 @@ func (api API) performRequest(method, url string, body io.Reader) (response []by return ioutil.ReadAll(resp.Body) } -// Create a DNS record for a given domain. The name +// CreateDNSRecord creates a DNS record for a given domain. The name // field in DNSRecord is in the format [hostname].[domainname] func (api API) CreateDNSRecord(domain string, record DNSRecord) error { // We need to transform name -> hostname for JSON. @@ -85,13 +85,13 @@ func (api API) CreateDNSRecord(domain string, record DNSRecord) error { Hostname string `json:"hostname"` Type string `json:"type"` Content string `json:"content"` - Ttl string `json:"ttl"` + TTL string `json:"ttl"` } body.Hostname = record.Name body.Type = record.Type body.Content = record.Content - body.Ttl = record.Ttl + body.TTL = record.TTL b, err := json.Marshal(body) if err != nil { @@ -100,7 +100,7 @@ func (api API) CreateDNSRecord(domain string, record DNSRecord) error { resp, err := api.performRequest( "POST", - fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/create/", domain), + fmt.Sprintf("%s%s%s", api.baseURL, "api/dns/create/", domain), bytes.NewBuffer(b), ) if err != nil { @@ -122,13 +122,13 @@ func (api API) CreateDNSRecord(domain string, record DNSRecord) error { return nil } -// Deletes a DNS record for a given domain. The recordId can +// DeleteDNSRecord deletes a DNS record for a given domain. The recordID can // be retreived from GetDNSRecords. -func (api API) DeleteDNSRecord(domain, recordId string) error { +func (api API) DeleteDNSRecord(domain, recordID string) error { var body struct { - RecordId string `json:"record_id"` + RecordID string `json:"record_id"` } - body.RecordId = recordId + body.RecordID = recordID b, err := json.Marshal(body) if err != nil { @@ -137,7 +137,7 @@ func (api API) DeleteDNSRecord(domain, recordId string) error { resp, err := api.performRequest( "POST", - fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/delete/", domain), + fmt.Sprintf("%s%s%s", api.baseURL, "api/dns/delete/", domain), bytes.NewBuffer(b), ) if err != nil { @@ -159,11 +159,11 @@ func (api API) DeleteDNSRecord(domain, recordId string) error { return nil } -// Returns a slice of DNS records associated with a given domain. +// GetDNSRecords returns a slice of DNS records associated with a given domain. func (api API) GetDNSRecords(domain string) (records []DNSRecord, err error) { resp, err := api.performRequest( "GET", - fmt.Sprintf("%s%s%s", api.baseUrl, "api/dns/list/", domain), + fmt.Sprintf("%s%s%s", api.baseURL, "api/dns/list/", domain), nil, ) diff --git a/api/config.go b/api/config.go index d2e3905..b2133b8 100644 --- a/api/config.go +++ b/api/config.go @@ -5,11 +5,10 @@ import ( "io/ioutil" ) -// Each configuration should represent a specifc domain. -// A domain can have multiple hostnames, and the hostnames -// that should be automaticall updated are in Hostnames. -// If the root domain is to be managed, an empty hostname -// should be added to the Hostnames slice. +// Config represents the configuration for a +// specific domain. Each domain can have multiple +// hostnames, including the root domain, where +// hostname is an empty string. // // The interval is the polling time (in seconds) for // daemon mode. @@ -22,7 +21,7 @@ type Config struct { Username string `json:"username"` } -// Loads configurations from a file. The configuration +// LoadConfigs loads configurations from a file. The configuration // is stored as an array of JSON serialized Config structs. func LoadConfigs(path string) ([]Config, error) { var configs struct { diff --git a/api/config_test.go b/api/config_test.go index eac3a28..50354d0 100644 --- a/api/config_test.go +++ b/api/config_test.go @@ -9,7 +9,7 @@ var expectedConfigs []Config func init() { expectedConfigs = []Config{ - Config{ + { Username: "dev-account", Token: "asdasdasdasdasdad", Interval: 60, @@ -17,7 +17,7 @@ func init() { Domain: "test.com", Hostnames: []string{"mail", "chat"}, }, - Config{ + { Username: "production-account", Token: "123123123123", Interval: 3600, diff --git a/dyndns/daemon.go b/dyndns/daemon.go index 95dfdd1..e1b727f 100644 --- a/dyndns/daemon.go +++ b/dyndns/daemon.go @@ -26,9 +26,9 @@ func contains(c api.Config, val string) bool { return false } -func updateDNSRecord(a api.API, domain, recordId string, newRecord api.DNSRecord) error { +func updateDNSRecord(a api.API, domain, recordID string, newRecord api.DNSRecord) error { log.Logger.Printf("Deleting DNS record for %s.\n", newRecord.Name) - err := a.DeleteDNSRecord(domain, newRecord.RecordId) + err := a.DeleteDNSRecord(domain, newRecord.RecordID) if err != nil { return err } @@ -90,11 +90,11 @@ func runConfig(c api.Config, daemon bool) { log.Logger.Printf("Running update check for %s.", r.Name) if r.Content != ip { r.Content = ip - err = updateDNSRecord(a, c.Domain, r.RecordId, r) + err = updateDNSRecord(a, c.Domain, r.RecordID, r) if err != nil { - log.Logger.Printf("Failed to update record %s [%s] with IP: %s\n\t%s\n", r.RecordId, r.Name, ip, err) + log.Logger.Printf("Failed to update record %s [%s] with IP: %s\n\t%s\n", r.RecordID, r.Name, ip, err) } else { - log.Logger.Printf("Updated record %s [%s] with IP: %s\n", r.RecordId, r.Name, ip) + log.Logger.Printf("Updated record %s [%s] with IP: %s\n", r.RecordID, r.Name, ip) } } } @@ -110,10 +110,13 @@ func runConfig(c api.Config, daemon bool) { } } -// For each domain, check if the host record matches -// the current external IP. If it does not, it updates. -// If daemon is true, then Run will run forever, polling at -// an interval specified in each config. +// Run will process each configuration in configs. +// If daemon is true, then Run will run forever, +// processing each configuration at its specified +// interval. +// +// Each configuration represents a domain with +// multiple hostnames. func Run(configs []api.Config, daemon bool) { for _, config := range configs { wg.Add(1) diff --git a/dyndns/external.go b/dyndns/external.go index e9ac53f..56dda14 100644 --- a/dyndns/external.go +++ b/dyndns/external.go @@ -28,7 +28,7 @@ func tryMirror(url string) (string, error) { return string(contents), nil } -// Retrieves the external facing IP Address. +// GetExternalIP retrieves the external facing IP Address. // If multiple mirrors are provided in Urls, // it will try each one (in order), should // preceding mirrors fail. diff --git a/log/log.go b/log/log.go index bdb4a14..fc8b3e4 100644 --- a/log/log.go +++ b/log/log.go @@ -9,7 +9,7 @@ import ( // Global logger. var Logger *log.Logger -// Initialize the logger with a specific io.Writer. +// Init intializes the logger with a specific io.Writer. // This function is generally called near startup. func Init(writer io.Writer) { Logger = log.New(writer, "", log.Ldate|log.Ltime)