terragrunt-local 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #! /bin/bash
  2. findup ()
  3. {
  4. DIR='..'
  5. DIRTEST=$( cd $DIR ; pwd )
  6. while [[ "$DIRTEST" != "/" ]]; do
  7. DIR="$DIR/.."
  8. DIRTEST=$( cd $DIR ; pwd )
  9. if [[ -d "$DIR/$1" ]]; then
  10. echo "$DIR/$1"
  11. return 0
  12. fi
  13. done
  14. # Failed to find
  15. return 1
  16. }
  17. if [ ! -f terragrunt.hcl ]; then
  18. echo "ERROR: No terragrunt.hcl in this directory. Aborting."
  19. exit 1
  20. fi
  21. GITSOURCE=$( cat terragrunt.hcl | egrep -v '^ *#' | egrep 'source *= *"git@' )
  22. if [ "$GITSOURCE" == "" ]; then
  23. echo "ERROR: No source from Git detected. Aborting."
  24. exit 2
  25. fi
  26. # Strip off source = and quotes
  27. GITSOURCE=$( echo "$GITSOURCE" | sed -E 's/.*"([^"]+)".*/\1/' )
  28. # Strip off any ?ref= if it's there
  29. GITSOURCE=$( echo "$GITSOURCE" | sed -E 's/\?.*$//' )
  30. # Thanks to terragrunt needing that magic // construct
  31. GITSOURCE_REPO=$( echo "$GITSOURCE" | sed -E 's#//.*$##' )
  32. GITSOURCE_PATHINREPO=$( echo "$GITSOURCE" | sed -E "s#.*//##" )
  33. BARE_REPONAME=$( basename "$GITSOURCE_REPO" .git )
  34. LOCALPATH=$( findup "$BARE_REPONAME" )
  35. if [[ "$LOCALPATH" == "" ]]; then
  36. echo "ERROR: No $BARE_REPONAME in any parent directory"
  37. exit 1
  38. fi
  39. NEWPATH="$LOCALPATH//$GITSOURCE_PATHINREPO"
  40. echo Substituting \'$GITSOURCE\' with \'$NEWPATH\'
  41. # Test locally
  42. # the double // is intentional! Terragrunt uses this to determine the root of the modules repository.
  43. terragrunt $* --terragrunt-source $NEWPATH