Skip to content

Commit

Permalink
feat(cmd): manifests could dump as file
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Jun 16, 2023
1 parent 724aea2 commit 29f3314
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
20 changes: 14 additions & 6 deletions cmd/kubepkg/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"fmt"
"io"
"os"
"strings"

"github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -176,11 +178,17 @@ type ManifestDumper struct {

func (s *ManifestDumper) DumpManifests(kubepkgs []*v1alpha1.KubePkg) error {
if s.ExtractManifestsYaml != "" {
manifestsYamlFile, err := ioutil.CreateOrOpen(s.ExtractManifestsYaml)
if err != nil {
return errors.Wrapf(err, "open %s failed", s.ExtractManifestsYaml)
var w io.Writer = os.Stdout

if s.ExtractManifestsYaml != "-" {
manifestsYamlFile, err := ioutil.CreateOrOpen(s.ExtractManifestsYaml)
if err != nil {
return errors.Wrapf(err, "open %s failed", s.ExtractManifestsYaml)
}
defer manifestsYamlFile.Close()

w = manifestsYamlFile
}
defer manifestsYamlFile.Close()

for i := range kubepkgs {
manifests, err := manifest.ExtractComplete(kubepkgs[i])
Expand All @@ -192,8 +200,8 @@ func (s *ManifestDumper) DumpManifests(kubepkgs []*v1alpha1.KubePkg) error {
if err != nil {
return errors.Wrapf(err, "encoding to yaml failed: %s", s.ExtractManifestsYaml)
}
_, _ = manifestsYamlFile.WriteString("---\n")
_, _ = manifestsYamlFile.Write(data)
_, _ = fmt.Fprint(w, "---\n")
_, _ = w.Write(data)
}
}
}
Expand Down
21 changes: 8 additions & 13 deletions cmd/kubepkg/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package main

import (
"context"
"fmt"

"github.com/innoai-tech/infra/pkg/cli"
"github.com/octohelm/kubepkg/pkg/kubepkg"
"github.com/octohelm/kubepkg/pkg/kubepkg/manifest"
"sigs.k8s.io/yaml"
)

func init() {
Expand All @@ -22,6 +19,7 @@ type Manifests struct {

type PrintManifests struct {
Namespace string `flag:",omitempty"`
Output string `flag:",omitempty"`
KubepkgJSON string `arg:""`
}

Expand All @@ -37,18 +35,15 @@ func (p *PrintManifests) Run(ctx context.Context) error {
if p.Namespace != "" {
kpkg.Namespace = p.Namespace
}
}

manifests, err := manifest.ExtractComplete(kpkg)
if err != nil {
return err
}
d := ManifestDumper{
ExtractManifestsYaml: "-",
}

for _, m := range manifests {
fmt.Println("---")
data, _ := yaml.Marshal(m)
fmt.Println(string(data))
}
if p.Output != "" {
d.ExtractManifestsYaml = p.Output
}

return nil
return d.DumpManifests(kpkgs)
}

0 comments on commit 29f3314

Please sign in to comment.