format_xml.sh 722 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/bin/bash
  2. set -e
  3. # A POSIX variable
  4. OPTIND=1 # Reset in case getopts has been used previously in the shell.
  5. # Initialize variables
  6. mode="format"
  7. while getopts "h?c" opt; do
  8. case "$opt" in
  9. h|\?)
  10. show_help
  11. exit 0
  12. ;;
  13. c) mode="check"
  14. ;;
  15. esac
  16. done
  17. xml_files=$(find . -name "*.xml")
  18. ret=0
  19. for f in $xml_files
  20. do
  21. xmllint -format "${f}" > "${f}".new
  22. case "$mode" in
  23. format)
  24. if ! cmp "${f}" "${f}".new >/dev/null 2>&1
  25. then
  26. echo "formatting $f"
  27. cp "${f}".new "${f}"
  28. fi
  29. ;;
  30. check)
  31. if ! cmp "${f}" "${f}".new >/dev/null 2>&1
  32. then
  33. echo "$f needs formatting - run ./scripts/format_xml.sh $f"
  34. ret=1
  35. fi
  36. ;;
  37. esac
  38. rm "${f}".new
  39. done
  40. exit $ret