Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

install.sh 8.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  1. #!/usr/bin/env bash
  2. # This script installs or updates the promptfoo CLI tool
  3. # It detects the OS and architecture, downloads the appropriate binary,
  4. # and sets up the necessary environment variables
  5. # Usage:
  6. # To install or update promptfoo:
  7. # curl -sSL https://raw.githubusercontent.com/promptfoo/promptfoo/main/install.sh | bash
  8. #
  9. # To install a specific version:
  10. # curl -sSL https://raw.githubusercontent.com/promptfoo/promptfoo/main/install.sh | bash -s -- -v <version>
  11. #
  12. # To install without prompts (non-interactive):
  13. # curl -sSL https://raw.githubusercontent.com/promptfoo/promptfoo/main/install.sh | bash -s -- -y
  14. #
  15. # To install to a custom directory:
  16. # curl -sSL https://raw.githubusercontent.com/promptfoo/promptfoo/main/install.sh | bash -s -- -d <directory>
  17. #
  18. # Options:
  19. # -v, --version <version> Install a specific version of promptfoo
  20. # -y, --yes Skip confirmation prompts (non-interactive)
  21. # -d, --dir <directory> Install to a custom directory
  22. # -h, --help Display this help message
  23. # Ensure the entire script is downloaded before execution
  24. {
  25. set -e
  26. echo "WARNING: This installation script is in alpha. Expect bugs and issues."
  27. echo "It does not work for ARM-based systems."
  28. echo "Please report any problems you encounter to the promptfoo team."
  29. echo "Press Ctrl+C to cancel or wait 5 seconds to continue..."
  30. sleep 5
  31. # Configuration variables
  32. GITHUB_REPO="promptfoo/promptfoo"
  33. INSTALL_DIR="$HOME/.promptfoo"
  34. BIN_DIR="/usr/local/bin"
  35. TMP_DIR=$(mktemp -d)
  36. # Check if a command exists
  37. # Usage: promptfoo_has <command>
  38. promptfoo_has() {
  39. type "$1" >/dev/null 2>&1
  40. }
  41. # Echo a message to stdout
  42. # Usage: promptfoo_echo <message>
  43. promptfoo_echo() {
  44. command printf %s\\n "$*" 2>/dev/null
  45. }
  46. # Download a file using curl or wget
  47. # Usage: promptfoo_download <url> [<additional_args>...]
  48. promptfoo_download() {
  49. if promptfoo_has "curl"; then
  50. curl --fail --compressed -q "$@"
  51. elif promptfoo_has "wget"; then
  52. # Emulate curl with wget
  53. ARGS=$(promptfoo_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \
  54. -e 's/--compressed //' \
  55. -e 's/--fail //' \
  56. -e 's/-L //' \
  57. -e 's/-I /--server-response /' \
  58. -e 's/-s /-q /' \
  59. -e 's/-sS /-nv /' \
  60. -e 's/-o /-O /' \
  61. -e 's/-C - /-c /')
  62. # shellcheck disable=SC2086
  63. eval wget $ARGS
  64. fi
  65. }
  66. # Get the latest version of promptfoo from GitHub
  67. promptfoo_latest_version() {
  68. promptfoo_echo "$(promptfoo_download -s "https://api.github.com/repos/$GITHUB_REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')"
  69. }
  70. # Detect the user's shell profile file
  71. promptfoo_detect_profile() {
  72. if [ "${PROFILE-}" = '/dev/null' ]; then
  73. # The user has specifically requested NOT to have promptfoo touch their profile
  74. return
  75. fi
  76. if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
  77. promptfoo_echo "${PROFILE}"
  78. return
  79. fi
  80. local DETECTED_PROFILE
  81. DETECTED_PROFILE=''
  82. if [ "${SHELL#*bash}" != "$SHELL" ]; then
  83. if [ -f "$HOME/.bashrc" ]; then
  84. DETECTED_PROFILE="$HOME/.bashrc"
  85. elif [ -f "$HOME/.bash_profile" ]; then
  86. DETECTED_PROFILE="$HOME/.bash_profile"
  87. fi
  88. elif [ "${SHELL#*zsh}" != "$SHELL" ]; then
  89. if [ -f "$HOME/.zshrc" ]; then
  90. DETECTED_PROFILE="$HOME/.zshrc"
  91. elif [ -f "$HOME/.zprofile" ]; then
  92. DETECTED_PROFILE="$HOME/.zprofile"
  93. fi
  94. fi
  95. if [ -z "$DETECTED_PROFILE" ]; then
  96. for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zprofile" ".zshrc"; do
  97. if DETECTED_PROFILE="$(promptfoo_try_profile "${HOME}/${EACH_PROFILE}")"; then
  98. break
  99. fi
  100. done
  101. fi
  102. if [ -n "$DETECTED_PROFILE" ]; then
  103. promptfoo_echo "$DETECTED_PROFILE"
  104. fi
  105. }
  106. # Check if a profile file exists and is readable
  107. # Usage: promptfoo_try_profile <profile_path>
  108. promptfoo_try_profile() {
  109. if [ -z "${1-}" ] || [ ! -f "${1}" ]; then
  110. return 1
  111. fi
  112. promptfoo_echo "${1}"
  113. }
  114. # Handle errors and perform cleanup
  115. # Usage: handle_error <error_message>
  116. handle_error() {
  117. promptfoo_echo "Error: $1" >&2
  118. cleanup
  119. exit 1
  120. }
  121. # Clean up temporary files
  122. cleanup() {
  123. rm -rf "$TMP_DIR"
  124. }
  125. # Detect the operating system and architecture
  126. detect_os_arch() {
  127. OS=$(uname -s | tr '[:upper:]' '[:lower:]')
  128. ARCH=$(uname -m)
  129. case $ARCH in
  130. x86_64) ARCH="x64" ;;
  131. aarch64 | arm64) ARCH="arm64" ;;
  132. *) handle_error "Unsupported architecture: $ARCH" ;;
  133. esac
  134. case $OS in
  135. linux) ;;
  136. darwin) OS="macos" ;;
  137. *) handle_error "Unsupported operating system: $OS" ;;
  138. esac
  139. }
  140. # Check for existing installation and prompt for update
  141. check_existing_installation() {
  142. if [ -f "$BIN_DIR/promptfoo" ]; then
  143. read -p "promptfoo is already installed. Do you want to update it? (y/N) " -n 1 -r
  144. promptfoo_echo
  145. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  146. promptfoo_echo "Installation cancelled."
  147. exit 0
  148. fi
  149. fi
  150. }
  151. # Download and install the promptfoo binary
  152. install_promptfoo() {
  153. local VERSION
  154. VERSION=$(promptfoo_latest_version)
  155. local FILENAME="promptfoo-$OS-$ARCH"
  156. local URL="https://github.com/$GITHUB_REPO/releases/download/$VERSION/$FILENAME"
  157. promptfoo_echo "=> Downloading promptfoo $VERSION for $OS-$ARCH..."
  158. promptfoo_download -L "$URL" -o "$TMP_DIR/promptfoo" || handle_error "Failed to download promptfoo"
  159. chmod +x "$TMP_DIR/promptfoo"
  160. promptfoo_echo "=> Installing promptfoo..."
  161. mkdir -p "$INSTALL_DIR"
  162. mv "$TMP_DIR/promptfoo" "$INSTALL_DIR/promptfoo"
  163. if [ -w "$BIN_DIR" ]; then
  164. ln -sf "$INSTALL_DIR/promptfoo" "$BIN_DIR/promptfoo"
  165. ln -sf "$INSTALL_DIR/promptfoo" "$BIN_DIR/pf"
  166. else
  167. sudo ln -sf "$INSTALL_DIR/promptfoo" "$BIN_DIR/promptfoo"
  168. sudo ln -sf "$INSTALL_DIR/promptfoo" "$BIN_DIR/pf"
  169. fi
  170. promptfoo_echo "=> promptfoo has been installed successfully!"
  171. }
  172. # Update PATH in the user's shell profile if necessary
  173. update_path() {
  174. local PROFILE
  175. PROFILE="$(promptfoo_detect_profile)"
  176. local PROFILE_INSTALL_DIR
  177. PROFILE_INSTALL_DIR="$(printf %s "${BIN_DIR}" | sed "s:^$HOME:\$HOME:")"
  178. SOURCE_STR="\\nexport PATH=\"\$PATH:$PROFILE_INSTALL_DIR\" # This adds promptfoo to the PATH\\n"
  179. if [ -z "${PROFILE-}" ]; then
  180. local TRIED_PROFILE
  181. if [ -n "${PROFILE}" ]; then
  182. TRIED_PROFILE="${PROFILE} (as defined in \$PROFILE), "
  183. fi
  184. promptfoo_echo "=> Profile not found. Tried ${TRIED_PROFILE-}~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
  185. promptfoo_echo "=> Create one of them and run this script again"
  186. promptfoo_echo " OR"
  187. promptfoo_echo "=> Append the following lines to the correct file yourself:"
  188. command printf "${SOURCE_STR}"
  189. else
  190. if ! command grep -qc "$BIN_DIR" "$PROFILE"; then
  191. promptfoo_echo "=> Appending promptfoo source string to $PROFILE"
  192. command printf "${SOURCE_STR}" >>"$PROFILE"
  193. else
  194. promptfoo_echo "=> promptfoo source string already in ${PROFILE}"
  195. fi
  196. fi
  197. }
  198. # Main installation process
  199. main() {
  200. detect_os_arch
  201. check_existing_installation
  202. echo "Detected OS: $OS"
  203. echo "Detected ARCH: $ARCH"
  204. echo "Latest version: $(promptfoo_latest_version)"
  205. echo "Download URL: https://github.com/$GITHUB_REPO/releases/download/$(promptfoo_latest_version)/promptfoo-$OS-$ARCH"
  206. install_promptfoo
  207. update_path
  208. cleanup
  209. promptfoo_echo "=> Close and reopen your terminal to start using promptfoo or run the following to use it now:"
  210. command printf "${SOURCE_STR}"
  211. }
  212. # Run the main installation process
  213. main
  214. } # This ensures the entire script is downloaded before execution
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...