From 229c0f58fa739b1858c276be583088e524c981b2 Mon Sep 17 00:00:00 2001 From: Eric Mesa Date: Thu, 23 Feb 2023 23:33:16 -0500 Subject: [PATCH 1/7] Removed writing API key to command line Closes #5 --- main.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 90abeb4..a00786b 100644 --- a/main.go +++ b/main.go @@ -128,17 +128,18 @@ func main() { if err != nil { log.Fatal(err) } - fmt.Printf("settings.jon should be at the following path: %s\n", configFilePath) + fmt.Printf("Looking for settings.jon should be at the following path: %s\n", configFilePath) newIPAddress := getHostIpAddress() settingsJson, err := os.Open(configFilePath) // if os.Open returns an error then handle it if err != nil { - fmt.Println(err) - os.Exit(1) + fmt.Println("Unable to open the confix file. Did you place it in the right spot?") + log.Fatal(err) } defer func(settingsJson *os.File) { err := settingsJson.Close() if err != nil { + log.Printf("Couldn't close the settings file. Error: %s", err) } }(settingsJson) @@ -146,17 +147,17 @@ func main() { var settings *credentials err = json.Unmarshal(byteValue, &settings) if err != nil { - fmt.Printf("could not unmarshal json: %s\n", err) + fmt.Println("Check that you do not have errors in your JSON file.") + log.Fatalf("could not unmarshal json: %s\n", err) return } fmt.Printf("IP address outside the NAT is: %s\n", newIPAddress) - fmt.Printf("Dreamhost API key is: %s\n", settings.ApiKey) fmt.Printf("Domains to update are: %s\n", settings.Domains) dnsRecords := getDNSRecords(settings.ApiKey) var records dnsRecordsJSON err = json.Unmarshal([]byte(dnsRecords), &records) if err != nil { - fmt.Printf("err is: %s\n", err) + log.Fatalf("Unable to unmarshall JSON from Dreamhost. err is: %s\n", err) } var updatedDomains []string From 6bd7ad6f5fd4ad0adf918ca5b3257df32a8f934e Mon Sep 17 00:00:00 2001 From: Eric Mesa Date: Fri, 24 Feb 2023 08:32:56 -0500 Subject: [PATCH 2/7] lowercase struct for idiomatic Go --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index a00786b..d6cb311 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ type dnsRecordsJSON struct { Data []map[string]string `json:"data"` } -type UrlIPPair struct { +type urlIPPair struct { url string ipAddress string } From 0b5be938b50a86441efe4fa9d712f4786ec25506 Mon Sep 17 00:00:00 2001 From: Eric Mesa Date: Fri, 24 Feb 2023 08:37:23 -0500 Subject: [PATCH 3/7] Created better consistency between log messages and CLI messages for issue #6 --- main.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index d6cb311..6c15ab5 100644 --- a/main.go +++ b/main.go @@ -42,15 +42,15 @@ type commandResult struct { func webGet(url string) string { response, err := http.Get(url) if err != nil { - log.Fatal(err) + log.Println(err) } result, err := io.ReadAll(response.Body) response.Body.Close() if response.StatusCode > 299 { - log.Fatalf("Response failed with status code: %d and \nbody: %s\n", response.StatusCode, result) + log.Printf("Response failed with status code: %d and \nbody: %s\n", response.StatusCode, result) } if err != nil { - log.Fatal(err) + log.Println(err) } return string(result) } @@ -99,7 +99,7 @@ func addDNSRecord(domain string, newIPAddress string, apiKey string) string { if err != nil { fmt.Printf("Error: %s\n", err) } - fmt.Printf("Result of trying to add DNS record for %s is %s\n", domain, result.Data) + log.Printf("Result of trying to add DNS record for %s is %s\n", domain, result.Data) return result.Data } @@ -110,9 +110,9 @@ func deleteDNSRecord(domain string, newIPAddress string, apiKey string) string { var result commandResult err := json.Unmarshal([]byte(response), &result) if err != nil { - fmt.Printf("Error: %s\n", err) + log.Printf("Error: %s\n", err) } - fmt.Printf("Result of trying to delete DNS record for %s is %s\n", domain, result.Data) + log.Printf("Result of trying to delete DNS record for %s is %s\n", domain, result.Data) return result.Data } @@ -163,7 +163,7 @@ func main() { var updatedDomains []string for _, url := range records.Data { if contains(settings.Domains, url["record"]) { - currentDomain := UrlIPPair{url: url["record"], ipAddress: url["value"]} + currentDomain := urlIPPair{url: url["record"], ipAddress: url["value"]} updatedDomains = append(updatedDomains, currentDomain.url) if currentDomain.ipAddress != newIPAddress { log.Printf("%s has an old IP of %s. Will attempt to change to %s", currentDomain.url, currentDomain.ipAddress, newIPAddress) From 3a0158ce99a0e3e32b5ac3387ed5d093b52d96eb Mon Sep 17 00:00:00 2001 From: Eric Mesa Date: Fri, 24 Feb 2023 08:40:54 -0500 Subject: [PATCH 4/7] added flag for verbose output --- main.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 6c15ab5..3a17b25 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,8 @@ import ( "log" "net/http" "net/url" - "os" + "os + "flag" "github.com/adrg/xdg" ) @@ -124,6 +125,11 @@ func updateDNSRecord(domain string, currentIP string, newIPAddress string, apiKe } func main() { + + // parse CLI flags + verbose := flag.Bool("-v", false, "prints log output to the commandline.") + flag.Parse() + configFilePath, err := xdg.ConfigFile("dreamhostdns/settings.json") if err != nil { log.Fatal(err) From 0713adaa4a48bca9b14beb068f46a8200fa6b20a Mon Sep 17 00:00:00 2001 From: Eric Mesa Date: Fri, 24 Feb 2023 08:46:42 -0500 Subject: [PATCH 5/7] fix import typo --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 3a17b25..fc25b41 100644 --- a/main.go +++ b/main.go @@ -2,14 +2,14 @@ package main import ( "encoding/json" + "flag" "fmt" "io" "io/ioutil" "log" "net/http" "net/url" - "os - "flag" + "os" "github.com/adrg/xdg" ) From d56da735b649d71af9eca28e7deba4a45c691fba Mon Sep 17 00:00:00 2001 From: Eric Mesa Date: Fri, 24 Feb 2023 20:42:43 -0500 Subject: [PATCH 6/7] set up the file logger --- main.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index fc25b41..fee00fc 100644 --- a/main.go +++ b/main.go @@ -126,13 +126,27 @@ func updateDNSRecord(domain string, currentIP string, newIPAddress string, apiKe func main() { + logFilePath, _ := xdg.DataFile("dreamhostdns/dnsupdates.log") + // once you figure out how to import https://github.com/natefinch/lumberjack/tree/v2.0 , use that + logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0775) + if err != nil { + log.Printf("Error %s\n", err) + } + fileLogger := log.New(logFile, "", log.LstdFlags) + // parse CLI flags - verbose := flag.Bool("-v", false, "prints log output to the commandline.") + verbose := flag.Bool("v", false, "prints log output to the commandline.") flag.Parse() + // user wants verbose logs + if *verbose { + fmt.Println("User chose verbose CLI output!") // this is just a placeholder + } + configFilePath, err := xdg.ConfigFile("dreamhostdns/settings.json") if err != nil { log.Fatal(err) + fileLogger.Println(err) } fmt.Printf("Looking for settings.jon should be at the following path: %s\n", configFilePath) newIPAddress := getHostIpAddress() @@ -140,12 +154,14 @@ func main() { // if os.Open returns an error then handle it if err != nil { fmt.Println("Unable to open the confix file. Did you place it in the right spot?") + fileLogger.Println(err) log.Fatal(err) } defer func(settingsJson *os.File) { err := settingsJson.Close() if err != nil { log.Printf("Couldn't close the settings file. Error: %s", err) + fileLogger.Printf("Couldn't close the settings file. Error: %s", err) } }(settingsJson) @@ -154,15 +170,18 @@ func main() { err = json.Unmarshal(byteValue, &settings) if err != nil { fmt.Println("Check that you do not have errors in your JSON file.") + fileLogger.Printf("Could not unmashal json: %s\n", err) log.Fatalf("could not unmarshal json: %s\n", err) return } fmt.Printf("IP address outside the NAT is: %s\n", newIPAddress) - fmt.Printf("Domains to update are: %s\n", settings.Domains) + fileLogger.Printf("IP address outsite the NAT is %s\n", newIPAddress) + fmt.Printf("Domains to update are: %s\n", settings.Domains) dnsRecords := getDNSRecords(settings.ApiKey) var records dnsRecordsJSON err = json.Unmarshal([]byte(dnsRecords), &records) if err != nil { + fileLogger.Printf("Unable to unmashal the JSON from Dreamhost. err is: %n", err) log.Fatalf("Unable to unmarshall JSON from Dreamhost. err is: %s\n", err) } @@ -172,10 +191,12 @@ func main() { currentDomain := urlIPPair{url: url["record"], ipAddress: url["value"]} updatedDomains = append(updatedDomains, currentDomain.url) if currentDomain.ipAddress != newIPAddress { + fileLogger.Printf("%s has an old IP of %s. Will attempt to change to %s", currentDomain.url, currentDomain.ipAddress, newIPAddress) log.Printf("%s has an old IP of %s. Will attempt to change to %s", currentDomain.url, currentDomain.ipAddress, newIPAddress) updateDNSRecord(currentDomain.url, currentDomain.ipAddress, newIPAddress, settings.ApiKey) } else { - log.Printf("%s is already set to the IP address: %s", currentDomain.url, currentDomain.ipAddress) + fileLogger.Printf("%s is already set to IP address: %s", currentDomain.url, currentDomain.ipAddress) + log.Printf("%s is already set to the IP address: %s", currentDomain.url, currentDomain.ipAddress) } } From fb67eaf94c54ab55d35bed9c1850f011bffa3644 Mon Sep 17 00:00:00 2001 From: Eric Mesa Date: Sat, 25 Feb 2023 19:03:22 -0500 Subject: [PATCH 7/7] Implemented verbose flag Closes issue #6 --- main.go | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index fee00fc..294ed81 100644 --- a/main.go +++ b/main.go @@ -91,6 +91,13 @@ func contains(s []string, str string) bool { return false } +//conditionalLog will print a log to the console if logActive true +func conditionalLog(message string, logActive bool){ + if logActive{ + log.Println(message) + } +} + // addDNSRecord adds an IP address to a domain in dreamhost func addDNSRecord(domain string, newIPAddress string, apiKey string) string { command := "dns-add_record&record=" + domain + "&type=A" + "&value=" + newIPAddress @@ -145,8 +152,8 @@ func main() { configFilePath, err := xdg.ConfigFile("dreamhostdns/settings.json") if err != nil { - log.Fatal(err) - fileLogger.Println(err) + conditionalLog(err.Error(), *verbose) + fileLogger.Fatal(err) } fmt.Printf("Looking for settings.jon should be at the following path: %s\n", configFilePath) newIPAddress := getHostIpAddress() @@ -154,14 +161,15 @@ func main() { // if os.Open returns an error then handle it if err != nil { fmt.Println("Unable to open the confix file. Did you place it in the right spot?") - fileLogger.Println(err) - log.Fatal(err) + conditionalLog(err.Error(), *verbose) + fileLogger.Fatal(err) } defer func(settingsJson *os.File) { err := settingsJson.Close() if err != nil { - log.Printf("Couldn't close the settings file. Error: %s", err) - fileLogger.Printf("Couldn't close the settings file. Error: %s", err) + errorString := fmt.Sprintf("Couldn't close the settings file. Error: %s", err) + conditionalLog(errorString, *verbose) + fileLogger.Fatal(errorString) } }(settingsJson) @@ -170,8 +178,9 @@ func main() { err = json.Unmarshal(byteValue, &settings) if err != nil { fmt.Println("Check that you do not have errors in your JSON file.") - fileLogger.Printf("Could not unmashal json: %s\n", err) - log.Fatalf("could not unmarshal json: %s\n", err) + errorString := fmt.Sprintf("Could not unmashal json: %s\n", err) + conditionalLog(errorString, *verbose) + fileLogger.Fatal(errorString) return } fmt.Printf("IP address outside the NAT is: %s\n", newIPAddress) @@ -181,8 +190,9 @@ func main() { var records dnsRecordsJSON err = json.Unmarshal([]byte(dnsRecords), &records) if err != nil { - fileLogger.Printf("Unable to unmashal the JSON from Dreamhost. err is: %n", err) - log.Fatalf("Unable to unmarshall JSON from Dreamhost. err is: %s\n", err) + errorString := fmt.Sprintf("Unable to unmashal the JSON from Dreamhost. err is: %n", err) + conditionalLog(errorString, *verbose) + fileLogger.Fatal(errorString) } var updatedDomains []string @@ -191,12 +201,14 @@ func main() { currentDomain := urlIPPair{url: url["record"], ipAddress: url["value"]} updatedDomains = append(updatedDomains, currentDomain.url) if currentDomain.ipAddress != newIPAddress { - fileLogger.Printf("%s has an old IP of %s. Will attempt to change to %s", currentDomain.url, currentDomain.ipAddress, newIPAddress) - log.Printf("%s has an old IP of %s. Will attempt to change to %s", currentDomain.url, currentDomain.ipAddress, newIPAddress) + logString := fmt.Sprintf("%s has an old IP of %s. Will attempt to change to %s", currentDomain.url, currentDomain.ipAddress, newIPAddress) + fileLogger.Printf(logString) + conditionalLog(logString, *verbose) updateDNSRecord(currentDomain.url, currentDomain.ipAddress, newIPAddress, settings.ApiKey) } else { - fileLogger.Printf("%s is already set to IP address: %s", currentDomain.url, currentDomain.ipAddress) - log.Printf("%s is already set to the IP address: %s", currentDomain.url, currentDomain.ipAddress) + logString := fmt.Sprintf("%s is already set to IP address: %s", currentDomain.url, currentDomain.ipAddress) + fileLogger.Println(logString) + conditionalLog(logString, *verbose) } }