Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions add-openai-api-key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

OPENAI_API_KEY=""
# Check if the OPENAI_API_KEY is already in the config file
if ! grep -qF "OPENAI_API_KEY" "$HOME/.happycommit/config.toml"; then
# Read the OPENAI_API_KEY from the user
printf "Please enter your OPENAI_API_KEY (you can get it at https://beta.openai.com/account/api-keys):\nNote: Your input will not be shown on the screen.\n> "
read -rs OPENAI_API_KEY
printf "\n"
# Add the OPENAI_API_KEY to the config.toml file
printf "OPENAI_API_KEY = \"%s\"\n" "$OPENAI_API_KEY" >> "$HOME/.happycommit/config.toml"
else
printf "OPENAI_API_KEY already exists in $HOME/.happycommit/config.toml\n"
printf "Would you like to update it? (y/n) "
read -r update
if [ "$update" = "y" ]; then
# TODO: Remove code duplication
printf "Please enter your OPENAI_API_KEY (you can get it at https://beta.openai.com/account/api-keys):\nNote: Your input will not be shown on the screen.\n> "
read -rs OPENAI_API_KEY
printf "\n"
# Remove the previous OPENAI_API_KEY from the config file
if [ "$machine" = "Mac" ]; then
sed -i '' '/OPENAI_API_KEY/d' "$HOME/.happycommit/config.toml"
else
sed -i '/OPENAI_API_KEY/d' "$HOME/.happycommit/config.toml"
fi
# Add the OPENAI_API_KEY to the config file
printf "OPENAI_API_KEY = \"%s\"\n" "$OPENAI_API_KEY" >> "$HOME/.happycommit/config.toml"
fi
fi

printf "API Key added successfully. Happy committing!\n"
34 changes: 4 additions & 30 deletions install-commit-gpt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,8 @@ if [ ! -f "$HOME/.happycommit/config.toml" ]; then
touch "$HOME/.happycommit/config.toml"
fi

OPENAI_API_KEY=""
# Check if the OPENAI_API_KEY is already in the config file
if ! grep -qF "OPENAI_API_KEY" "$HOME/.happycommit/config.toml"; then
# Read the OPENAI_API_KEY from the user
printf "Please enter your OPENAI_API_KEY (you can get it at https://beta.openai.com/account/api-keys):\nNote: Your input will not be shown on the screen.\n> "
read -rs OPENAI_API_KEY
printf "\n"
# Add the OPENAI_API_KEY to the config.toml file
printf "OPENAI_API_KEY = \"%s\"\n" "$OPENAI_API_KEY" >> "$HOME/.happycommit/config.toml"
else
printf "OPENAI_API_KEY already exists in $HOME/.happycommit/config.toml\n"
printf "Would you like to update it? (y/n) "
read -r update
if [ "$update" = "y" ]; then
# TODO: Remove code duplication
printf "Please enter your OPENAI_API_KEY (you can get it at https://beta.openai.com/account/api-keys):\nNote: Your input will not be shown on the screen.\n> "
read -rs OPENAI_API_KEY
printf "\n"
# Remove the previous OPENAI_API_KEY from the config file
if [ "$machine" = "Mac" ]; then
sed -i '' '/OPENAI_API_KEY/d' "$HOME/.happycommit/config.toml"
else
sed -i '/OPENAI_API_KEY/d' "$HOME/.happycommit/config.toml"
fi
# Add the OPENAI_API_KEY to the config file
printf "OPENAI_API_KEY = \"%s\"\n" "$OPENAI_API_KEY" >> "$HOME/.happycommit/config.toml"
fi
fi

# Done!
printf "git commit-gpt installed successfully. You can now use 'git commit-gpt' to run happycommit.\n"
printf "git commit-gpt installed successfully.
printf "You should now run 'add-openai-api-key' to add your OpenAI API key to happycommit's config file.\n"
printf "This script can be found in jackbackes/happycommit on github.\n"
You can now use 'git commit-gpt' to run happycommit.\n"
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,16 +230,19 @@ fn load_api_key() -> Result<String, Box<dyn std::error::Error>> {
let config_path = dirs::home_dir().unwrap().join(".happycommit/config.toml");
let config = std::fs::read_to_string(config_path)?;
let config: toml::Value = toml::from_str(&config)?;
let openai_api_key = config.get("openai_api_key");
let openai_api_key = config.get("OPENAI_API_KEY");
if openai_api_key.is_none() {
return Err("OPENAI_API_KEY not set in ~/.happycommit/config.toml".into());
}
Ok(openai_api_key.unwrap().to_string())
let result = openai_api_key.unwrap().to_string();
// strip quotes
let result = result.replace("\"", "");
Ok(result)
};
let dotenv_checker = || -> Result<String, Box<dyn std::error::Error>> {
dotenv::dotenv().ok();
let openai_api_key =
dotenv::var("OPENAI_API_KEY").expect("OPENAI_API_KEY not set in .env file");
dotenv::var("OPENAI_API_KEY")?;
Ok(openai_api_key)
};

Expand Down