Skip to content

Commit

Permalink
Move set-config function to script
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Nov 2, 2017
1 parent d0cec2d commit 6b06012
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
13 changes: 13 additions & 0 deletions bin/set-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

KEY="$1"
VALUE="$2"
FILE="$3"

touch "$FILE"

if grep -q "$1=" "$FILE"; then
sed "s@$KEY=.*@$KEY=\"$VALUE\"@" -i "$FILE"
else
echo "export $KEY=\"$VALUE\"" >> "$FILE"
fi
14 changes: 0 additions & 14 deletions system/.function
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ prepend-path() {
[ -d $1 ] && PATH="$1:$PATH"
}

# Update config file

set-config() {
local KEY="$1"
local VALUE="$2"
local FILE="$3"
touch "$FILE"
if grep -q "$1=" "$FILE"; then
sed "s@$KEY=.*@$KEY=\"$VALUE\"@" -i "$FILE"
else
echo "export $KEY=$VALUE" >> "$FILE"
fi
}

# Show 256 TERM colors

colors() {
Expand Down
10 changes: 10 additions & 0 deletions test/bin.bats
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@
EXPECTED=$'{ "x": 1 }'
[ "$ACTUAL" = "$EXPECTED" ]
}

@test "set-config" {
run set-config KEY_A VALUE_1 MYFILE
run set-config KEY_B VALUE_2 MYFILE
run set-config KEY_A VALUE_3 MYFILE
ACTUAL=`cat MYFILE`
EXPECTED=$'export KEY_A="VALUE_3"\nexport KEY_B="VALUE_2"'
[ "$ACTUAL" = "$EXPECTED" ]
run rm MYFILE
}

0 comments on commit 6b06012

Please sign in to comment.