Skip to content

Commit 7d53d7d

Browse files
Merge pull request #401 from oceanc80/OCPBUGS-1272
OCPBUGS-1272: Add pipe support to render-veneer basic command (#1026)
2 parents ab5ecfd + 0e8a0ad commit 7d53d7d

File tree

11 files changed

+96
-118
lines changed

11 files changed

+96
-118
lines changed

staging/operator-registry/alpha/declcfg/load.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ func extractCSV(objs []string) string {
118118
return ""
119119
}
120120

121-
func readYAMLOrJSON(r io.Reader) (*DeclarativeConfig, error) {
121+
// LoadReader reads yaml or json from the passed in io.Reader and unmarshals it into a DeclarativeConfig struct.
122+
// Path references will not be de-referenced so callers are responsible for de-referencing if necessary.
123+
func LoadReader(r io.Reader) (*DeclarativeConfig, error) {
122124
cfg := &DeclarativeConfig{}
123125
dec := yaml.NewYAMLOrJSONDecoder(r, 4096)
124126
for {
@@ -173,7 +175,7 @@ func LoadFile(root fs.FS, path string) (*DeclarativeConfig, error) {
173175
}
174176
defer file.Close()
175177

176-
cfg, err := readYAMLOrJSON(file)
178+
cfg, err := LoadReader(file)
177179
if err != nil {
178180
return nil, err
179181
}

staging/operator-registry/alpha/declcfg/load_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/operator-framework/operator-registry/alpha/property"
1515
)
1616

17-
func TestReadYAMLOrJSON(t *testing.T) {
17+
func TestLoadReader(t *testing.T) {
1818
type spec struct {
1919
name string
2020
fsys fs.FS
@@ -80,7 +80,7 @@ func TestReadYAMLOrJSON(t *testing.T) {
8080
f, err := s.fsys.Open(s.path)
8181
require.NoError(t, err)
8282

83-
cfg, err := readYAMLOrJSON(f)
83+
cfg, err := LoadReader(f)
8484
s.assertion(t, err)
8585
if err == nil {
8686
require.NotNil(t, cfg)

staging/operator-registry/alpha/veneer/basic/basic.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package basic
22

33
import (
44
"context"
5-
"errors"
6-
"os"
7-
"path/filepath"
5+
"fmt"
6+
"io"
87

98
"github.com/operator-framework/operator-registry/alpha/action"
109
"github.com/operator-framework/operator-registry/alpha/declcfg"
@@ -15,30 +14,8 @@ type Veneer struct {
1514
Registry image.Registry
1615
}
1716

18-
func (v Veneer) Render(ctx context.Context, ref string) (*declcfg.DeclarativeConfig, error) {
19-
// only taking first argument as file
20-
stat, serr := os.Stat(ref)
21-
if serr != nil {
22-
return nil, serr
23-
}
24-
25-
if stat.IsDir() {
26-
return nil, errors.New("cannot render veneers by directory reference")
27-
}
28-
return v.renderFile(ctx, ref)
29-
}
30-
31-
func (v Veneer) renderFile(ctx context.Context, ref string) (*declcfg.DeclarativeConfig, error) {
32-
// xform any relative to absolute paths
33-
abspath, err := filepath.Abs(ref)
34-
if err != nil {
35-
return nil, err
36-
}
37-
// xform to break apart dir/file elements
38-
rpath, fname := filepath.Split(abspath)
39-
root := os.DirFS(rpath)
40-
41-
cfg, err := declcfg.LoadFile(root, fname)
17+
func (v Veneer) Render(ctx context.Context, reader io.Reader) (*declcfg.DeclarativeConfig, error) {
18+
cfg, err := declcfg.LoadReader(reader)
4219
if err != nil {
4320
return cfg, err
4421
}
@@ -52,8 +29,7 @@ func (v Veneer) renderFile(ctx context.Context, ref string) (*declcfg.Declarativ
5229

5330
for _, b := range cfg.Bundles {
5431
if !isBundleVeneer(&b) {
55-
outb = append(outb, b)
56-
continue
32+
return nil, fmt.Errorf("unexpected fields present in basic veneer bundle")
5733
}
5834
r.Refs = []string{b.Image}
5935
contributor, err := r.Run(ctx)
@@ -67,7 +43,8 @@ func (v Veneer) renderFile(ctx context.Context, ref string) (*declcfg.Declarativ
6743
return cfg, nil
6844
}
6945

70-
// isBundleVeneer identifies loaded partial Bundle data from YAML/JSON veneer source as having no properties,
46+
// isBundleVeneer identifies a Bundle veneer source as having a Schema and Image defined
47+
// but no Properties, RelatedImages or Package defined
7148
func isBundleVeneer(b *declcfg.Bundle) bool {
72-
return len(b.Properties) == 0
49+
return b.Schema != "" && b.Image != "" && b.Package == "" && len(b.Properties) == 0 && len(b.RelatedImages) == 0
7350
}

staging/operator-registry/cmd/opm/alpha/veneer/basic.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,22 @@ func newBasicVeneerRenderCmd() *cobra.Command {
2020
output string
2121
)
2222
cmd := &cobra.Command{
23-
Use: "basic basic-veneer-file",
24-
Short: "Generate a declarative config blob from a single 'basic veneer' file",
25-
Long: `Generate a declarative config blob from a single 'basic veneer' file, typified as a declarative configuration file where olm.bundle objects have no properties`,
26-
Args: cobra.ExactArgs(1),
23+
Use: "basic basic-veneer-file",
24+
Short: `Generate a file-based catalog from a single 'basic veneer' file
25+
When FILE is '-' or not provided, the veneer is read from standard input`,
26+
Long: `Generate a file-based catalog from a single 'basic veneer' file
27+
When FILE is '-' or not provided, the veneer is read from standard input`,
28+
Args: cobra.MaximumNArgs(1),
2729
Run: func(cmd *cobra.Command, args []string) {
30+
// Handle different input argument types
31+
// When no arguments or "-" is passed to the command,
32+
// assume input is coming from stdin
33+
// Otherwise open the file passed to the command
34+
data, source, err := openFileOrStdin(cmd, args)
35+
if err != nil {
36+
log.Fatalf("unable to open %q: %v", source, err)
37+
}
38+
defer data.Close()
2839

2940
var write func(declcfg.DeclarativeConfig, io.Writer) error
3041
switch output {
@@ -50,7 +61,7 @@ func newBasicVeneerRenderCmd() *cobra.Command {
5061
veneer.Registry = reg
5162

5263
// only taking first file argument
53-
cfg, err := veneer.Render(cmd.Context(), args[0])
64+
cfg, err := veneer.Render(cmd.Context(), data)
5465
if err != nil {
5566
log.Fatal(err)
5667
}

staging/operator-registry/cmd/opm/alpha/veneer/cmd.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package veneer
22

33
import (
4+
"io"
5+
"os"
6+
47
"github.com/spf13/cobra"
58
)
69

@@ -16,3 +19,11 @@ func NewCmd() *cobra.Command {
1619

1720
return runCmd
1821
}
22+
23+
func openFileOrStdin(cmd *cobra.Command, args []string) (io.ReadCloser, string, error) {
24+
if len(args) == 0 || args[0] == "-" {
25+
return io.NopCloser(cmd.InOrStdin()), "stdin", nil
26+
}
27+
reader, err := os.Open(args[0])
28+
return reader, args[0], err
29+
}

staging/operator-registry/cmd/opm/alpha/veneer/semver.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ import (
1818
func newSemverCmd() *cobra.Command {
1919
output := ""
2020
cmd := &cobra.Command{
21-
Use: "semver [FILE]",
22-
Short: "Generate a file-based catalog from a single 'semver veneer' file \nWhen FILE is '-' or not provided, the veneer is read from standard input",
23-
Long: "Generate a file-based catalog from a single 'semver veneer' file \nWhen FILE is '-' or not provided, the veneer is read from standard input",
24-
Args: cobra.MaximumNArgs(1),
21+
Use: "semver [FILE]",
22+
Short: `Generate a file-based catalog from a single 'semver veneer' file
23+
When FILE is '-' or not provided, the veneer is read from standard input`,
24+
Long: `Generate a file-based catalog from a single 'semver veneer' file
25+
When FILE is '-' or not provided, the veneer is read from standard input`,
26+
Args: cobra.MaximumNArgs(1),
2527
RunE: func(cmd *cobra.Command, args []string) error {
2628
// Handle different input argument types
2729
// When no arguments or "-" is passed to the command,
2830
// assume input is coming from stdin
2931
// Otherwise open the file passed to the command
30-
data, source, err := openFileOrReadStdin(cmd, args)
32+
data, source, err := openFileOrStdin(cmd, args)
3133
if err != nil {
3234
return err
3335
}
36+
defer data.Close()
3437

3538
var write func(declcfg.DeclarativeConfig, io.Writer) error
3639
switch output {
@@ -80,18 +83,3 @@ func newSemverCmd() *cobra.Command {
8083
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml|mermaid)")
8184
return cmd
8285
}
83-
84-
func openFileOrReadStdin(cmd *cobra.Command, args []string) (io.Reader, string, error) {
85-
var err error = nil
86-
var source string = ""
87-
var reader io.Reader
88-
89-
if len(args) == 0 || args[0] == "-" {
90-
reader = cmd.InOrStdin()
91-
source = "stdin"
92-
} else {
93-
reader, err = os.Open(args[0])
94-
source = args[0]
95-
}
96-
return reader, source, err
97-
}

vendor/github.com/operator-framework/operator-registry/alpha/declcfg/load.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-registry/alpha/veneer/basic/basic.go

Lines changed: 8 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-registry/cmd/opm/alpha/veneer/basic.go

Lines changed: 16 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-registry/cmd/opm/alpha/veneer/cmd.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/operator-framework/operator-registry/cmd/opm/alpha/veneer/semver.go

Lines changed: 8 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)