Updated readme
changed the nushell language denominators from "nushell" to "nu" to see if Chroma pics them up correctly
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
```
|
||||
Shebang tells the system to use the NuShell interpreter.
|
||||
|
||||
```nushell
|
||||
```nu
|
||||
let timeStamp = date now | format date "%Y-%m-%dT%H:%M:%S"
|
||||
let cfToken = "hurdyburdytokengoeshere"
|
||||
let myIp = http get https://api.ipify.org
|
||||
@@ -17,24 +17,24 @@ let authHeader = {
|
||||
```
|
||||
The usual suspects here, except we use the `let` command to set immutable variables. Note how the headers array is treated like an actual array and instead of relying on `curl`, NuShell has native `http get` capability.
|
||||
|
||||
```nushell
|
||||
```nu
|
||||
let dnsZones = (http get $"($cfAPIBaseURI)zones" --headers $authHeader).result
|
||||
```
|
||||
Using the URI & header variables we just established, the script pulls a JSON array of my DNS zones from Cloudflare, [as seen here](https://pastebin.com/fLGsDTVK). NuShell is made for structured data and thus natively understands JSON arrays. We're after the `result` part of the JSON response, hence the `.result` tacked on at the end, just like in PowerShell.
|
||||
### Traversing the JSON array of DNS zones
|
||||
```nushell
|
||||
```nu
|
||||
$dnsZones | each {
|
||||
|zone|
|
||||
```
|
||||
NuShell has no problems working with JSON arrays, so we do a simple `each` loop, declaring that the zone we're currently working on should be referred to as `$zone`.
|
||||
### Traversing the arrays of DNS records in the array of DNS zones
|
||||
|
||||
```nushell
|
||||
```nu
|
||||
let badRecords = ((http get $"($cfAPIBaseURI)zones/($zone.id)/dns_records" --headers $authHeader).result | where type == "A" and content =~ "^(?!^100)" and content != $myIp)
|
||||
```
|
||||
Leveraging the built-in `http get` functionality we query the Cloudflare API for the DNS records of the DNS zone we're working with. As the JSON data returned from this query is natively supported in NuShell, we can filter it directly in order to only deal with A-records (`where type == "A"`) that are not TailScale entries, ie the IP does not begin with "100" (`and content =~ "^(?!^100)"`) and that aren't already set to the current WAN IP (`and content != $myIp`). To deal with any such records, it's time for another `each` loop:
|
||||
|
||||
```nushell
|
||||
```nu
|
||||
$badRecords | each {
|
||||
|badRecord|
|
||||
let recordBody = {
|
||||
@@ -45,7 +45,7 @@ $badRecords | each {
|
||||
*Oh look, native array support in a human-readable format!*
|
||||
Determining that the current DNS record we're working with shall be called $badRecord, we create the (JSON) array body to submit to Cloudflare. `comment` is simply the comment we'd like to add, in this case that the record was automatically updated. `content` holds the IP we'd like the DNS record to have.
|
||||
### Updating the DNS records
|
||||
```nushell
|
||||
```nu
|
||||
let updateResult = (http patch $"($cfAPIBaseURI)zones/($zone.id)/dns_records/($badRecord.id)" $recordBody --headers $authHeader --content-type application/json)
|
||||
if $updateResult.success {
|
||||
print $"Successfully changed the DNS record for ($badRecord.name) from ($badRecord.content) to ($myIp)"
|
||||
@@ -82,7 +82,7 @@ Sixth and final columt is what to actually *do*. Simple enough in this case, the
|
||||
Now save the file (`ctrl+x` in Nano) and that's all there is to it!
|
||||
|
||||
### Full source code
|
||||
```nushell
|
||||
```nu
|
||||
#!/usr/bin/env nu
|
||||
#First stab at a nushell script, let's fix Cloudflare DNS
|
||||
let timeStamp = date now | format date "%Y-%m-%dT%H:%M:%S"
|
||||
|
||||
Reference in New Issue
Block a user