Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update/improve gi command #20

Closed
git2samus opened this issue Nov 23, 2013 · 8 comments
Closed

update/improve gi command #20

git2samus opened this issue Nov 23, 2013 · 8 comments

Comments

@git2samus
Copy link

the gi command is currently implemented as:

function gi() {
    curl http://gitignore.io/api/$@
}

which works by invoking it as gi linux,java however it could be reimplemented as:

function gi() (
    IFS=,
    curl http://gitignore.io/api/"$*"
)

that would allow calling it as gi python java which is more natural.

a second improvement would be to implement it like this:

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

also notice the keyword function isn't strictly necessary if you write gi() as the definition.

@joeblau
Copy link
Collaborator

joeblau commented Nov 24, 2013

Those are all great suggestions. Which OS and Shell are you using? I'll have to test it across a few different platforms to make sure I can roll it into everything.

@git2samus
Copy link
Author

I use Bash and Linux mostly.

@git2samus
Copy link
Author

btw any reason you use curl instead of wget?

@joeblau
Copy link
Collaborator

joeblau commented Jan 9, 2014

wget is not installed on OSX by default. That would mean that users would have to brew install wget to run the command as listed.

In your first comment what does IFS=, do?

@git2samus
Copy link
Author

wget is not installed on OSX by default. That would mean that users would have to brew install wget to run the command as listed.

the same goes for curl in Linux, maybe we could add a check to see which one is installed? something like:

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

In your first comment what does IFS=, do?

IFS is a variable bash uses in several situations, in this case I use it so when the "${gi_args[*]}" array gets expanded bash doesn't separate each term with a space but use a comma instead.

notice that in order to do this I used [*] and not [@] also notice the entire function is enclosed in parenthesis and not braces, this creates a subshell when it's run and that prevents the modification of IFS to leak to the user's environment.

there's other ways to prevent that but this works fine.

@joeblau
Copy link
Collaborator

joeblau commented Jan 10, 2014

Thanks for the explanation. I feel like I need an advanced section to the site to explain some of these options. My goal was to keep the installation script as simple as possible, but you're showing that there are definitely a lot of use cases that I did not take into account. When I get a few hours, I'll have to try these out and see what would be simplest to implement

Thanks a lot of your input, it's been very helpful.

@GeorgeErickson
Copy link

If you want a one line version for bash.

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

@git2samus
Copy link
Author

that's a good one, equivalent to the first form proposed (the one without additional arguments for curl/wget)

the command substitution should be quoted tho, otherwise it might break if there's a whitespace literal on any argument, which is unlikely but should be covered I guess

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

@joeblau joeblau closed this as completed in c0825e1 Apr 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants