Skip to content

Advanced Command Line

fakeyanss edited this page Aug 14, 2022 · 4 revisions

Here are some advanced command line improvements for gitignore.io

Command by @git2samus

function gi() (
    gi_args=()
    for arg; do
        if [[ $arg = -- ]]; then
            curl_args=("${gi_args[@]}")
            gi_args=()
        else
            gi_args+=("$arg")
        fi
    done
    IFS=,
    curl "${curl_args[@]}" http://gitignore.io/api/"${gi_args[*]}"
)

this allows invocations as the previous but also permits passing arguments to curl like: gi --proxy somewhere:8080 -- linux python

Command by @git2samus

if hash curl; then
    curl "${curl_args[@]}" http://gitignore.io/api/"${gi_args[*]}"
elif hash wget; then
    wget -O- "${curl_args[@]}" http://gitignore.io/api/"${gi_args[*]}"
else
    echo "please install curl or wget to run this command" >&2
    exit 1
fi

Adds a check to see if curl or wget is installed.

Command by @GeorgeErickson

function gi { curl http://www.gitignore.io/api/"$(IFS=, ; echo "$*")"; }

Bash one-liner

function gi
  curl -L -s https://www.gitignore.io/api/$argv;
end

Create a file for the function using vim ~/.config/fish/functions/gi.fish and enter the below code

Command from oh-my-zsh

function gi() { curl -sL https://www.gitignore.io/api/$@ ;}

_gitignoreio_get_command_list() {
  curl -sL https://www.gitignore.io/api/list | tr "," "\n"
}

_gitignoreio () {
  compset -P '*,'
  compadd -S '' `_gitignoreio_get_command_list`
}

compdef _gitignoreio gi

If you want to accelerate the completion, cache the gi command list into a local file works.

_gitignoreio_get_command_list() {
    # gi cmd cache file
    cache=~/.config/.gi_cmd_list
    ls cache >/dev/null 2>&1
    if [[ $? == 0 ]]; then
        modify=$(date -j -f %c $(stat -x $cache|grep 'Modify: '|awk -F 'Modify: ' '{print $2}') +%s)
        expire=$(($(date +%s)-$modify))
        # check update once half a month
        if [[ expire > 1296000 ]]; then
            cat $cache
            exit 0
        fi
    fi
    curl -sL https://www.toptal.com/developers/gitignore/api/list | tr "," "\n" > $cache
    cat $cache
}

Provides completion for zsh

Command from @Phoenix09

git config --global alias.ignore '!gi() { IFS=","; curl -L -s "https://www.gitignore.io/api/$*" | tee .gitignore;}; gi'

Improved git alias
tee to .gitignore and accept multiple parameters