TekWizely / bash-tpl

A smart, lightweight shell script templating engine, written in Bash
MIT License
65 stars 4 forks source link

a conditional .INCLUDE throws error when file does not exist #18

Closed dkebler closed 3 months ago

dkebler commented 1 year ago
% if [[ -f  "$BUILD_SRC/Dockerfile" ]]; then
 .INCLUDE "$BUILD_SRC/Dockerfile"
% fi

if the file "$BUILD_SRC/Dockerfile" does not exist then the .INCLUDE statement is ignored BUT bash-tpl STILL seems to want to check that the .INCLUDE file exists thus throwing an error. This is unexpected.

File not found: '/data/Hacking/computing/docker/uci-docker-nextcloud/src/Dockerfile'
/dev/fd/63: line 142: syntax error near unexpected token `fi'
/dev/fd/63: line 142: `fi'

if "$BUILD_SRC/Dockerfile" exists it all works as expected.

TekWizely commented 1 year ago

Hi @dkebler ,

Just to confirm, the .INCLUDE directive is a 'compile-time' feature, but the % directive is a runtime feature. i.e the commands in % lines are not executed until you run the script generated by the bash-tpl command. But, by that time, the .INCLUDE has already happened (and failed in your case)

I could see a possible new feature to make the include file optional, maybe something like:

.INCLUDE? "include.tpl"

I'll grind on that some more, but in the meantime:

Q: Does your "$BUILD_SRC/Dockerfile" file include bash-tpl template commands?

If YES, then what you might do is modify your build script to compile it separately and then turn the .INCLUDE into a source to be executed at runtime:

If NO, i.e. your Dockerfile is just a pure text file (not templated), then you can turn your .INCLUDE into a cat to be executed at runtime.

Let me know if either of those could work for you for now, and I'll think about how to make the include accept an optional flag for times when the included file may not exist.

-TW

PS: See the discussions here (issue #16) as it contains some overlap with this discussion. It also contains a starter makefile which could be useful for setting up a multi-file setup.

TekWizely commented 1 year ago

@dkebler pls lemme know if you get a chance to read my msg - I'd like to continue the discussion.

-TW

dkebler commented 12 months ago

finally gettting back to this.

My workaround was simply to make temporary file with a comment therein if it doesn't exist.

Not ideal but it works.

#!/bin/bash 
echo "------------ creating Dockfile from template in Dockerfile.d -------------"
[[ ! -f $BDIR/.src/Dockerfile ]] && echo "#dummy file" > $BDIR/.src/Dockerfile
[[ -f $APPEND_BUILD_ENV ]] && source "$APPEND_BUILD_ENV" && echo using $APPEND_BUILD_ENV when building Dockerfile && cat $APPEND_BUILD_ENV && echo -e "\n-----"
pushd $(dirname "$(realpath "$BASH_SOURCE")") > /dev/null || return 1
source <(../lib/bash-tpl Dockerfile.tpl ) | grep -v '^# ' > ../Dockerfile
echo "done ------- creating Dockfile from template in Dockerfile.d -------------"
popd > /dev/null || return 2

This also means I can remove the conditional in my template, so now just

# appends any additional custom Dockerfile code, currently causes error
.INCLUDE "$BDIR/.src/Dockerfile"
dkebler commented 12 months ago

In this case bash-tpl is not generating a bash script to be run later but a Dockerfile which is more a less a "static" file (i.e. can't do a "cat" later) . To include this file was simply a way to give the user a way to inject some additional custom lines into the Dockerfile so NO it doesn't have any templating code but NO I can not use cat as you suggest.