-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.sh
executable file
·110 lines (95 loc) · 2.46 KB
/
prompt.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
OPTIONS="hius:a"
function Help() {
cat <<EOF
USAGE:
./<scriptname> [-$OPTIONS]
DESCRIPTION:
Bash prompt configuration tool for ci/cd gurus
OPTIONS:
-h display this help message
-i install
-u uninstall
-s set "key=value" in nearest config file
EOF
}
function set-template() {
case "$1" in
LONG)
PS1='\$(_CYAN)[\\\\u: \\\\W]\$(git-branch)\\n\$(_END)\$(wf-get name):\$(wf-get conclusion)\\n$'
;;
SHORT)
PS1='\$(_CYAN)[\\\\u: \\\\W]\$(git-branch):\$(_END)\$(wf-get status)\\n\\\\$'
;;
esac
sed -i -E "s/(\"PS1\":) \"\"/\1 \"$PS1\"/" $HOME/.cool-prompt/config.json
}
function Init() {
[ -d ~/.cool-prompt ] && Uninstall
[ ! "$OWNER" ] && OWNER="snippets-n-memes"
[ ! "$REPO" ] && REPO="cool-prompt"
[ ! "$WF_NAME" ] && WF_NAME="Sample Workflow"
[ ! "$WF_HOST" ] && WF_HOST="github"
# [ ! "$PROJECT_ID" ] && PROJECT_ID="36257401"
mkdir ~/.cool-prompt
touch ~/.cool-prompt/execution-log
cat <<EOF > ~/.cool-prompt/config.json
{
"workflows": [
{
"OWNER": "$OWNER",
"REPO": "$REPO",
"WF_NAME": "$WF_NAME",
"HOST": "$WF_HOST",
"URL": ""
}
],
"PS1": ""
}
EOF
. bashrc-functions.sh
set-template SHORT
cp fetch.sh ~/.cool-prompt/
cp bashrc-functions.sh ~/.cool-prompt/
crontab -l 2>/dev/null >/tmp/temp-crontab
echo '* * * * * . $HOME/.bashrc; bash --login $HOME/.cool-prompt/fetch.sh' >> /tmp/temp-crontab
crontab /tmp/temp-crontab
rm /tmp/temp-crontab
echo "source ~/.cool-prompt/bashrc-functions.sh" >> /tmp/.bashrc
cat ~/.bashrc >> /tmp/.bashrc
echo 'export PS1=$(get-config PS1)' >> /tmp/.bashrc
mv /tmp/.bashrc ~/.bashrc
}
function Uninstall() {
sed -i '/source ~\/.cool-prompt\/bashrc-functions\.sh/d' ~/.bashrc
sed -i '/export PS1=$(get-config PS1)/d' ~/.bashrc
rm -rf ~/.cool-prompt/
crontab -l | grep -v ".cool-prompt/fetch.sh" > /tmp/temp-crontab
crontab /tmp/temp-crontab
rm /tmp/temp-crontab
}
function add-config() {
mkdir .cool-prompt
cat <<EOF > .cool-prompt/config.json
{
"workflows": [
{
"OWNER": "$OWNER",
"REPO": "$REPO",
"WF_NAME": "$WF_NAME",
"HOST": "$WF_HOST",
"URL": ""
}
]
}
EOF
}
while getopts "$OPTIONS" option; do
case "${option}" in
h) Help ;;
u) Uninstall ;;
i) Init && echo ".bashrc configured" ;;
s) set-config ${OPTARG%%=*} ${OPTARG##*=} ;;
a) add-config ;;
?) echo "USAGE: ./<scriptname> [-$OPTIONS]" ;;
esac
done