Skip to content

Conversation

@DannyBen
Copy link
Member

As discussed in #216

@DannyBen DannyBen changed the title Add support for generating tab-indented scripts using BASHLY_TAB_INDENT Add support for generating tab-indented scripts using BASHLY_TAB_INDENT Apr 28, 2022
@hojerst
Copy link

hojerst commented Apr 28, 2022

Just tested this and seems to be working for my specific usecase. However as documented it currently doesn't support tab/space indention in the heredoc itself. Note: Depending on the usecase using U+3000 might not be a feasble solution.

Consider a bash script or other template that you want to keep inline. This specific usecase doesn't allow to use unicode chars for spacing as it is not supported by the underlying language.

That beeing said, although the heredoc support is not perfect yet, it definitily has improved with this PR

@DannyBen
Copy link
Member Author

Thanks for testing. I agree, perfect it is not.

Considering all the discussed aspects, I feel this is the best possible implementation at this stage. I will go ahead and merge it, and start working on the user settings.

@DannyBen DannyBen merged commit 8efb62e into master Apr 28, 2022
@DannyBen DannyBen deleted the add/tab-indent branch April 28, 2022 15:20
@DannyBen
Copy link
Member Author

DannyBen commented Apr 28, 2022

@hojerst btw - when an inline indentation is needed, wouldn't the originally proposed solution work? The one where you write your function-that-returns-heredoc inside one of the lib/* files?

If so - I will re-add this as an alternative example.

@hojerst
Copy link

hojerst commented Apr 28, 2022

normally yes, but this breaks when you use the -w flag which wrapps everything in a function. So this currently also adds indention to code from lib/*.

@hojerst
Copy link

hojerst commented Apr 28, 2022

Maybe not indenting the code in the wrapper function would be the most compatible way (but this results in a wrongly indented generated script)

@DannyBen
Copy link
Member Author

Yeah, well if you want to wrap and use heredocs and indent your heredocs - the official answer is "touch luck" :)

I am curious - why use wrap at all? Isn't bashly providing you with sufficient tools to generate the full result without the need to wrap it in a function?

@hojerst
Copy link

hojerst commented Apr 28, 2022

Wrapping isn't a functionality I personally need. I just want to provide this function to the users of my script. This allows to source the script without executing it directly. Isn't this the main function of the -w flag anyway? I can live with not having it wrapped for now though.

On the other hand moving 90% of my business logic to a function in lib and passing args from command to the lib function easily kills readability of code. (one of the major features of bashly imho).

Maybe for clarity my specific usecase:
I'm writing a small bash helper that allows me to manage a Unifi USG router. The router provides SSH but is rather limited regarding available tooling. I want to call a ssh hostname command with command being a multiline shell script. This script in itself needs to do regex replacements with sed. To make things more complicated, the regex needs to include variable parts AND needs to use ' and ". Being regex the need to use \ is a must too. The most readable way to generate this script for me is something like:

script="$(cat <<-EOF script goes here EOF )" 

Needing to deal with double and tripple escaping \, ' and " is just aweful without heredocs in bash.

@DannyBen
Copy link
Member Author

DannyBen commented Apr 28, 2022

I see... and here-strings wont work?

<<<'script goes here '

and have you considered taking all the "SSH scripts" and putting them in an external file, that is read from?

@hojerst
Copy link

hojerst commented Apr 28, 2022

yeah I considered those, but there are multiple downsides to both approaches.

  1. separate file: I need to insert dynamic values into the script. With heredocs you can just insert vars from the bash environment using ${var} syntax. Thats not trivial for external files. Sure I could copy them as is to the remote and run them with a argument, but this needs a temporary file on the server, which I do not want either.
  2. for the usecase of needing vars expanded, I can't use single quote multiline strings ('). This leaves me with double quote (") multiline strings. However as the regexes in my script contain a bunch of \ and " by themself, I would need to escape those in the string. Something that is not needed for using heredocs.
    As the generated script will also be passed to another host and run there, getting the quoting/escaping right is very important - and this is something already quite tedious when dealing with regexes (which my script also includes as it needs to use complex sed commands).

Heredocs are just the simplest/most readable solution for my usecase. (I use heredocs quite often as a 'poor-mans template engine').

Anyway, I got a solution for my usecase which allows me to stick with bashly (which I really like btw).

@DannyBen
Copy link
Member Author

Alright. A version will be released tomorrow. Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants