Skip to content

Instantly share code, notes, and snippets.

@popstas
Last active September 8, 2023 12:20
Show Gist options
  • Save popstas/9755463bf0a463fd4650dec269bbaa0b to your computer and use it in GitHub Desktop.
Save popstas/9755463bf0a463fd4650dec269bbaa0b to your computer and use it in GitHub Desktop.
gptb
#!/bin/bash
# install:
# 1. save it to /usr/local/bin/gptb
# 2. make it executable: chmod +x /usr/local/bin/gptb
# 3. add environment OPENAI_API_KEY=sk-...
#
# usage: POSTFIX="on ubuntu" gptb what you need for from bash
set -eu
PREFIX="${PREFIX:-how to}"
POSTFIX="${POSTFIX:-in bash, one-liner}"
out_file=$(mktemp -t gptb-XXXX)
trap 'rm -f "$out_file"' EXIT
p="$PREFIX $@ $POSTFIX"
echo "$p"
npx chatgpt "$p" | tee -a "$out_file"
cmd="$(cat $out_file | grep '```' -A1 | sed -n 2p)"
echo -e "\033[0;37m${cmd}\033[0m"
while true; do
read -p "Run this command? (y/n) " yn
case $yn in
[Yy]* ) eval "$cmd"; break;;
[Nn]* ) echo "No, exiting..."; exit;;
* ) echo "Please answer yes or no.";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment