Skip to content

Commit

Permalink
add export to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmdm committed Feb 27, 2024
1 parent 3223e8b commit 44b071f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
30 changes: 21 additions & 9 deletions cmd/halloumi/cmd_takeoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"slices"
"strings"

y3 "gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/tools/clientcmd"
Expand All @@ -34,9 +35,9 @@ type TakeoffPlatterParams struct {

type TakeoffParams struct {
GlobalSettings
Release string
Platter TakeoffPlatterParams
OutputDir string
Release string
Platter TakeoffPlatterParams
Out string
}

//go:embed cmd_takeoff_help.txt
Expand All @@ -60,7 +61,7 @@ func GetTakeoffParams(settings GlobalSettings, source io.Reader, args []string)
}

RegisterGlobalFlags(flagset, &params.GlobalSettings)
flagset.StringVar(&params.OutputDir, "outDir", "", "if present outputs platter resources to outDir instead of applying to k8")
flagset.StringVar(&params.Out, "out", "", "if present outputs platter resources to directory specified, if out is - outputs to standard out")

args, params.Platter.Args = internal.CutArgs(args)

Expand Down Expand Up @@ -109,11 +110,11 @@ func TakeOff(ctx context.Context, params TakeoffParams) error {

internal.AddHallmouiMetadata(resources, params.Release)

if params.OutputDir != "" {
if err := ExportToFS(params.OutputDir, params.Release, resources); err != nil {
return fmt.Errorf("failed to export release: %w", err)
if params.Out != "" {
if params.Out == "-" {
return ExportToStdout(resources)
}
return nil
return ExportToFS(params.Out, params.Release, resources)
}

restcfg, err := clientcmd.BuildConfigFromFlags("", params.KubeConfigPath)
Expand Down Expand Up @@ -213,5 +214,16 @@ func ExportToFS(dir, release string, resources []*unstructured.Unstructured) err
}
}

return xerr.MultiErrFrom("", errs...)
return xerr.MultiErrFrom("failed to write resource(s)", errs...)
}

func ExportToStdout(resources []*unstructured.Unstructured) error {
output := make(map[string]any, len(resources))
for _, resource := range resources {
output[internal.Canonical(resource)] = resource.Object
}

encoder := y3.NewEncoder(os.Stdout)
encoder.SetIndent(2)
return encoder.Encode(output)
}
2 changes: 1 addition & 1 deletion cmd/halloumi/cmd_takeoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ spec:
name: sample-app
`),
},
OutputDir: "",
Out: "",
}

restcfg, err := clientcmd.BuildConfigFromFlags("", params.KubeConfigPath)
Expand Down

0 comments on commit 44b071f

Please sign in to comment.