update headers to use code blocks again now that i fixed that
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
title: azure locations and file crawling
|
||||
image:
|
||||
src: 2025/azure-locations.jpg
|
||||
alt: "A Linux terminal. There is a fun rainbow flag in ascii art at the top, and then the user has called a command asking Azure for a list of resources applicable to a specific resource type"
|
||||
alt: "A Linux terminal. There is a fun rainbow flag in ascii art at the top, and then the user has called a command asking Azure for a list of locations applicable to a specific resource type. The output is lengthy."
|
||||
tags:
|
||||
- reference
|
||||
- software
|
||||
@@ -75,7 +75,7 @@ Okay, let's backtrack. From a given Bicep file, we want:
|
||||
1. All referenced resource types
|
||||
1. All referenced modules
|
||||
|
||||
### grep
|
||||
### `grep`
|
||||
|
||||
Resources and modules both have patterns in how they are declared. Thankfully, they're pretty simple regexes. `grep` will spit out lines in a file that match a given regex.
|
||||
|
||||
@@ -89,7 +89,7 @@ grep -E "^resource " "$file"
|
||||
grep -E "^module " "$file"
|
||||
```
|
||||
|
||||
### cut
|
||||
### `cut`
|
||||
|
||||
From there, let's use `cut` to strip off the parts we don't want.
|
||||
|
||||
@@ -108,7 +108,7 @@ grep -E "^module " "$file" \
|
||||
|
||||
These calls are a little opaque. `-d` sets a **delimiter** (what to split on). `-f` picks a **field** to return, numbered from 1.
|
||||
|
||||
### mapfile
|
||||
### `mapfile`
|
||||
|
||||
We'll save these values to variables. `mapfile` reads a file, putting each line into a new array element. `-t` **trims** newline characters. The `<`s do some redirection, and yes, the space between them *matters*.
|
||||
|
||||
@@ -120,7 +120,7 @@ mapfile -t modules < <(grep -E "^module " "$file" \
|
||||
| cut -d "'" -f 2 -)
|
||||
```
|
||||
|
||||
### dirname (& more)
|
||||
### `dirname` (& more)
|
||||
|
||||
We can't just stop there. We need to search each module in turn. Using `dirname`, we can get the directory of the file we're searching, then append the relative module path.
|
||||
|
||||
@@ -143,7 +143,7 @@ Additionally, `mapfile` usually writes from index 0 onwards. But with the `-O` a
|
||||
|
||||
Finally, we got some recursion going! `get_resources` calls `get_resources` for every module found.
|
||||
|
||||
### the get_resources function
|
||||
### the `get_resources` function
|
||||
|
||||
So far, our code looks like this:
|
||||
|
||||
@@ -176,7 +176,7 @@ Now we need to use these resource types to get available locations. First, actua
|
||||
mapfile -t resources < <(get_resources "main.bicep")
|
||||
```
|
||||
|
||||
### sort
|
||||
### `sort`
|
||||
|
||||
Does sorting matter? Not really, but `sort` has a useful feature, `-u`, which returns **u**nique items (aka, it deduplicates). Looking up the same resource type twice slows us down.
|
||||
|
||||
@@ -186,7 +186,7 @@ mapfile -t resources < <(get_resources "main.bicep" | sort -u)
|
||||
|
||||
`sort` is one reason it helps to have newlines as delimiters - it expects that.
|
||||
|
||||
### az
|
||||
### `az`
|
||||
|
||||
We'll use `az` to list *all* the locations - just to give ourselves a starting point. You could also use the locations for the first resource type.
|
||||
|
||||
@@ -205,7 +205,7 @@ mapfile -t newLocations < <(az provider show --namespace "$namespace" \
|
||||
|
||||
`--out tsv` means we will get a list with no decoration whatsoever - it's vital for programmatic handling of `az` command output.
|
||||
|
||||
### cut (again)
|
||||
### `cut` (again)
|
||||
|
||||
We'll need to get those `$namespace` and `$resourceType` variables. `cut` comes back in handy:
|
||||
|
||||
@@ -221,7 +221,7 @@ namespace=$(echo "$resource" | cut -d "/" -f 1 -)
|
||||
resourceType=$(echo "$resource" | cut -d "/" -f 2 -)
|
||||
```
|
||||
|
||||
### comm
|
||||
### `comm`
|
||||
|
||||
Okay, we can get locations. How do we handle finding their intersection?
|
||||
|
||||
@@ -248,7 +248,7 @@ then
|
||||
fi
|
||||
```
|
||||
|
||||
### tee
|
||||
### `tee`
|
||||
|
||||
We'll print the locations to the shell. We can even use `tee` to print them to a file for good measure:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user