-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitignoreio
143 lines (133 loc) · 4.79 KB
/
.gitignoreio
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
140
141
142
143
function gi() {
local URL="https://www.toptal.com/developers/gitignore/api"
local EXTRA_LINES=("*.DS_Store" ".env")
local EXTRA_LINES_COMMENT="# User Config"
local DB_FILE="$HOME/.config/gitignoreio/database"
local DAYS_BEFORE_AUTO_UPDATE=1
if [ ! -f "$DB_FILE" ]; then
mkdir -p "$(dirname "$DB_FILE")"
curl -sL $URL/list | tr ',' '\n' > "$DB_FILE"
echo "Local database created at $DB_FILE."
else
local LAST_MODIFIED=$(stat -f "%m" "$DB_FILE")
local CURRENT_TIME=$(date +%s)
local TIME_DIFF=$((CURRENT_TIME - LAST_MODIFIED))
if [ $TIME_DIFF -gt $((DAYS_BEFORE_AUTO_UPDATE * 24 * 60 * 60)) ]; then
curl -sL $URL/list | tr ',' '\n' > "$DB_FILE"
echo "Local database updated."
fi
fi
if [ -z "$1" ] || [ "$1" == "help" ]; then
echo "gitignore.io help:"
echo " help - shows this help"
echo " update - force an update of the project types database"
echo " list - lists project types"
echo " search :string: - fuzzy find"
echo " base - create a .gitignore with only the values from EXTRA_LINES"
echo " :type: - creates .gitignore file for a project"
return
fi
if [ "$1" == "update" ]; then
curl -sL $URL/list | tr ',' '\n' > "$DB_FILE"
echo "Local database updated."
return
fi
if [ "$1" == "base" ]; then
echo $EXTRA_LINES_COMMENT >> .gitignore
for line in "${EXTRA_LINES[@]}"; do
echo $line >> .gitignore
done
echo ".gitignore file created."
fi
if [ "$1" == "list" ]; then
IFS=',' read -ra LIST <<< "$(cat "$DB_FILE" | tr '\n' ',')"
for i in "${LIST[@]}"; do
echo "$i"
done
unset LIST
fi
if [ "$1" == "search" ] && [ -n "$2" ]; then
grep -iE "$2" "$DB_FILE"
fi
if [ "$1" != "list" ] && [ "$1" != "search" ] && [ "$1" != "base" ]; then
response=$(grep -i "^$1$" "$DB_FILE")
if [ -z "$response" ]; then
echo "Error: Type does not exist."
return
fi
if [ -f .gitignore ]; then
echo "HEY $(whoami)!, .gitignore exists already! (o)verwrite, (a)ppend, (q)uit?"
read -r action
case $action in
[oO]* )
curl -sL "$URL/$response" > .gitignore
echo $EXTRA_LINES_COMMENT >> .gitignore
for line in "${EXTRA_LINES[@]}"; do
echo $line >> .gitignore
done
echo ".gitignore file created."
;;
[aA]* )
curl -sL "$URL/$response" >> .gitignore
echo $EXTRA_LINES_COMMENT >> .gitignore
for line in "${EXTRA_LINES[@]}"; do
echo $line >> .gitignore
done
echo ".gitignore file created."
;;
[qQ]* )
return
;;
* )
echo "Invalid. Exit."
return
;;
esac
else
curl -sL "$URL/$response" > .gitignore
echo $EXTRA_LINES_COMMENT >> .gitignore
for line in "${EXTRA_LINES[@]}"; do
echo $line >> .gitignore
done
echo ".gitignore file created."
fi
fi
}
function git() {
case "$1" in
"init")
command git init
echo "Do you want to create a .gitignore file? (y/n)"
read -r answer
case "$answer" in
"y")
echo "What kind of project are you creating? (Enter . to search for a project type)"
while true; do
read -r project_type
case "$project_type" in
".")
echo "Search term:"
read -r search_term
gi search $search_term
echo "Project type:"
;;
*)
if gi $project_type; then
break
else
echo "Invalid project type. Please enter a valid project type or . to search for a project type:"
fi
;;
esac
done
;;
*)
echo "Skipping .gitignore creation."
;;
esac
;;
*)
command git "$@"
;;
esac
}