Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ubuntupunk/794d840604b7f2f2a7f4d0b2c2222a0a to your computer and use it in GitHub Desktop.
Save ubuntupunk/794d840604b7f2f2a7f4d0b2c2222a0a to your computer and use it in GitHub Desktop.
gi gitignore function
gi() {
local query="$@"
local url="https://www.toptal.com/developers/gitignore/api/$query"
local result
# Fetch the .gitignore template
result=$(curl -sL "$url" 2>&1)
if [[ $? -ne 0 ]]; then
echo "Error: Failed to fetch data from $url (network or curl issue)."
return 1
fi
# Check for API error response
if [[ "$result" =~ [Ee][Rr][Rr][Oo][Rr] || "$result" =~ "Invalid template" ]]; then
echo "Query '$query' has no match. See possible templates with 'gi list'."
return 1
fi
# Handle 'list' command
if [[ "$1" == "list" ]]; then
echo "$result"
return 0
fi
# Clean up the result (remove "Created by" comment)
result=$(echo "$result" | grep -v "# Created by https://www.toptal.com/developers/gitignore")
# Check if .gitignore exists
if [[ -f .gitignore ]]; then
echo "Appending to existing .gitignore for query: $query"
echo "$result" >> .gitignore
else
echo "Creating new .gitignore for query: $query"
echo "$result" > .gitignore
fi
echo "Successfully added .gitignore entries for '$query'."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment