gen-completion 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. set -euo pipefail
  11. if [[ $# -lt 1 ]]
  12. then
  13. echo "usage: $0 <command>
  14. Generates shell completion for all available tools.
  15. Commands:
  16. generate generates and writes shell completion to ../misc/completions
  17. check checks whether all shell completion scripts are up-to-date"
  18. exit 1
  19. fi
  20. cmd=$1 && shift
  21. directory="$(dirname "$0")"
  22. supported_shells=(bash zsh)
  23. python_autocompletions=(scratch)
  24. . misc/shlib/shlib.bash
  25. case "$cmd" in
  26. generate)
  27. for name in "${python_autocompletions[@]}"; do
  28. for shell in "${supported_shells[@]}"; do
  29. mkdir -p "$directory"/../misc/completions/"$shell"
  30. "$directory"/pyactivate -m materialize.cli."$name" completion "$shell" > "$directory"/../misc/completions/"$shell"/_"$name"
  31. done
  32. done
  33. ;;
  34. check)
  35. shasum=sha1sum
  36. if ! command_exists "$shasum"; then
  37. shasum=shasum
  38. fi
  39. for name in "${python_autocompletions[@]}"; do
  40. for shell in "${supported_shells[@]}"; do
  41. if [ ! -f "$directory/../misc/completions/$shell/_$name" ]; then
  42. printf "missing shell completion scripts. try running \`bin/gen-completion generate\` and checking in the changes\n"
  43. exit 1
  44. fi
  45. latest=$("$directory"/pyactivate -m materialize.cli."$name" completion "$shell" | "$shasum")
  46. existing=$("$shasum" < "$directory"/../misc/completions/"$shell"/_"$name")
  47. if [[ "$latest" != "$existing" ]]; then
  48. printf "shell completion scripts have uncommitted changes. try running \`bin/gen-completion generate\` and checking in the changes\n"
  49. exit 1
  50. fi
  51. done
  52. done
  53. ;;
  54. *)
  55. printf "unknown command %q\n" "$cmd"
  56. exit 1
  57. ;;
  58. esac