Skip to content

Commit 32830f4

Browse files
authored
♻️💡 imp order of methods and properties and more line comments (#17)
* refactor: imp order of methods and properties and more line comments (#17) Logic is now at base of script making it much easier to work with. Also split up the method sections into groups for better organization. This fixes refactor: organize properties and optional properties better. #3 * 🔖 v1.0.1 Updating version tag in script
1 parent 88a2884 commit 32830f4

File tree

1 file changed

+77
-58
lines changed

1 file changed

+77
-58
lines changed

wp-git-sync.sh

Lines changed: 77 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
#!/bin/sh
22

33
# WordPress SSH Git CI Script
4-
# v1.0.0
4+
# v1.0.1
55

6+
# Set argument parameters on start
67

8+
TOKENUSER="NONE"
9+
TOKEN="NONE"
710

11+
while getopts b:d:g:p:r:t:u: option; do
12+
case "${option}" in
13+
b) BRANCH=${OPTARG};;
14+
d) DEVBRANCH=${OPTARG};;
15+
g) GITREPO=${OPTARG};;
16+
p) GITPATH=${OPTARG};;
17+
r) RUNTYPE=${OPTARG};;
18+
t) TOKEN=${OPTARG};;
19+
u) TOKENUSER=${OPTARG};;
20+
*) RUNTYPE="push";;
21+
esac
22+
done
823

9-
set_defaults() {
10-
if [ -z "$GITPATH" ]; then
11-
print_error_msg "-p is empty. Please provide a path to git directory for repository & try again."
12-
exit 2
13-
fi
14-
if [ -z "$RUNTYPE" ]; then
15-
RUNTYPE='push'
16-
fi
17-
}
24+
# Remote connection methods
1825

19-
is_stashes() {
20-
output=$(git stash list )
21-
case $1 in
22-
check) stashes=0;;
23-
esac
24-
case $output in
25-
*+"stash"*)
26-
stashes=1;;
27-
esac
26+
add_devops_remote() {
27+
git remote add devops "https://${TOKENUSER}:${TOKEN}@${GITREPO}" 2>&1
28+
print_status_msg "Remote devops addded"
29+
git remote -v 2>&1
2830
}
2931

3032
rm_devops_remote() {
@@ -33,12 +35,6 @@ rm_devops_remote() {
3335
git remote -v 2>&1
3436
}
3537

36-
add_devops_remote() {
37-
git remote add devops "https://${TOKENUSER}:${TOKEN}@${GITREPO}" 2>&1
38-
print_status_msg "Remote devops addded"
39-
git remote -v 2>&1
40-
}
41-
4238
add_or_remove_devops() {
4339
output=$(git remote )
4440
case $output in
@@ -59,6 +55,21 @@ add_or_remove_devops() {
5955
esac
6056
}
6157

58+
# Handling changes on WordPress server
59+
60+
## Stashing changes
61+
62+
is_stashes() {
63+
output=$(git stash list )
64+
case $1 in
65+
check) stashes=0;;
66+
esac
67+
case $output in
68+
*+"stash"*)
69+
stashes=1;;
70+
esac
71+
}
72+
6273
git_stash() {
6374
is_stashes "check"
6475
if [ "$stashes" -eq 1 ] ; then
@@ -76,11 +87,24 @@ git_stash() {
7687
return
7788
}
7889

90+
## Commit changes
91+
92+
commit_git() {
93+
git add -A . 2>&1
94+
git commit -m "Server Side Commit
95+
96+
This was commited from SiteGround during release push" 2>&1
97+
}
98+
99+
## Merge changes
100+
79101
merge_from_devops() {
80102
git merge "${DEVBRANCH}" 2>&1
81103
git branch -D "${DEVBRANCH}" 2>&1
82104
}
83105

106+
# Pull from repository methods
107+
84108
pull_from_devops() {
85109
add_or_remove_devops "add"
86110
git_stash "clear"
@@ -97,36 +121,28 @@ pull_from_devops() {
97121
print_status_msg "pull_from_dev function was executed successfully."
98122
}
99123

124+
# Push to repository methods
125+
100126
push_to_devops() {
101-
# code
102127
pull_from_devops
103128
add_or_remove_devops "add"
104129
git push devops "$BRANCH" 2>&1
105130
print_status_msg "push_to_dev function was executed successfully."
106131
add_or_remove_devops "rm"
107132
}
108133

109-
print_status_msg() {
110-
printf "\033[32m%s\n" "$1"
111-
}
112-
113-
print_warning_msg() {
114-
printf "\033[33m%s\n" "$1"
115-
}
116-
117-
print_error_msg() {
118-
printf "\033[31m%s\n" "$1"
119-
}
120-
121-
commit_git() {
122-
git add -A . 2>&1
123-
git commit -m "Server Side Commit
134+
# Utilities
124135

125-
This was commited from SiteGround during release push" 2>&1
136+
set_defaults() {
137+
if [ -z "$GITPATH" ]; then
138+
print_error_msg "-p is empty. Please provide a path to git directory for repository & try again."
139+
exit 2
140+
fi
141+
if [ -z "$RUNTYPE" ]; then
142+
RUNTYPE='push'
143+
fi
126144
}
127145

128-
TOKEN="NONE"
129-
TOKENUSER="NONE"
130146
clean_repository() {
131147
# code
132148
if [ -n "$TOKEN" ] || [ "$TOKEN" != "NONE" ] || [ -n "$TOKENUSER" ] || [ "$TOKENUSER" != "NONE" ]; then
@@ -140,21 +156,24 @@ clean_repository() {
140156
fi
141157
}
142158

159+
## Print messages in colored text
143160

144-
while getopts b:d:g:p:r:t:u: option
145-
do
146-
case "${option}"
147-
in
148-
b) BRANCH=${OPTARG};;
149-
d) DEVBRANCH=${OPTARG};;
150-
g) GITREPO=${OPTARG};;
151-
p) GITPATH=${OPTARG};;
152-
r) RUNTYPE=${OPTARG};;
153-
t) TOKEN=${OPTARG};;
154-
u) TOKENUSER=${OPTARG};;
155-
*) RUNTYPE="push";;
156-
esac
157-
done
161+
### Greeen status message usually signifying success
162+
print_status_msg() {
163+
printf "\033[32m%s\n" "$1"
164+
}
165+
166+
### Yellow message usually signifying a warning message of some kind
167+
print_warning_msg() {
168+
printf "\033[33m%s\n" "$1"
169+
}
170+
171+
### Red message idicate that there was an error an action should be taken
172+
print_error_msg() {
173+
printf "\033[31m%s\n" "$1"
174+
}
175+
176+
# Methods and properties set, now start script logic models
158177

159178
set_defaults
160179

0 commit comments

Comments
 (0)