diff options
| -rw-r--r-- | README.md | 77 | ||||
| -rw-r--r-- | nccli.go | 9 | ||||
| -rw-r--r-- | nccli_test.go | 9 | ||||
| -rwxr-xr-x | run.sh | 8 |
4 files changed, 58 insertions, 45 deletions
@@ -1,14 +1,23 @@ # Namecheap CLI -A quick script to list/add/remove/update DNS records for a Namecheap managed domain. It does not use any non-standard Go libraries. +A CLI to list/add/remove/update DNS records for a +Namecheap managed domain using only standard Go libraries. ## Why -At the time of writing, there is no simple way to add DNS records for my Namecheap domain from the cli, since strictly "adding" is not supported. Instead, we have to first get all the current records and include the new one as part of the [setHosts](https://www.namecheap.com/support/api/methods/domains-dns/set-hosts/) method. This would involve a bunch of XML parsing which is beyond my bash-fu. +At the time of writing, there is no simple way to "add" +DNS records to my Namecheap domain from the CLI, +since strictly "adding" is not supported. +Instead, we have to first get all the current records and include +the new one as part of the +[setHosts](https://www.namecheap.com/support/api/methods/domains-dns/set-hosts/) +method. This would involve a bunch of XML parsing which is beyond my bash-fu. ## Pre-Requisites -You must setup an API Access Key, and whitelist your access IP. Follow instructions for [Enabling API Access](https://www.namecheap.com/support/api/intro/). +You must setup an API Access Key, and whitelist your access IP. +Follow instructions for +[Enabling API Access](https://www.namecheap.com/support/api/intro/). ## Usage @@ -16,7 +25,29 @@ Available sub-commands: * `list`: show all DNS records currently configured. * `add`: add a DNS record. * `update`: update an existing DNS record. -* `remove`: remove existing DNS record(s). You will be prompted for which record(s) you want to remove. +* `remove`: remove existing DNS record(s). +You will be prompted for which record(s) you want to remove. + +### Non-Docker +1. Build: +```bash +go build nccli.go +``` + +2. Create `nccli.env` file with content: +```bash +API_USER="[ your namecheap user ]" +API_KEY="[ your namecheap api access key ]" +ACCESS_IP="[ your namecheap api whitelisted IP ]" +``` + +3. Run: +```bash +DOMAIN=[ your namecheap domain (eg. foo.xyz) ] ./run.sh list +# OR +source nccli.env +DOMAIN=[ your namecheap domain (eg. foo.xyz) ] ./nccli list +``` ### Docker @@ -25,12 +56,11 @@ docker run --rm -it --name nctest \ -e API_USER="[ your namecheap user ]" \ -e API_KEY="[ your namecheap api access key ]" \ -e ACCESS_IP="[ your namecheap api whitelisted IP ]" \ - -e DOMAIN_MAIN="[ your namecheap managed domain ]" \ - -e DOMAIN_END="[ your namecheap managed domain's ending (net, or com, or xyz etc.) ]" \ + -e DOMAIN="[ your namecheap domain (eg. foo.xyz) ]" \ registry.gitlab.com/kdam0/nccli:[your architecture] \ - /bin/nccli add -h + /bin/nccli list ``` -Outputs: +Example Outputs: ```bash Usage of add: -name string @@ -49,31 +79,6 @@ Images are available for the following architectures: * linux/amd64 * linux/arm32v7 -### Non-Docker -1. Create `nccli.env` file with content: -```bash -API_USER="[ your namecheap user ]" -API_KEY="[ your namecheap api access key ]" -ACCESS_IP="[ your namecheap api whitelisted IP ]" -DOMAIN_MAIN="[ your namecheap managed domain ]" -DOMAIN_END="[ your namecheap managed domain's ending (net, or com, or xyz etc.) ]" -``` - -2. Build: -```bash -go build nccli.go -``` - -3. Source vars file: -```bash -source nccli.env -``` - -4. Run: -```bash -./nccli -h -``` - ## How to use it to automate Certbot DNS verification Certbot does not have a DNS plugin for Namecheap, so we rely on its `--manual-auth-hook` flag to run a script to place DNS records @@ -99,8 +104,7 @@ where `auth.sh` is: API_USER="[ your namecheap user ]" API_KEY="[ your namecheap api access key ]" API_ACCESS_IP="[ your namecheap api whitelisted IP ]" -DOMAIN_MAIN="[ your namecheap managed domain ]" -DOMAIN_END="[ your namecheap managed domain's ending (net, or com, or xyz etc.) ]" +DOMAIN="[ your namecheap domain (eg. foo.xyz) ]" echo "CERTBOT DOMAIN: ${CERTBOT_DOMAIN}" echo "CERTBOT VALUE: ${CERTBOT_VALIDATION}" @@ -111,8 +115,7 @@ docker run --rm -it \ -e API_USER="${API_USER}" \ -e API_KEY="${API_KEY}" \ -e ACCESS_IP="${API_ACCESS_IP}" \ - -e DOMAIN_MAIN="${DOMAIN_MAIN}" \ - -e DOMAIN_END="${DOMAIN_END}" \ + -e DOMAIN="${DOMAIN}" \ registry.gitlab.com/kdam0/nccli \ /bin/nccli \ add \ @@ -69,7 +69,7 @@ func getEnv(key string) (string, error) { // Load ENV vars into the apiUrl var. // Returns nil if config loaded successfully, error otherwise. func loadConfig() (string, error) { - keys := [5]string{"API_USER", "API_KEY", "ACCESS_IP", "DOMAIN_MAIN", "DOMAIN_END"} + keys := [4]string{"API_USER", "API_KEY", "ACCESS_IP", "DOMAIN"} for _, key := range keys { _, err := getEnv(key) if err != nil { @@ -79,8 +79,11 @@ func loadConfig() (string, error) { user, _ := getEnv("API_USER") apiKey, _ := getEnv("API_KEY") accessIP, _ := getEnv("ACCESS_IP") - domainMain, _ := getEnv("DOMAIN_MAIN") - domainEnd, _ := getEnv("DOMAIN_END") + + domain, _ := getEnv("DOMAIN") + domainSlices := strings.Split(domain, ".") + domainMain := domainSlices[0] + domainEnd := domainSlices[1] apiUrl := "https://api.namecheap.com/xml.response?ApiUser=" + user + "&ApiKey=" + apiKey + diff --git a/nccli_test.go b/nccli_test.go index 4495f05..f59ed7e 100644 --- a/nccli_test.go +++ b/nccli_test.go @@ -35,11 +35,10 @@ func TestLoadConfigMissingKey(t *testing.T) { os.Setenv("API_USER", "user") os.Setenv("API_KEY", "mykey") os.Setenv("ACCESS_IP", "1.1.1.1") - os.Setenv("DOMAIN_MAIN", "mysite") - // NOTE: DOMAIN_END is missing. + // NOTE: DOMAIN is missing. _, err := loadConfig() - _, errWant := getEnv("DOMAIN_END") + _, errWant := getEnv("DOMAIN") if err.Error() != errWant.Error() || err == nil { t.Fatalf(`have %v, want %v`, err, errWant) @@ -52,8 +51,8 @@ func TestLoadConfigAllKeys(t *testing.T) { os.Setenv("API_USER", "user") os.Setenv("API_KEY", "mykey") os.Setenv("ACCESS_IP", "1.1.1.1") - os.Setenv("DOMAIN_MAIN", "mysite") - os.Setenv("DOMAIN_END", "xyz") + os.Setenv("DOMAIN", "mysite.xyz") + // os.Setenv("DOMAIN_END", "xyz") apiUrl, _ := loadConfig() want := "https://api.namecheap.com/xml.response?ApiUser=user&ApiKey=mykey&UserName=user&ClientIp=1.1.1.1&SLD=mysite&TLD=xyz" @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +source nccli.env +# TODO: validate that $DOMAIN is provided. +API_USER=$API_USER \ + API_KEY=$API_KEY \ + ACCESS_IP=$ACCESS_IP \ + ./nccli "$@" |
