@@ -16,6 +16,94 @@ function find_script_dir {
1616script_dir=$( find_script_dir)
1717script_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+
19107bold=$( tput bold)
20108normal=$( 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:
130222EOF
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
134234upsearch () {
135235 slashes=${PWD// [^\/]/ }
0 commit comments