Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Wrong indentation when variable is multiline #326

Closed
trashhead opened this issue Nov 15, 2023 · 8 comments
Closed

Wrong indentation when variable is multiline #326

trashhead opened this issue Nov 15, 2023 · 8 comments

Comments

@trashhead
Copy link

I have this variable defined in my azure pipeline.

  application.properties: |
    customMessage=This is dev machine
    customMessage2=This is dev machine2

I use it here:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-test-config
  namespace: elinstallation
data:
  application.properties: |
    #{application.properties}#

But this results in the following:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-test-config
  namespace: elinstallation
data:
  application.properties: |
    customMessage=This is dev machine
customMessage2=This is dev machine2

First line is ok but second has wrong indentation.

@qetza
Copy link
Owner

qetza commented Nov 16, 2023

Hi @trashhead,
This is because of how YAML multiline values are read, the first spaces which represents the YAML indentation are stripped from your value on all lines.

So the YAML value for application.properties in

application.properties: |
    customMessage=This is dev machine
    customMessage2=This is dev machine2

will be

customMessage=This is dev machine
customMessage2=This is dev machine2

(note the end of line at the end also)

If you need some spaces at the start of your lines you need to add it after the YAML indentation (your first line has the correct indentation because the indentation is in the file before the replace token)

application.properties: |-
    customMessage=This is dev machine
        customMessage2=This is dev machine2

(the added - in the YAML will avoid adding a new line at the end of the value, see https://yaml-multiline.info/ for doing test)

@trashhead
Copy link
Author

I see you have some functions implemented, upper, lower etc. Would it not be possible to add something similiar to "indent" which might take an argument of how much it should indent every row.
Would help alot :)

@qetza
Copy link
Owner

qetza commented Nov 20, 2023

Hi @trashhead,
This could be an enhancement but then would you expect the first line to be also indented or only starting from the second line; as usually the token is already indented correctly so will be the first line.

So #{indent(application.properties, 4)}# with a value as you specified would be replaced by:

customMessage=This is dev machine
    customMessage2=This is dev machine2

and

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-test-config
  namespace: elinstallation
data:
  application.properties: |
    #{indent(application.properties, 4)}#

would become:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-test-config
  namespace: elinstallation
data:
  application.properties: |
    customMessage=This is dev machine
    customMessage2=This is dev machine2
    

note: the last empty line will also be indented

@trashhead
Copy link
Author

Since anything could be stored in a variable, even yaml, keeping all indentations specified within the variable is important

So these lines:

customMessage=This is dev machine
  customMessage2=This is dev machine2

and this:

 application.properties: |
    #{indent(application.properties, 4)}#

would result in:

data:
  application.properties: |
    customMessage=This is dev machine
      customMessage2=This is dev machine2

and these:

customMessage=This is dev machine
customMessage2=This is dev machine2

it would result in:

data:
  application.properties: |
    customMessage=This is dev machine
    customMessage2=This is dev machine2

So maybe an argument isn't actually necessary?
It should just indent everything the same amount as how much first line is indented aka how much this is indented "#{indent(application.properties)}#".

@trashhead
Copy link
Author

On further consideration maybe there doesnt even have to be an indent function?
Could it just be default behaviour.
Can't see any reason that someone would want weird indentation-breaking translation of tokens.

@qetza
Copy link
Owner

qetza commented Nov 23, 2023

Hi @trashhead,
The replace token task is just doing at the end a simple find & replace on text files so it doesn't know anything about the file and it's structure.
So there's no way to implement a smart indent feature, the only logic i can add is in the functions to transform the values but without any understanding of the context.

@trashhead
Copy link
Author

If it is possible to implement that indent-function that would be nice :)

@qetza
Copy link
Owner

qetza commented Mar 10, 2024

I'm archiving the project and moving it to a new repository https://github.com/qetza/replacetokens-task

@qetza qetza closed this as completed Mar 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants