Skip to content

Instantly share code, notes, and snippets.

@tcaddy
Last active September 9, 2024 14:40
Show Gist options
  • Save tcaddy/cbd0c267490745191bbbd5c65f932a29 to your computer and use it in GitHub Desktop.
Save tcaddy/cbd0c267490745191bbbd5c65f932a29 to your computer and use it in GitHub Desktop.
Golang text/template for parsing a redis database URL

Golang text/template for parsing Redis database URL

This is backup docs for this Go Template Playground setup: here

The idea is that we want to use External Secrets Operator to parse a secret that happens to be a Redis database URL. We want to be able to split the URL into its pieces so we can use them in our apps downstream. This will let us maintain one "official" secret payload in our secret pipeline but have multiple variants of it downstream in our apps.

template:

URL: {{ .url }}
Proto: {{ (split "://" .url)._0 }}
Host: {{ (split ":" (split "/" (split "://" .url)._1)._0)._0 }}
Port: {{ (split ":" (split "/" (split "://" .url)._1)._0)._1 }}
DB Index: {{ (split "/" (split "://" .url)._1)._1 }}
Address: {{ (split ":" (split "/" (split "://" .url)._1)._0)._0 }}:{{ (split ":" (split "/" (split "://" .url)._1)._0)._1 }}

input:

{
  "url": "redis://10.1.2.3:6378/4"
}

output:

URL: redis://10.1.2.3:6378/4
Proto: redis
Host: 10.1.2.3
Port: 6378
DB Index: 4
Address: 10.1.2.3:6378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment