aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKumar Damani <me@kumardamani.net>2022-06-07 02:49:28 +0000
committerKumar Damani <me@kumardamani.net>2022-06-07 02:49:28 +0000
commit93dbf2e414a5cbb9e0d9e0785286d978fb0fbfea (patch)
tree16bd65da31792a14342b9a481dc88050b994526f
parent4445692f75e4e4072cc4940bcb9eff80ae5559f0 (diff)
Added comments to script.
-rw-r--r--nccli.go57
1 files changed, 43 insertions, 14 deletions
diff --git a/nccli.go b/nccli.go
index 4059bd1..87d578a 100644
--- a/nccli.go
+++ b/nccli.go
@@ -53,9 +53,9 @@ type Host struct {
// Helper functions //
-// A helper to check if a env var is defined. If not defined, show error.
-// Args: key - the env var to check for
-// Returns if exists, the value of the var
+// Check if a env var is defined. If not defined, show error.
+// Args: key - the env var to check for.
+// Returns if exists, the value of the var.
func getEnv(key string) string {
value := os.Getenv(key)
if len(value) == 0 {
@@ -67,6 +67,7 @@ func getEnv(key string) string {
return value
}
+// Load ENV vars into the url var.
func loadConfig() {
url = "https://api.namecheap.com/xml.response?ApiUser=" + getEnv("API_USER") +
"&ApiKey=" + getEnv("API_KEY") +
@@ -76,6 +77,7 @@ func loadConfig() {
"&TLD=" + getEnv("DOMAIN_END")
}
+// Print host recrods to stdout.
func logRecords(hosts []Host) {
fmt.Println("================================")
fmt.Println("Number of DNS records:", len(hosts))
@@ -92,6 +94,10 @@ func logRecords(hosts []Host) {
fmt.Println("================================")
}
+// Make the API request to Namecheap.
+// Args: endPoint - the api method to call.
+// extraParams (optional) - more params to add to the url.
+// Returns the response byte stream.
func makeReq(endPoint string, extraParams ...string) []byte {
apiUrl := url + "&Command=namecheap.domains.dns." + endPoint
@@ -127,6 +133,10 @@ func makeReq(endPoint string, extraParams ...string) []byte {
return responseData
}
+// Converts user input strings into ints. Errors if input is wrong.
+// Args: userChoicesStrs - slice of strings corresponding to user choices.
+// maxVal - the maximum int value allowed as input.
+// Returns the slice of integers.
func normalizeUserChoices(userChoicesStrs []string, maxVal int) []int {
var chosenInts []int
for _, choice := range userChoicesStrs {
@@ -147,7 +157,10 @@ func normalizeUserChoices(userChoicesStrs []string, maxVal int) []int {
return chosenInts
}
-// If an existing (Name, Type) exists => update the first match...
+// Updates the hostRecords set with new data.
+// If an existing (Name, Type) exists, update the first match.
+// Args: hostRecords - slice of Host corresponding to DNS records.
+// newHost - the new values to set for the DNS record.
func updateHostRecordSet(hostRecords []Host, newHost Host) {
for i, host := range hostRecords {
if (host.Name == newHost.Name) && (host.Type == newHost.Type) {
@@ -158,6 +171,9 @@ func updateHostRecordSet(hostRecords []Host, newHost Host) {
}
}
+// Performs the API calls that updates Namecheap in any way,
+// and handles the resulting response validation.
+// Args: urlEncodedHosts - params consisting of DNS records.
func updateNamecheapRecords(urlEncodedHosts string) {
// make the api request to namecheap
responseData := makeReq("sethosts", urlEncodedHosts)
@@ -178,6 +194,9 @@ func updateNamecheapRecords(urlEncodedHosts string) {
}
}
+// Build a string with all DNS records.
+// Args: hosts - slice of Host corresponding to DNS records.
+// Returns all DNS records in the format expected by the API.
func urlEncodeHosts(hosts []Host) string {
var res string
for i, host := range hosts {
@@ -192,6 +211,9 @@ func urlEncodeHosts(hosts []Host) string {
return res
}
+// Prompt for user input regarding removal of records, and
+// handles user flow.
+// Returns a slice of strings corresponding to user selections.
func userInputHandler() []string {
var chosen []string
for {
@@ -208,7 +230,6 @@ func userInputHandler() []string {
fmt.Println("Nothing chosen. Exiting.")
os.Exit(0)
}
- // split by ","
chosen = strings.Split(text, " ")
fmt.Println("You chose", chosen, ". Are you sure you want to remove these? Y/n:")
@@ -228,6 +249,8 @@ func userInputHandler() []string {
// Main functions //
+// Get all existing DNS records.
+// Returns a slice of Host corresponding to DNS records.
func getAllRecords() []Host {
responseData := makeReq("gethosts")
var responseObj GetResponseObj
@@ -241,13 +264,18 @@ func getAllRecords() []Host {
return responseObj.Hosts
}
+// Add a DNS record.
+// Args: rName - the name of the DNS record.
+// rType - the type of the DNS record.
+// rAddress - the value of the DNS record.
+// rMXPref - the priority of the DNS record.
+// rTTL - the ttl of the DNS record.
func addRecord(rName string, rType string, rAddress string, rMXPref int, rTTL int) {
// we need to get the records before we can add to it.
var hostRecords = getAllRecords()
if verbose {
logRecords(hostRecords)
}
-
hostRecords = append(hostRecords, Host{
Name: rName,
Type: rType,
@@ -255,12 +283,12 @@ func addRecord(rName string, rType string, rAddress string, rMXPref int, rTTL in
MXPref: rMXPref,
TTL: rTTL,
})
-
- // encode all hosts into a url
urlEncodedHosts := urlEncodeHosts(hostRecords)
updateNamecheapRecords(urlEncodedHosts)
}
+// Remove DNS record(s).
+// Will prompt for user input.
func removeRecord() {
// we need to get the records before we can remove one
var hostRecords = getAllRecords()
@@ -270,7 +298,7 @@ func removeRecord() {
userChoicesStrs := userInputHandler()
userChoicesInts := normalizeUserChoices(userChoicesStrs, len(hostRecords))
- // contains the desired set of host records.
+ // build the desired set of host records.
var newHostRecords []Host
for i, host := range hostRecords {
found := false
@@ -284,19 +312,22 @@ func removeRecord() {
newHostRecords = append(newHostRecords, host)
}
}
-
- // encode all hosts into a url
urlEncodedHosts := urlEncodeHosts(newHostRecords)
updateNamecheapRecords(urlEncodedHosts)
}
+// Update DNS record.
+// Args: rName - the name of the DNS record.
+// rType - the type of the DNS record.
+// rAddress - the value of the DNS record.
+// rMXPref - the priority of the DNS record.
+// rTTL - the ttl of the DNS record.
func updateRecord(rName string, rType string, rAddress string, rMXPref int, rTTL int) {
// we need to get the records before we can update it.
var hostRecords = getAllRecords()
if verbose {
logRecords(hostRecords)
}
-
updateHostRecordSet(
hostRecords,
Host{
@@ -307,8 +338,6 @@ func updateRecord(rName string, rType string, rAddress string, rMXPref int, rTTL
TTL: rTTL,
},
)
-
- // encode all hosts into a url
urlEncodedHosts := urlEncodeHosts(hostRecords)
updateNamecheapRecords(urlEncodedHosts)
}