Skip to content

Commit 28a61e0

Browse files
authored
Merge pull request #34 from aakb/feature/template-install
Feature/template install
2 parents a461409 + eb7a4c4 commit 28a61e0

File tree

5 files changed

+102
-3
lines changed

5 files changed

+102
-3
lines changed

completion/bash/itkdev-docker-compose-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ _idc_completions()
99
fi
1010

1111
# Keep the suggestions in a local variable
12-
local suggestions=($(compgen -W "url open drush traefik:start traefik:stop traefik:open traefik:url mailhog:open mailhog:url sql:connect sql:port xdebug hosts:insert sync sync:db sync:files images:pull composer" -- "${COMP_WORDS[1]}"))
12+
local suggestions=($(compgen -W "url open drush template:install traefik:start traefik:stop traefik:open traefik:url mailhog:open mailhog:url sql:connect sql:port xdebug hosts:insert sync sync:db sync:files images:pull composer" -- "${COMP_WORDS[1]}"))
1313

1414
COMPREPLY=("${suggestions[@]}")
1515

completion/zsh/_itkdev-docker-compose

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ _itkdev-docker-compose() {
3535
'sync\:files[Sync files base.]' \
3636
'sql\:connect[Print mysql command for connecting to database.]' \
3737
'sql\:port[Display the exposed MariaDB SQL server port.]' \
38+
'template\:install[Install template]' \
3839
'traefik\:start[Start reverse proxy]' \
3940
'traefik\:stop[Stop reverse proxy]' \
4041
'traefik\:open[Open reverse proxy web-interface]' \

scripts/itkdev-docker-compose

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,94 @@ function find_script_dir {
1616
script_dir=$(find_script_dir)
1717
script_path=$script_dir/$(basename "${BASH_SOURCE[0]}")
1818

19+
function template_install {
20+
local force=0
21+
local list=0
22+
local name=""
23+
24+
for var in "$@"
25+
do
26+
case "$var" in
27+
--force)
28+
force=1
29+
;;
30+
--list)
31+
list=1
32+
;;
33+
*)
34+
name="$var"
35+
;;
36+
esac
37+
done
38+
39+
local templates_dir=$script_dir/../templates
40+
local source_dir=$templates_dir/$name/
41+
local target_dir=$PWD
42+
43+
if [ "$list" -eq 1 ]; then
44+
echo Templates:
45+
for name in $templates_dir/*; do
46+
echo " $(basename $name)"
47+
done
48+
exit 0
49+
fi
50+
51+
if [ -z "$name" ]; then
52+
(>&2 echo "${bold}Missing template name${normal}")
53+
exit 1
54+
fi
55+
if [ ! -d $source_dir ]; then
56+
(>&2 echo "${bold}Invalid template name: $name (use --list to show valid template names)${normal}")
57+
exit 1
58+
fi
59+
60+
if [ "$force" -ne 1 ]; then
61+
if [ -e $target_dir/docker-compose.yml ]; then
62+
(>&2 echo "${bold}Target $target_dir/docker-compose.yml already exists (use --force to overwrite)${normal}")
63+
exit 1
64+
fi
65+
if [ -e $target_dir/.docker ]; then
66+
(>&2 echo "${bold}Target $target_dir/.docker already exists (use --force to overwrite)${normal}")
67+
exit 1
68+
fi
69+
fi
70+
71+
read -p "Are you sure you want to install the $name template in $target_dir (y|N)? " -n 1 -r
72+
echo
73+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
74+
exit
75+
fi
76+
77+
echo "${bold}Installing $name template${normal}"
78+
rsync --archive $source_dir $target_dir
79+
80+
local project_name_default=$(basename $target_dir)
81+
82+
# Help user set up a .env file
83+
if [ ! -e $target_dir/.env ]; then
84+
local project_name
85+
local domain
86+
read -p "Project name ($project_name_default)? " -e project_name
87+
if [ -z "$project_name" ]; then
88+
project_name=$project_name_default
89+
fi
90+
read -p "Domain (optional)? " -e domain
91+
cat > $target_dir/.env <<EOF
92+
COMPOSE_PROJECT_NAME=$project_name
93+
COMPOSE_DOMAIN=$domain
94+
EOF
95+
else
96+
cat <<EOF
97+
98+
${bold}Edit .env and make sure that COMPOSE_PROJECT_NAME and, optionally, COMPOSE_DOMAIN is set${normal}, e.g.
99+
100+
COMPOSE_PROJECT_NAME=$project_name_default
101+
COMPOSE_DOMAIN=$project_name_default.local.computer
102+
103+
EOF
104+
fi
105+
}
106+
19107
bold=$(tput bold)
20108
normal=$(tput sgr0)
21109

@@ -58,6 +146,10 @@ Commands:
58146
sql:port
59147
Display the exposed MariaDB SQL server port.
60148
149+
template:install name [--force] [--list]
150+
Install a named docker-composer template in the current
151+
directory.
152+
61153
traefik:start
62154
Start træfik reverse proxy.
63155
@@ -130,6 +222,14 @@ Configuration:
130222
EOF
131223
}
132224

225+
# template:install must be run before we check for existence of
226+
# docker-compose.yml.
227+
if [[ "$#" -gt "0" && "$1" == "template:install" ]]; then
228+
shift
229+
template_install "$@"
230+
exit
231+
fi
232+
133233
# @see https://unix.stackexchange.com/questions/13464/is-there-a-way-to-find-a-file-in-an-inverse-recursive-search/13474
134234
upsearch () {
135235
slashes=${PWD//[^\/]/}

templates/symfony-3/docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ services:
3838
- mariadb
3939
volumes:
4040
- .:/app:delegated
41-
- drush-cache:/root/.drush
4241

4342
nginx:
4443
image: nginx:latest

templates/symfony-4/docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ services:
3838
- mariadb
3939
volumes:
4040
- .:/app:delegated
41-
- drush-cache:/root/.drush
4241

4342
nginx:
4443
image: nginx:latest

0 commit comments

Comments
 (0)