Skip to content

Commit c746664

Browse files
committed
added substitutes template generation from FBC
Signed-off-by: grokspawn <[email protected]>
1 parent 31ef961 commit c746664

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

alpha/template/converter/converter.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,34 @@ import (
99
"sigs.k8s.io/yaml"
1010

1111
"github.com/operator-framework/operator-registry/alpha/template/basic"
12+
"github.com/operator-framework/operator-registry/alpha/template/substitutes"
1213
"github.com/operator-framework/operator-registry/pkg/image"
1314
)
1415

1516
type Converter struct {
16-
FbcReader io.Reader
17-
OutputFormat string
18-
Registry image.Registry
17+
FbcReader io.Reader
18+
OutputFormat string
19+
Registry image.Registry
20+
DestinationTemplateType string // TODO: when we have a template factory, we can pass it here
1921
}
2022

2123
func (c *Converter) Convert() error {
22-
bt, err := basic.FromReader(c.FbcReader)
23-
if err != nil {
24-
return err
24+
var b []byte
25+
switch c.DestinationTemplateType {
26+
case "basic":
27+
bt, err := basic.FromReader(c.FbcReader)
28+
if err != nil {
29+
return err
30+
}
31+
b, _ = json.MarshalIndent(bt, "", " ")
32+
case "substitutes":
33+
st, err := substitutes.FromReader(c.FbcReader)
34+
if err != nil {
35+
return err
36+
}
37+
b, _ = json.MarshalIndent(st, "", " ")
2538
}
2639

27-
b, _ := json.MarshalIndent(bt, "", " ")
2840
if c.OutputFormat == "json" {
2941
fmt.Fprintln(os.Stdout, string(b))
3042
} else {

alpha/template/substitutes/substitutes.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,26 @@ func (t Template) processSubstitution(ctx context.Context, cfg *declcfg.Declarat
158158

159159
return nil
160160
}
161+
162+
// FromReader reads FBC from a reader and generates a BasicTemplate from it
163+
func FromReader(r io.Reader) (*SubstitutesForTemplate, error) {
164+
var entries []*declcfg.Meta
165+
if err := declcfg.WalkMetasReader(r, func(meta *declcfg.Meta, err error) error {
166+
if err != nil {
167+
return err
168+
}
169+
170+
entries = append(entries, meta)
171+
return nil
172+
}); err != nil {
173+
return nil, err
174+
}
175+
176+
bt := &SubstitutesForTemplate{
177+
Schema: schema,
178+
Entries: entries,
179+
Substitutions: []Substitute{},
180+
}
181+
182+
return bt, nil
183+
}

cmd/opm/alpha/convert-template/convert.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func NewCmd() *cobra.Command {
1717
}
1818
cmd.AddCommand(
1919
newBasicConvertCmd(),
20+
newSubstitutesConvertCmd(),
2021
)
2122
return cmd
2223
}
@@ -49,6 +50,49 @@ If no argument is specified or is '-' input is assumed from STDIN.
4950
}
5051

5152
converter.FbcReader = reader
53+
converter.DestinationTemplateType = "basic"
54+
err = converter.Convert()
55+
if err != nil {
56+
return fmt.Errorf("converting: %v", err)
57+
}
58+
59+
return nil
60+
},
61+
}
62+
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)")
63+
64+
return cmd
65+
}
66+
67+
func newSubstitutesConvertCmd() *cobra.Command {
68+
var (
69+
converter converter.Converter
70+
output string
71+
)
72+
cmd := &cobra.Command{
73+
Use: "substitutes [<fbc-file> | -]",
74+
Args: cobra.MaximumNArgs(1),
75+
Short: "Generate a substitutes template from existing FBC",
76+
Long: `Generate a substitutes template from existing FBC.
77+
78+
This command outputs a substitutes catalog template to STDOUT from input FBC.
79+
If no argument is specified or is '-' input is assumed from STDIN.
80+
`,
81+
RunE: func(c *cobra.Command, args []string) error {
82+
switch output {
83+
case "yaml", "json":
84+
converter.OutputFormat = output
85+
default:
86+
log.Fatalf("invalid --output value %q, expected (json|yaml)", output)
87+
}
88+
89+
reader, name, err := util.OpenFileOrStdin(c, args)
90+
if err != nil {
91+
return fmt.Errorf("unable to open input: %q", name)
92+
}
93+
94+
converter.FbcReader = reader
95+
converter.DestinationTemplateType = "substitutes"
5296
err = converter.Convert()
5397
if err != nil {
5498
return fmt.Errorf("converting: %v", err)

0 commit comments

Comments
 (0)