opencode 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/sh
  2. set -e
  3. if [ -n "$OPENCODE_BIN_PATH" ]; then
  4. resolved="$OPENCODE_BIN_PATH"
  5. else
  6. # Get the real path of this script, resolving any symlinks
  7. script_path="$0"
  8. while [ -L "$script_path" ]; do
  9. link_target="$(readlink "$script_path")"
  10. case "$link_target" in
  11. /*) script_path="$link_target" ;;
  12. *) script_path="$(dirname "$script_path")/$link_target" ;;
  13. esac
  14. done
  15. script_dir="$(dirname "$script_path")"
  16. script_dir="$(cd "$script_dir" && pwd)"
  17. # Map platform names
  18. case "$(uname -s)" in
  19. Darwin) platform="darwin" ;;
  20. Linux) platform="linux" ;;
  21. MINGW*|CYGWIN*|MSYS*) platform="win32" ;;
  22. *) platform="$(uname -s | tr '[:upper:]' '[:lower:]')" ;;
  23. esac
  24. # Map architecture names
  25. case "$(uname -m)" in
  26. x86_64|amd64) arch="x64" ;;
  27. aarch64) arch="arm64" ;;
  28. armv7l) arch="arm" ;;
  29. *) arch="$(uname -m)" ;;
  30. esac
  31. name="opencode-${platform}-${arch}"
  32. binary="opencode"
  33. [ "$platform" = "win32" ] && binary="opencode.exe"
  34. # Search for the binary starting from real script location
  35. resolved=""
  36. current_dir="$script_dir"
  37. while [ "$current_dir" != "/" ]; do
  38. candidate="$current_dir/node_modules/$name/bin/$binary"
  39. if [ -f "$candidate" ]; then
  40. resolved="$candidate"
  41. break
  42. fi
  43. current_dir="$(dirname "$current_dir")"
  44. done
  45. if [ -z "$resolved" ]; then
  46. printf "It seems that your package manager failed to install the right version of the opencode CLI for your platform. You can try manually installing the \"%s\" package\n" "$name" >&2
  47. exit 1
  48. fi
  49. fi
  50. # Handle SIGINT gracefully
  51. trap '' INT
  52. # Execute the binary with all arguments
  53. exec "$resolved" "$@"