Code Monkey home page Code Monkey logo

Comments (10)

hairyhenderson avatar hairyhenderson commented on June 30, 2024 2

Resolved by #165 and #166

from gomplate.

hairyhenderson avatar hairyhenderson commented on June 30, 2024 1

Ok - I've logged #165 to track the indent enhancement that's come out of this.

For the indentfirst functionality, you can pipe the output to trimSpace to achieve the same effect:

$ gomplate -i '{{ "foo\nbar\nbaz" | indent "    " | trimSpace }}' 
foo
    bar
    baz

from gomplate.

thesuperzapper avatar thesuperzapper commented on June 30, 2024 1

@glensc @hairyhenderson You can replicate the behavior of the Helm nindent function in gomplate by printing "\n" before your template:

metadata:
  labels:
    {{- print "\n" (.data.my_labels | toYAML | trimSpace | indent 4) }}

This sort of pattern lets you remove leading newlines with{{-, so you can indent the template for visual clarity.

Note, the trimSpace is there to remove the strange dangling newline that toYAML seems to create.

from gomplate.

hairyhenderson avatar hairyhenderson commented on June 30, 2024

@estahn for this, you want the indent function - I actually wrote that specifically for dealing with YAML indentation 😉

from gomplate.

estahn avatar estahn commented on June 30, 2024

@hairyhenderson Well, that would result in pretty ugly templates, e.g.

spec:
  schedule: "{{ .schedule }}"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      activeDeadlineSeconds: 30
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: app
              image: "123"
              args:
{{ .args | toYAML | indent("              ") }}

from gomplate.

hairyhenderson avatar hairyhenderson commented on June 30, 2024

Yup, I agree, that's not ideal. But there's no way for gomplate to know about text outside of the {{/}} delimiters.

In your specific case, since YAML is a JSON superset, it's probably best to take the approach you seem to have explored in #163 and render that particular value as JSON.

from gomplate.

estahn avatar estahn commented on June 30, 2024

Would it be possible to adjust indent to something what jinja provides?

http://jinja.pocoo.org/docs/2.9/templates/#indent

indent(s, width=4, indentfirst=False)
Return a copy of the passed string, each line indented by 4 spaces. The first line is not indented. If you want to change the number of spaces or indent the first line too you can pass additional parameters to the filter:

{{ mytext|indent(2, true) }}
indent by two spaces and indent the first line too.

from gomplate.

hairyhenderson avatar hairyhenderson commented on June 30, 2024

@estahn Just to be clear about what you're asking - are you looking for indent to be callable like this?

$ gomplate -i '{{ "foo" | indent 4 }}'
    foo

That seems reasonable. Because indent already exists, I can't change the existing functionality, but what I can do is overload it so that you can provide an optional width parameter.

Is the indentfirst option of jinja's indent useful to you? If I were to implement that it'd probably be as a separate function (maybe strings.IndentAfter or something like that).

from gomplate.

estahn avatar estahn commented on June 30, 2024

@hairyhenderson It is probably useful otherwise you will need to place the placeholder always at the beginning of the line.

Example:

spec:
  schedule: "{{ .schedule }}"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      activeDeadlineSeconds: 30
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: app
              image: "123"
              args:
                {{ .args | toYAML | indent(16) }}

vs

spec:
  schedule: "{{ .schedule }}"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      activeDeadlineSeconds: 30
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: app
              image: "123"
              args:
{{ .args | toYAML | indent(16) }}

from gomplate.

glensc avatar glensc commented on June 30, 2024

The need for trimSpace problem is still there:

i.e for yaml formating typically the indent should be omitted so the function itself can be kept indented.

or I'm missing something?

I'm trying to load .env file for helm values file:

$ cat .env
APP_ENV=production
APP_DEBUG=false
APP_KEY=SomeRandomString
APP_URL=http://localhost

$ cat values.yml
config:
  {{ (ds "data") | data.ToYAML | strings.Indent 1 "  " | trimSpace }}

config-example:
  {{ (ds "data") | data.ToYAML | strings.Indent 1 "  " }}

$ gomplate -d data=file://`pwd`/.env?type=application/x-env -f values.yml
config:
  APP_DEBUG: "false"
  APP_ENV: production
  APP_KEY: SomeRandomString
  APP_URL: http://localhost

config-example:
    APP_DEBUG: "false"
  APP_ENV: production
  APP_KEY: SomeRandomString
  APP_URL: http://localhost

in helm, there's extra function nindent doing that:

from gomplate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.