Skip to content

Commit

Permalink
add YQ_PRETTYPRINT
Browse files Browse the repository at this point in the history
  • Loading branch information
nxcc committed Aug 23, 2023
1 parent ccfbe5b commit 525829f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Some environment variables can help working with cuegen:
CUEGEN_HTTP_USERNAME username for git authentication
SOPS_AGE_KEY age key for decryption
SOPS_AGE_KEY_FILE age key file for decryption
YQ_PRETTYPRINT !="": run yaml output thru `yq -P`
starting with `/`: use as path to yq

## Components
Components can be
Expand Down Expand Up @@ -179,32 +181,33 @@ Load all files from directory `scripts` as key/values into `configMap.scripts.da

## Changelog

* `v0.1.0` - Initial release
* `v0.1.1` - Improved attribute lookup
* `v0.2.0` - Added checks when reading `cuegen.yaml`
* `v0.2.1` - Improved error messages
* `v0.3.0` - Added ability to read subpaths from git repos
* `v0.3.1` - No code changes, trigger cmp build
* `v0.3.2` - No code changes, bump go version to 1.20
* `v0.4.0` - switch default config to `cuegen.cue`, use cue v0.5.0-beta5
* `v0.4.1` - Make components & checkPaths optional
* `v0.4.2` - downgrade cue to v0.5.0-beta.2 ([performance regression][gh2243])
* `v0.4.3` - fix running as kustomize plugin
* `v0.4.4` - improve handling of git urls
* `v0.5.0` - add cuegen default config
* `v0.6.0` - add dumpOverlays option
* `v0.7.0` - upgrade cue to v0.5.0 (many fixes, rare performance regression still present)
* `v0.7.1` - fix secret handling of @readfile
* `v0.7.2` - internal cleanup
* `v0.8.0` - allow remote cuegen directories, rm kustomize plugin support
* `v0.9.0` - upgrade cue to v0.6.0
* `v0.1.0` - Initial release
* `v0.1.1` - Improved attribute lookup
* `v0.2.0` - Added checks when reading `cuegen.yaml`
* `v0.2.1` - Improved error messages
* `v0.3.0` - Added ability to read subpaths from git repos
* `v0.3.1` - No code changes, trigger cmp build
* `v0.3.2` - No code changes, bump go version to 1.20
* `v0.4.0` - switch default config to `cuegen.cue`, use cue v0.5.0-beta5
* `v0.4.1` - Make components & checkPaths optional
* `v0.4.2` - downgrade cue to v0.5.0-beta.2 ([performance regression][gh2243])
* `v0.4.3` - fix running as kustomize plugin
* `v0.4.4` - improve handling of git urls
* `v0.5.0` - add cuegen default config
* `v0.6.0` - add dumpOverlays option
* `v0.7.0` - upgrade cue to v0.5.0 (many fixes, rare performance regression still present)
* `v0.7.1` - fix secret handling of @readfile
* `v0.7.2` - internal cleanup
* `v0.8.0` - allow remote cuegen directories, rm kustomize plugin support
* `v0.9.0` - upgrade cue to v0.6.0
* `v0.10.0` - add YQ_PRETTYPRINT to filter output thru `yq -P``

[CUE]: https://cuelang.org
[SOPS]: https://github.com/mozilla/sops
[k8stut]: https://cuelang.org/docs/tutorials/
[eg]: examples/
[rel]: https://github.com/noris-network/cuegen/releases/latest
[cmp]: https://argo-cd.readthedocs.io/en/stable/user-guide/config-management-plugins/#option-2-configure-plugin-via-sidecar
[cmp]: https://argo-cd.readthedocs.io/en/stable/operator-manual/config-management-plugins/#sidecar-plugin
[cuegen-cmp]: https://hub.docker.com/r/nxcc/cuegen-cmp
[expenv]: https://pkg.go.dev/os#ExpandEnv
[cfgschema]: internal/app/schema.cue
Expand Down
4 changes: 3 additions & 1 deletion docker/Dockerfile-cmp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM golang:1.20 as builder
FROM golang:1.21 as builder
WORKDIR /go/src/app
ARG VERSION
COPY . ./
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w -X 'main.build=$VERSION'" -o cuegen .
RUN sed "s/VERSION/$VERSION/g" docker/plugin.yaml > plugin.yaml
RUN curl -sfL https://github.com/mikefarah/yq/releases/download/v4.35.1/yq_linux_amd64 -o /tmp/yq && chmod 755 /tmp/yq

FROM gcr.io/distroless/static-debian11
COPY --from=builder /tmp/yq /bin/yq
COPY --from=builder /go/src/app/cuegen /bin/cuegen
COPY --from=builder /go/src/app/plugin.yaml /home/argocd/cmp-server/config/plugin.yaml
21 changes: 20 additions & 1 deletion internal/cuegen/cuegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -175,7 +176,25 @@ func (cg Cuegen) Exec() error {
return fmt.Errorf("Exec: marshal stream: %v", err)
}

fmt.Print(noBinaryPrefix(yamlString))
fixedYamlString := noBinaryPrefix(yamlString)

// filter output thru yq -P
yqPp := os.Getenv("YQ_PRETTYPRINT")
if yqPp != "" {
yqBin := "yq"
if strings.HasPrefix(yqPp, "/") {
yqBin = yqPp
}
cmd := exec.Command(yqBin, "-P")
cmd.Stdin = strings.NewReader(fixedYamlString)
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
return err
}
} else {
fmt.Print(fixedYamlString)
}

return nil
}
Expand Down

0 comments on commit 525829f

Please sign in to comment.