default output to stdout

This commit is contained in:
CamrynCarter
2026-08-19 00:09:37 -04:00
committed by GitHub
parent 3d1f528e8f
commit d70063e887
3 changed files with 22 additions and 14 deletions
+3 -3
View File
@@ -517,13 +517,13 @@ func addStoreCreateManifest(rso *flags.StoreRootOpts, ro *flags.CliRootOpts) *co
cmd := &cobra.Command{
Use: "manifest",
Short: "Create a hauler content manifest from the store's metadata",
Example: ` # generate a manifest for the default store into ./store-manifest.yaml
Example: ` # print a manifest for the default store to stdout
hauler store create manifest
# generate a manifest for a specific store into ./my-store-manifest.yaml
# print a manifest for a specific store to stdout
hauler store create manifest --store /path/to/my-store
# generate a manifest for a specific store into a custom path
# write a manifest for a specific store to a file
hauler store create manifest --store /path/to/store --output my-manifest.yaml`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
+18 -10
View File
@@ -55,10 +55,16 @@ type manifestDoc struct {
// CreateManifestCmd walks the store's OCI index (and the manifests/configs it
// references) to reconstruct a hauler content manifest capable of recreating the
// store's contents via `hauler store sync`. It groups discovered content into
// Images/Charts/Files documents and writes them to o.Output.
// Images/Charts/Files documents and writes them to o.Output, or to stdout when
// o.Output is empty.
func CreateManifestCmd(ctx context.Context, o *flags.CreateManifestOpts, s *store.Layout) error {
l := log.FromContext(ctx)
toStdout := o.Output == ""
if toStdout {
l.SetLevel("fatal")
}
var images []manifestImage
var charts []manifestChart
var files []manifestFile
@@ -184,11 +190,6 @@ func CreateManifestCmd(ctx context.Context, o *flags.CreateManifestOpts, s *stor
base := filepath.Base(s.Root)
output := o.Output
if output == "" {
output = base + "-manifest.yaml"
}
var out strings.Builder
if len(images) > 0 {
if err := writeDoc(&out, "", consts.ImagesContentKind, base+"-images", struct {
@@ -216,12 +217,19 @@ func CreateManifestCmd(ctx context.Context, o *flags.CreateManifestOpts, s *stor
}
}
if err := os.WriteFile(output, []byte(out.String()), 0o644); err != nil {
return fmt.Errorf("writing manifest to [%s]: %w", output, err)
if toStdout {
if _, err := os.Stdout.Write([]byte(out.String())); err != nil {
return err
}
return nil
}
outPath := output
if abs, err := filepath.Abs(output); err == nil {
if err := os.WriteFile(o.Output, []byte(out.String()), 0o644); err != nil {
return fmt.Errorf("writing manifest to [%s]: %w", o.Output, err)
}
outPath := o.Output
if abs, err := filepath.Abs(o.Output); err == nil {
outPath = abs
}
l.Infof("wrote manifest with [%d] image(s), [%d] chart(s), [%d] file(s) to [%s]", len(images), len(charts), len(files), outPath)
+1 -1
View File
@@ -11,5 +11,5 @@ type CreateManifestOpts struct {
func (o *CreateManifestOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()
f.StringVarP(&o.Output, "output", "o", "", "(Optional) Path to write the generated manifest to (default \"$STORENAME-manifest.yaml\")")
f.StringVarP(&o.Output, "output", "o", "", "(Optional) Path to write the generated manifest to (defaults to stdout)")
}