Skip to content

Commit

Permalink
Fix yaml indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
bhapas committed Feb 13, 2024
1 parent b6529ba commit 87c0826
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
7 changes: 4 additions & 3 deletions internal/stack/_static/kibana.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,16 @@ xpack.fleet.outputs:

{{ $logstash_enabled := fact "logstash_enabled" }}
{{ if eq $logstash_enabled "true" }}
{{ $logstash_ssl_certificate := fact "logstash_ssl_certificate" }}
{{ $logstash_ssl_key := fact "logstash_ssl_key" }}
- id: fleet-logstash-output
name: logstash-output
type: logstash
hosts: [ logstash:5044 ]
ssl:
certificate: |
{{ fact "logstash_ssl_certificate" }}
{{ indent $logstash_ssl_certificate 9 }}
key: |
{{ fact "logstash_ssl_key" }}

{{ indent $logstash_ssl_key 9 }}
{{ end }}
{{ end }}
11 changes: 9 additions & 2 deletions internal/stack/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
var (
templateFuncs = template.FuncMap{
"semverLessThan": semverLessThan,
"indent": indent,
}
staticSource = resource.NewSourceFS(static).WithTemplateFuncs(templateFuncs)
stackResources = []resource.Resource{
Expand Down Expand Up @@ -200,7 +201,7 @@ func addClientCertsToResources(resourceManager *resource.Manager, certResources
return fmt.Errorf("failed to read client certificate: %w", err)
}
// Replace newlines with spaces to create proper indentation in the config
certFile = strings.Replace(buf.String(), "\n", "\n ", 200)
certFile = buf.String()
continue
}
if res.Path == keyPath {
Expand All @@ -209,7 +210,7 @@ func addClientCertsToResources(resourceManager *resource.Manager, certResources
return fmt.Errorf("failed to read client key: %w", err)
}
// Replace newlines with spaces to create proper indentation in the config
keyFile = strings.Replace(buf.String(), "\n", "\n ", 200)
keyFile = buf.String()
continue
}
}
Expand All @@ -234,3 +235,9 @@ func semverLessThan(a, b string) (bool, error) {

return sa.LessThan(sb), nil
}

// indent adds the padding to the right of input string.
// Typically used for fixing yaml configs.
func indent(input string, padding int) string {
return strings.Replace(input, "\n", fmt.Sprintf("%-*s", padding, "\n"), 200)
}
35 changes: 35 additions & 0 deletions internal/stack/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,38 @@ func TestSemverLessThan(t *testing.T) {
require.NoError(t, err)
assert.True(t, b)
}

func TestIndent(t *testing.T) {
s := indent(`-----BEGIN CERTIFICATE-----
MIIByDCCAW+gAwIBAgIRAKZ7t5czbExcLrfZnBchSzUwCgYIKoZIzj0EAwIwHTEb
MBkGA1UEAxMSZWxhc3RpYy1wYWNrYWdlIENBMB4XDTI0MDIxMzA5MjM0M1oXDTI2
MDQyMzA5MjM0M1owGDEWMBQGA1UEAxMNZWxhc3RpYy1hZ2VudDBZMBMGByqGSM49
AgEGCCqGSM49AwEHA0IABBv3HqeW3NWIfp408trMNvBiSIHv4Dahc+os52yXN5/b
ho1G3WGLj0WYErCzJbB4He18pCV4c0/33o/lEYW3JjijgZQwgZEwDgYDVR0PAQH/
BAQDAgWgMBMGA1UdJQQMMAoGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwHwYDVR0j
BBgwFoAUw0L8p+5q7uZycR3T7xj5pyWOIU8wOwYDVR0RBDQwMoIJbG9jYWxob3N0
gg1lbGFzdGljLWFnZW50hwR/AAABhxAAAAAAAAAAAAAAAAAAAAABMAoGCCqGSM49
BAMCA0cAMEQCIFukH6qlkBvHkZAccsFZZtX4vHQ7foeNTQhursBMmynOAiA0wwwQ
vvG/LwXVsGCXgSJahuOLkBPOaX2N+oDdYt267A==
-----END CERTIFICATE-----`, 9)

exp :=
`-----BEGIN CERTIFICATE-----
MIIByDCCAW+gAwIBAgIRAKZ7t5czbExcLrfZnBchSzUwCgYIKoZIzj0EAwIwHTEb
MBkGA1UEAxMSZWxhc3RpYy1wYWNrYWdlIENBMB4XDTI0MDIxMzA5MjM0M1oXDTI2
MDQyMzA5MjM0M1owGDEWMBQGA1UEAxMNZWxhc3RpYy1hZ2VudDBZMBMGByqGSM49
AgEGCCqGSM49AwEHA0IABBv3HqeW3NWIfp408trMNvBiSIHv4Dahc+os52yXN5/b
ho1G3WGLj0WYErCzJbB4He18pCV4c0/33o/lEYW3JjijgZQwgZEwDgYDVR0PAQH/
BAQDAgWgMBMGA1UdJQQMMAoGCCsGAQUFBwMCMAwGA1UdEwEB/wQCMAAwHwYDVR0j
BBgwFoAUw0L8p+5q7uZycR3T7xj5pyWOIU8wOwYDVR0RBDQwMoIJbG9jYWxob3N0
gg1lbGFzdGljLWFnZW50hwR/AAABhxAAAAAAAAAAAAAAAAAAAAABMAoGCCqGSM49
BAMCA0cAMEQCIFukH6qlkBvHkZAccsFZZtX4vHQ7foeNTQhursBMmynOAiA0wwwQ
vvG/LwXVsGCXgSJahuOLkBPOaX2N+oDdYt267A==
-----END CERTIFICATE-----`

assert.Equal(t, exp, s)

s = indent("\n", 9)
exp = "\n "
assert.Equal(t, exp, s)
}

0 comments on commit 87c0826

Please sign in to comment.