generated from opencodeco/ctl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hfctl
executable file
Β·217 lines (184 loc) Β· 6.38 KB
/
hfctl
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env bash
PHP=8.2
SWOOLE=5.1
HFCID=runtime/hfctl.cid
if [ -e .hfctl ] && [ -s .hfctl ]; then
export $(cat .hfctl | sed 's/#.*//g' | xargs)
fi
IMAGE=${IMAGE:-opencodeco/hfctl:php$PHP-swoole$SWOOLE}
version() { # Displays image tag
echo -e "image: \033[1;32m$IMAGE\033[0m"
}
install() { # Installs `hfctl`, `hf` and `hyperf` in your system
HFCTL_DIR=$(dirname $(realpath $0))
sudo ln -sf "${HFCTL_DIR}/hfctl" /usr/local/bin/hfctl
sudo ln -sf "${HFCTL_DIR}/hfctl" /usr/local/bin/hf
sudo ln -sf "${HFCTL_DIR}/hfctl" /usr/local/bin/hyperf
}
self-update() { # Updates `hfctl` to the latest version
HFCTL_DIR=$(dirname $(realpath $0))
cd $HFCTL_DIR
git pull origin main
}
init() { # Initializes `.hfctl` config file
if [ -f .hfctl ]; then
echo -e "\033[0;31m.hfctl file already exists\033[0m"
exit 1
fi
echo "IMAGE=${PWD##*/}" > .hfctl
}
create() { # Creates a new Hyperf project
_docker-run ${HFTTY:--it} composer create-project hyperf/hyperf-skeleton:dev-master $1
sudo chown -R $UID:$UID $1
}
composer() { # Runs Composer commands
_docker-run ${HFTTY:--it} composer $@
}
start() { # Starts the Hyperf server (default port is 9501)
pre-check
PORT=${1:-9501}
if [ -n "$(docker ps --format 'table {{.Ports}}' | grep $PORT)" ]; then
echo -e "\033[0;31mPort $PORT is already in use by another container\033[0m"
exit 1
fi
echo -e "Container ID:" $(_docker-run "-dp$PORT:9501 --cidfile $HFCID" php bin/hyperf.php ${2:-start})
echo -e "\033[1;32mHyperf server started at $PORT\033[0m"
echo -e "http://localhost:$PORT"
}
watch() { # Starts the Hyperf watcher
pre-check
if [[ "$(composer show hyperf/watcher 2>&1)" == *"\"hyperf/watcher\" not found"* ]]; then
_docker-run ${HFTTY:--it} composer require --dev hyperf/watcher
fi
start ${1:-9501} server:watch
}
stop() { # Stops the Hyperf server
pre-check cid
docker stop $(cat $HFCID)
rm $HFCID
}
restart() { # Restarts the Hyperf server
pre-check cid
echo -e "Container ID:" $(docker restart $(cat $HFCID))
}
console() { # Runs Hyperf console commands (`php bin/hyperf.php <command>`)
pre-check
_docker-run ${HFTTY:--it} php bin/hyperf.php $@
}
logs() { # Shows the Hyperf container logs (use `-f` or `--follow` to follow logs)
DC=$(docker compose ps --format 'json {{.Name}}')
if [[ -n $DC ]]; then
docker compose logs $@
else
pre-check cid
docker logs $@ $(cat $HFCID)
fi
}
up() { # Runs Docker Compose up
docker compose up -d
}
down() { # Runs Docker Compose down
docker compose down --remove-orphans
}
test() { # Runs PHPUnit tests
_docker-run ${HFTTY:--it} php vendor/bin/co-phpunit --prepend test/bootstrap.php $@
}
coverage() { # Runs PHPUnit tests with HTML coverage
PORT=${1:-8090}
test --coverage-html runtime/coverage
_docker-run "-p$PORT:$PORT" php -S 0.0.0.0:$PORT -t runtime/coverage
}
lint() { # Runs PHP Coding Standards Fixer
_docker-run ${HFTTY:--it} php vendor/bin/php-cs-fixer fix $@
}
analyse() { # Runs PHPStan analyses
_docker-run ${HFTTY:--it} php vendor/bin/phpstan analyse ${@:--l5} app
}
sonar() { # Runs SonarQube analyses
if [ -z "$SONAR_TOKEN" ]; then
echo -e "\033[0;31mSONAR_TOKEN is not set\033[0m"
exit 1
fi
docker run --rm -v ./:/usr/src -e SONAR_TOKEN="$SONAR_TOKEN" sonarsource/sonar-scanner-cli -Dsonar.branch.name=$(git branch --show-current) $@
}
build() { # Builds the Hyperf image
docker build \
--build-arg PHP=$PHP \
--build-arg SWOOLE=$SWOOLE \
--build-arg COMPOSER_AUTH="$COMPOSER_AUTH" \
$@ \
-t $IMAGE .
}
push() { # Pushes the Hyperf image to Docker Hub
docker push $IMAGE
}
sh() { # Runs a shell in the Hyperf container
_docker-run "${HFTTY:--it} $@" sh
}
exec() { # Runs a command in the Hyperf container
_docker-run ${HFTTY:--it} $@
}
pre-check() { # Checks if the current directory is a Hyperf project
if ! [ -f bin/hyperf.php ]; then
echo -e "\033[0;31mAre you in a Hyperf project?\033[0m"
exit 1
fi
if [[ $1 == cid ]]; then
if ! [ -f $HFCID ]; then
echo -e "\033[0;31mThe Hyperf server is not running\033[0m"
exit 1
fi
fi
}
_docker-run() {
docker run --rm -w /opt -v ./:/opt $1 --entrypoint $2 $IMAGE ${@:3}
}
help() { # Shows this help message
echo -e " "
echo -e " "
echo -e " ,---, ___ ,--, "
echo -e ",--.' | .--., ,--.'|_ ,--.'| "
echo -e "| | : ,--.' \ | | :,'| | : "
echo -e ": : : | | /\/ : : ' :: : ' "
echo -e ": | |,--.: : : ,---. .;__,' / | ' | "
echo -e "| : ' |: | |-, / \| | | ' | | "
echo -e "| | /' :| : :/| / / ':__,'| : | | : "
echo -e "' : | | || | .'. ' / ' : |__' : |__ "
echo -e "| | ' | :' : ' ' ; :__ | | '.'| | '.'| "
echo -e "| : :_:,'| | | ' | '.'| ; : ; : ; "
echo -e "| | ,' | : \ | : : | , /| , / "
echo -e "\`--'' | |,' \ \ / ---\`-' ---\`-'"
echo -e " \`--' \`----' "
echo -e " "
echo -e $(version)
echo -e ""
echo -e "\033[1;33mUsage:\033[0m"
echo -e " hfctl <command> [arguments]"
echo -e ""
echo -e "\033[1;33mCommands:\033[0m"
for fn in $(declare -F | awk '{print $3}'); do
if [[ $fn == _* ]]; then continue; fi
echo -e " \033[1;32m$fn\033[0m\t"$(grep -E "^$fn\(\)\s\{\s+\#" $0 | sed 's/^.*#//g')
done | column -t -s $'\t'
echo -e ""
}
_readme() {
HEADER="| Command | Description |\n| --- | --- |"
COMMANDS=$(
for fn in $(declare -F | awk '{print $3}'); do
if [[ $fn == _* ]]; then continue; fi
echo -n "| \`$fn\` |" $(grep -E "^$fn\(\)\s\{\s+\#" $0 | sed 's/^.*#//g') "|\n"
done
)
START="### Commands"
END="<!-- Commands -->"
awk -v start="$START" -v end="$END" -v table="$HEADER\n$COMMANDS$END" '
$0 ~ start { found = 1; print; next }
found && $0 ~ end { print table; found = 0; next }
!found { print }
' "README.md" > README.md.tmp
mv README.md.tmp README.md
}
CMD=${1:-help}
ARGS=${@:2}
$CMD $ARGS