hazelweakly,
@hazelweakly@hachyderm.io avatar

Writing me the good code todayyy

Set all variables to their known defaults if they aren't already set

set_if_unset() {
key=$1
current_value="${!key}"
array_value="${base_variable_defaults["$key"]-${derived_variable_defaults["$key"]}}"
if [[ $current_value =~ "WARNING: UNSET" ]]; then
printf -v "$key" -- "$array_value"
fi
}

e_nomem,
@e_nomem@hachyderm.io avatar

@hazelweakly Reminds me of when I wrote bash functions that took an array as an input argument...

myarray=(some stuff here)

array_as_arg() {  
 arrayname="$1[@]"  
 input_array=( "${!arrayname}" )

 echo "${input_array[@]}"  
}

array_as_arg myarray  
hazelweakly,
@hazelweakly@hachyderm.io avatar

Other goodies from this cursed script

declare -A derived_variable_defaults=(
[KUBECONFIG_PATH]="./${CLUSTER:-dev}-kubeconfig.yaml"
[USE_SSO]="$([[ -z $CI ]] && printf -- "--with-sso")"
[helmfile_image]="${ACCOUNT_ID}.dkr.ecr-fips.us-east-1.amazonaws.com/helmfile:0.1-amd64"
[BASE_REGISTRY]="${registries["$ACCOUNT_ID:$CLUSTER"]-$(warn_var "registries[$ACCOUNT_ID:$CLUSTER]")}"
[helm_cmd]="$([[ ${@: -1} =~ ^(--diff|--deploy)$ ]] && echo "${helm_flag_to_cmd["${@: -1}"]}" || "diff")"
)

sleepyfox,
@sleepyfox@hachyderm.io avatar

@hazelweakly Of course it had to be related...

hazelweakly, (edited )
@hazelweakly@hachyderm.io avatar

Be honest and point on the doll where the bash script hurt you

jwd630,
@jwd630@mastodon.social avatar

@hazelweakly Dunno, neither shellcheck nor the bash’s I’ve tried like that derived_variable_defaults=( … initialization. But props for using an interpreter that’ll be found on an otherwise bare bones platform without needing a dependency installed.

hazelweakly,
@hazelweakly@hachyderm.io avatar

@jwd630 this code is actually specifically written in a way that is set -euo pipefail compliant, shellcheck compliant, and shellharden compliant

It's great ;D

jwd630,
@jwd630@mastodon.social avatar

@hazelweakly I learned something about bash associative array initialization while diving down the rabbit hole of your code snippets, before I hung it up realizing I was being that guy - so there's that.

hazelweakly,
@hazelweakly@hachyderm.io avatar

@jwd630 my condolences! Or, you're welcome. Take your poison :)

Two9A,
@Two9A@hachyderm.io avatar

@hazelweakly I'd vote "I didn't know bash could do this", but I use a letsencrypt helper that talks to Namecheap's JSON API and it's written entirely in bash.

That thing I try not to look at too hard, in case it eats me.

isotopp,
@isotopp@chaos.social avatar

@hazelweakly it is at this point two weeks ago that you should have considered doing that in go or python. Good luck with it in another two weeks. 😁

icing,
@icing@chaos.social avatar

@isotopp @hazelweakly Time is a cruel mistress.😌

khionu,
@khionu@hachyderm.io avatar

@hazelweakly sweetie, get some help

Di4na,
@Di4na@hachyderm.io avatar

@hazelweakly other: i am literally writing a better scripting, jigs, and cli tool making language just to stop these atrocities to spread now

mxk,
@mxk@hachyderm.io avatar

@hazelweakly one question: is it shellcheck clean?

hazelweakly,
@hazelweakly@hachyderm.io avatar

@mxk always! I have it running in my editor at all time and always set -euo pipefail

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • DreamBathrooms
  • ngwrru68w68
  • tester
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • tacticalgear
  • JUstTest
  • osvaldo12
  • normalnudes
  • cubers
  • cisconetworking
  • everett
  • GTA5RPClips
  • ethstaker
  • Leos
  • provamag3
  • anitta
  • modclub
  • megavids
  • lostlight
  • All magazines