-
Notifications
You must be signed in to change notification settings - Fork 9
/
shell-config
91 lines (74 loc) · 2.26 KB
/
shell-config
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
#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.
if [ -z "${__included_shell_config-}" ]; then
__included_shell_config=1
. shell-error
. shell-quote
__shell_config_comment='#'
shell_config_get()
{
[ "$#" -ge 2 ] && [ "$#" -le 3 ] ||
fatal "Usage: shell_config_get file name [delim]"
local file="$1" name="$2" delim="${3-=}"
[ -s "$file" ] ||
return 0
quote_sed_regexp_variable name "$name"
sed -n -e "s/^[[:space:]]*$name$delim//p" -- "$file"
}
shell_config_set()
{
[ "$#" -ge 3 ] && [ "$#" -le 5 ] ||
fatal "Usage: shell_config_set file name value [read-delim [write-delim]]"
local file="$1" name="$2" value="$3" r_delim="${4-=}" w_delim="${5-=}"
local n v nv='' created=''
if [ ! -f "$file" ]; then
if [ ! -w "${file%/*}" ]; then
message "${file%/*}: not writable."
return 1
fi
touch -- "$file" ||
return 1
created=1
fi
if [ -z "$created" ]; then
quote_sed_regexp_variable n "$name"
if v="$(grep -m1 "^[[:space:]]*$n$r_delim" -- "$file" 2>/dev/null)"; then
if [ "${v#*$name$r_delim}" != "$value" ]; then
quote_sed_regexp_variable nv "$w_delim$value"
sed -i -e "s/^[[:space:]]*$n$r_delim.*/$n$nv/" -- "$file"
fi
return
fi
if [ -n "${__shell_config_comment-}" ] &&
v="$(grep -m1 "^[[:space:]]*${__shell_config_comment:-#}[[:space:]]*$n$r_delim" -- "$file" 2>/dev/null)"; then
quote_sed_regexp_variable v "$v"
quote_sed_regexp_variable nv "$w_delim$value"
sed -i -e "s/^$v\$/$n$nv/" -- "$file"
return
fi
fi
printf '%s\n' "$name$w_delim$value" >> "$file"
}
shell_config_del()
{
[ "$#" -ge 2 ] && [ "$#" -le 3 ] ||
fatal "Usage: shell_config_del file name [delim]"
local file="$1" name="$2" delim="${3-=}"
[ -s "$file" ] ||
return 0
quote_sed_regexp_variable name "$name"
sed -i -e "/^[[:space:]]*$name$delim/d" -- "$file"
}
shell_config_comment()
{
[ "$#" -ge 2 ] && [ "$#" -le 3 ] ||
fatal "Usage: shell_config_comment file name [delim]"
local file="$1" name="$2" delim="${3-=}"
[ -s "$file" ] ||
return 0
quote_sed_regexp_variable name "$name"
sed -i -e "s/^[[:space:]]*$name$delim.*/${__shell_config_comment:-#}&/" -- "$file"
}
fi #__included_shell_config