Skip to content

Instantly share code, notes, and snippets.

@stefanotorresi
Last active February 6, 2019 17:01
Show Gist options
  • Save stefanotorresi/f3e6c2cb220dcf38237893ef9213af2e to your computer and use it in GitHub Desktop.
Save stefanotorresi/f3e6c2cb220dcf38237893ef9213af2e to your computer and use it in GitHub Desktop.
bash script to shorten helm release names
#!/usr/bin/env bash
set -euo pipefail
RELEASE_NAME=$1
MAX_LENGTH=${2-53}
HASH_SIZE=${3-8}
if [[ $MAX_LENGTH -le $(($HASH_SIZE+1)) ]]; then
echo "max length must be bigger than hash size + 1"
exit 1
fi
if [[ ${#RELEASE_NAME} -le $MAX_LENGTH ]]; then
echo -n $RELEASE_NAME
exit
fi
TRUNCATION_SIZE=$(($MAX_LENGTH-$HASH_SIZE-1))
TRUNCATION=${RELEASE_NAME:0:$TRUNCATION_SIZE}
RELEASE_NAME_HASH=$(echo -n $RELEASE_NAME | sha1sum | awk '{print $1}')
echo -n $TRUNCATION-${RELEASE_NAME_HASH:0:$HASH_SIZE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment