diff --git a/docker/init.sh b/docker/init.sh index 4e2118a6741485145ee981af7e9e0244e280c0e7..3ff7f152e684971e8dd23f9bebb51f91de1e3358 100755 --- a/docker/init.sh +++ b/docker/init.sh @@ -17,6 +17,7 @@ # 2023-12-07 v1.10 <www.axel-hahn.de> simplyfy console command; add php linter # 2024-07-01 v1.11 <www.axel-hahn.de> diff with colored output; suppress errors on port check # 2024-07-19 v1.12 <axel.hahn@unibe.ch> apply shell fixes +# 2024-07-22 v1.13 <axel.hahn@unibe.ch> show info if there is no database container; speedup replacements # ====================================================================== cd "$( dirname "$0" )" || exit 1 @@ -33,7 +34,7 @@ _self=$( basename "$0" ) # git@git-repo.iml.unibe.ch:iml-open-source/docker-php-starterkit.git selfgitrepo="docker-php-starterkit.git" -_version="1.12" +_version="1.13" # ---------------------------------------------------------------------- # FUNCTIONS @@ -169,6 +170,23 @@ function _fix_no-db(){ fi } +# helper functiion to generate replacements using sed +# it loops over all vars in the config file +# used in _generateFiles +function _getreplaces(){ + # loop over vars to make the replacement + grep "^[a-zA-Z]" "$_self.cfg" | while read -r line + do + # echo replacement: $line + mykey=$( echo "$line" | cut -f 1 -d '=' ) + myvalue="$( eval echo \"\$"$mykey"\" )" + + # TODO: multiline values fail here in replacement with sed + echo -e "s#{{$mykey}}#${myvalue}#g" + + done +} + # loop over all files in templates subdir make replacements and generate # a target file. # It skips if @@ -176,14 +194,14 @@ function _fix_no-db(){ # - target file has no updated lines function _generateFiles(){ - # re-read config vars - # shellcheck source=/dev/null . "${_self}.cfg" || exit 1 + params=$( _getreplaces | while read -r line; do echo -n "-e '$line' "; done ) + local _tmpfile=/tmp/newfilecontent$$.tmp h2 "generate files from templates..." - for mytpl in templates/* + time for mytpl in templates/* do # h3 $mytpl local _doReplace=1 @@ -207,19 +225,7 @@ function _generateFiles(){ local _md5; _md5=$( md5sum $_tmpfile | awk '{ print $1 }' ) sed -i "s#{{generator}}#GENERATED BY $_self - template: $mytpl - $_md5#g" $_tmpfile - # loop over vars to make the replacement - grep "^[a-zA-Z]" "$_self.cfg" | while read -r line - do - # echo replacement: $line - mykey=$( echo "$line" | cut -f 1 -d '=' ) - myvalue="$( eval echo \"\$"$mykey"\" )" - - if grep "{{$mykey}}" $_tmpfile >/dev/null; then - - # TODO: multiline values fail here in replacement with sed - sed -i -e "s#{{$mykey}}#${myvalue}#g" $_tmpfile - fi - done + eval sed -i "$params" "$_tmpfile" || exit _fix_no-db $_tmpfile @@ -229,12 +235,12 @@ function _generateFiles(){ mkdir -p "$( dirname ../"$target" )" || exit 2 mv "$_tmpfile" "../$target" || exit 2 echo OK + echo else rm -f $_tmpfile echo "SKIP: $mytpl - Nothing to do." fi fi - echo done } @@ -265,12 +271,26 @@ function _removeGeneratedFiles(){ # show running containers function _showContainers(){ local bLong=$1 + local _out h2 CONTAINERS - if [ -z "$bLong" ]; then + _out=$( if [ -z "$bLong" ]; then docker-compose -p "$APP_NAME" ps else docker ps | grep "$APP_NAME" + fi) + if [ "$( wc -l <<< "$_out" )" -eq 1 ]; then + if [ "$DB_ADD" = "false" ]; then + echo "The web container is <$APP_NAME> is not running. This app has no database container." + else + echo "No container is running for <$APP_NAME>." + fi + else + echo "$_out" + if [ "$DB_ADD" = "false" ]; then + echo "INFO: This app has no database container." + fi fi + } @@ -340,7 +360,7 @@ while true; do if [ -z "$action" ]; then echo - echo -e "\e[32m===== INITIALIZER FOR DOCKER APP [$APP_NAME] v$_version ===== \e[0m\n\r" + echo -e "\e[32m----===###| INITIALIZER FOR DOCKER v$_version | $APP_NAME |###===---\e[0m" _showContainers diff --git "a/docs/10_\360\237\244\224_Why.md" "b/docs/10_\360\237\244\224_Why.md" index 0810c73afeb8ba9f82171d595e17bddf74c3ecc9..3378b4ab42c20a9e20d89c8ef9687afa1900e44d 100644 --- "a/docs/10_\360\237\244\224_Why.md" +++ "b/docs/10_\360\237\244\224_Why.md" @@ -1,4 +1,4 @@ -# What problem does it solve? +## What problem does it solve? I have a few php based projects with or without a database. @@ -15,6 +15,6 @@ The init script: * opens a console * starts php lint -# What problems does it not solve? +## What problems does it not solve? It is a script for a dual container and Apache + PHP and (optional) Mariadb only for a local LAMP dev environment. Nothing else. diff --git "a/docs/20_\342\234\205_Requirements.md" "b/docs/20_\342\234\205_Requirements.md" index 6ce4b93939080f22960daa39b91db2494ab74d98..e196fea9956c1690690acf032e9ae126dc65b62d 100644 --- "a/docs/20_\342\234\205_Requirements.md" +++ "b/docs/20_\342\234\205_Requirements.md" @@ -1,4 +1,4 @@ -## Requirements ## +## Requirements It was tested on Linux only. diff --git "a/docs/30_\360\237\252\204_Init_a_new_project.md" "b/docs/30_\360\237\252\204_Init_a_new_project.md" index aab91c982950d37fc573e78f1b7d695e7d9b9b54..71f4d3ebcf6374cf9a79320560434e9aa856aeb2 100644 --- "a/docs/30_\360\237\252\204_Init_a_new_project.md" +++ "b/docs/30_\360\237\252\204_Init_a_new_project.md" @@ -1,7 +1,7 @@ -## How to use this template ## +## How to use this template -### Create a new project folder ### +### Create a new project folder Copy downloaded and extracted folder to a new name: @@ -41,7 +41,7 @@ The initial file structure looks like this: └── readme.md ``` -### Edit variables and files ### +### Edit variables and files * edit ```./docker/init.sh.cfg``` ... and edit * ```APP_NAME=my_new_app``` @@ -53,7 +53,7 @@ The initial file structure looks like this: * edit php settings in ```./docker/templates/extra-php.ini``` * edit mysql settings in ```./docker/templates/my.cnf``` -### First start ### +### First start * start init script in the docker folder: ```./docker/init.sh``` * use `g` to remove git data of the starterkit @@ -61,13 +61,13 @@ The initial file structure looks like this: * use `t` to generate config files from templates with the settings from ./docker/init.sh.cfg * use `u` to bring up the container (OR run ```docker-compose up -d``` ) -### Edit files ### +### Edit files * If the container was started you can ... * open in your browser http://localhost:8001 to access ```./public_html/``` * edit files with the IDE of your choice -### Remarks ### +### Remarks You can start ./docker/init.sh with a single letter in the interactive menu too, i.e. ```./docker/init.sh u``` to bring up the docker instance. diff --git a/docs/images/main_menu.png b/docs/images/main_menu.png index bc4d1696e5041db153d3da254c2523f2882d24bb..176508c68c3d0cb7a05c4bcbf8c26bc53d4e502a 100644 Binary files a/docs/images/main_menu.png and b/docs/images/main_menu.png differ