-
Notifications
You must be signed in to change notification settings - Fork 6
/
gitignore.io.sh
executable file
·139 lines (120 loc) · 4.38 KB
/
gitignore.io.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
# =============================================================================
# gitignore.io.sh - simple gitignore.io shell wrapper
# https://github.com/martinec/gitignore.io.sh
# =============================================================================
# Copyright (C) 2014
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#
# Cristian Martinez <martinec@scitify.com> (martinec)
#
# =============================================================================
# =============================================================================
# script tools
# =============================================================================
WRAPPER_TOOL_CURL="curl"
WRAPPER_TOOL_API="https://www.gitignore.io/api"
# =============================================================================
# exit with critical error
# =============================================================================
function die_with_critical_error(){
# send params to stderr
echo "$@" >&2
# exit
exit 1;
}
# =============================================================================
# gitignore.io function caller
# =============================================================================
function gitignore () {
local __output_variable=$1
shift
# gitignore.io.sh api params
api_params="$*"
# curl command arguments
curl_args="-s $WRAPPER_TOOL_API/$api_params"
# build command line
command_line=( $WRAPPER_TOOL_CURL $curl_args )
# command output
local command_output
{
# execute command
command_output=$(eval "${command_line[@]}")
# save return code
exit_status=$?
} > /dev/null
if [ $exit_status -ne 0 ]; then
die_with_critical_error "Aborting" "$WRAPPER_TOOL_CURL failed! " \
"exit($exit_status)."
fi
if [[ "$__output_variable" ]]; then
# shellcheck disable=SC2140
eval "$__output_variable"="\$command_output"
else
echo "$command_output"
fi
}
# =============================================================================
# test $WRAPPER_TOOL_CURL param
# =============================================================================
# test if curl param isn't empty
if [ ! "$WRAPPER_TOOL_CURL" ]; then
die_with_critical_error "Aborting" "Trying to execute empty command"
fi
# =============================================================================
# test if curl exists
# =============================================================================
command -v $WRAPPER_TOOL_CURL > /dev/null || \
{
die_with_critical_error "Aborting" "$WRAPPER_TOOL_CURL is required "\
"but it's not installed"
}
# =============================================================================
# main
# =============================================================================
script_name=$(basename "$0")
if [ ! -f .gitignore.in ]; then
if [ "$#" -ne 1 ]; then
{
echo "Usage: ./$script_name [list|template,...]"
echo " e.g ./$script_name list"
echo " ./$script_name c++,linux"
} >&2
exit 1
fi
ignore_list="$*"
else
if [ "$#" -ne 0 ]; then
{
echo "Usage: ./$script_name > .gitignore"
} >&2
exit 1
fi
ignore_list=$(grep '# ignore_list=' .gitignore.in | sed -e 's/# ignore_list=//g')
fi
gitignore_io=""
gitignore gitignore_io "$ignore_list"
if [ ! -f .gitignore.in ]; then
if [ "$1" != "list" ]; then
echo "# gitignore.io.sh - a simple gitignore.io shell wrapper"
echo "# https://github.com/martinec/gitignore.io.sh"
echo "# ./$script_name $ignore_list"
fi
echo "$gitignore_io"
else
# expand the ${gitignore_io} variable inside the gitignore_in file
eval "echo \"$(cat .gitignore.in)\""
fi