update_from_skeleton 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #! /bin/bash
  2. #
  3. # Updates the local environments from the skeleton directory
  4. # Parse Arguments. This is a standard parser.
  5. #
  6. # At present, should be fun from a ###-module directory
  7. # Future work:
  8. # * Run it from the account directory
  9. # * Specify only one path
  10. function argparse {
  11. PARAMS=""
  12. while (( "$#" )); do
  13. case "$1" in
  14. -h|--help)
  15. echo Usage: $0 '[-t|--test] [-d|--debug]'
  16. exit 0
  17. ;;
  18. -t|--test)
  19. TESTING="/bin/echo TESTING: "
  20. shift
  21. ;;
  22. -d|--debug)
  23. >&2 echo debug: Enabling debugging..
  24. DEBUG=1
  25. shift
  26. ;;
  27. # -p|--only-path)
  28. # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  29. # ONLY_PATH=$2
  30. # shift 2
  31. # else
  32. # echo "Error: Argument for $1 is missing" >&2
  33. # exit 1
  34. # fi
  35. # ;;
  36. -*|--*=) # unsupported flags
  37. echo "Error: Unsupported flag $1" >&2
  38. exit 1
  39. ;;
  40. *) # preserve positional arguments
  41. PARAMS="$PARAMS $1"
  42. shift
  43. ;;
  44. esac
  45. done
  46. # set positional arguments in their proper place
  47. eval set -- "$PARAMS"
  48. }
  49. function cmpfile {
  50. # Returns 0 if they're the same (and should not be copied)
  51. # Returns 1 if they're different (and should be copied)
  52. SRCFILE=$1
  53. DSTFILE=$2
  54. if [[ ! -f $SRCFILE ]]; then
  55. [[ $DEBUG ]] && echo Warning: $SRCFILE does not exist. Not copying...
  56. return 0
  57. fi
  58. if [[ ! -f $DSTFILE ]]; then
  59. echo Warning: $DSTFILE does not exist. Copying regardless...
  60. return 1
  61. fi
  62. diff $SRCFILE $DSTFILE > /dev/null 2> /dev/null
  63. if [[ $? == 0 ]]; then
  64. [[ $DEBUG ]] && >&2 echo debug: $SRCFILE and $DSTFILE are same
  65. return 0
  66. else
  67. [[ $DEBUG ]] && >&2 echo debug: $SRCFILE and $DSTFILE are different
  68. return 1
  69. fi
  70. }
  71. # Main
  72. argparse $*
  73. SHORT_PWD=$( basename ${PWD} )
  74. PARENT_PWD=$( basename $( cd .. && pwd ) )
  75. [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
  76. [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
  77. [[ $DEBUG == 1 ]] && >&2 echo debug: PARENT_PWD=$PARENT_PWD
  78. # Sanity checking
  79. if [[ $SHORT_PWD == "000-skeleton" ]]; then
  80. >&2 echo Error: Cannot run from skeleton directory. Exiting...
  81. exit 1
  82. fi
  83. if [[ ! $SHORT_PWD =~ ^[0-9]{3}-.* ]]; then
  84. >&2 echo Error: Please run from the module directory you wish to update. Exiting...
  85. exit 2
  86. fi
  87. SRC=../../../../000-skeleton/${SHORT_PWD}
  88. if [[ ! -d ${SRC} ]]; then
  89. >&2 echo Error: ${SRC} does not exist. Cannot copy.
  90. exit 3
  91. fi
  92. cmpfile ${SRC}/README.md ./README.md
  93. if [[ $? != 0 ]]; then
  94. echo Copying readme from skeleton...
  95. ${TESTING} cp -vf ${SRC}/README.md .
  96. else
  97. echo Not copying readme.
  98. fi
  99. cmpfile ${SRC}/terragrunt.hcl ./terragrunt.hcl
  100. if [[ $? != 0 ]]; then
  101. echo Copying terragrunt.hcl from skeleton...
  102. ${TESTING} cp -vf ${SRC}/terragrunt.hcl .
  103. else
  104. echo Not copying terragrunt.
  105. fi