-
Notifications
You must be signed in to change notification settings - Fork 254
provide required schema for basic template #1335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package converter | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/operator-framework/operator-registry/alpha/template/basic" | ||
"github.com/operator-framework/operator-registry/pkg/image" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type Converter struct { | ||
FbcReader io.Reader | ||
OutputFormat string | ||
Registry image.Registry | ||
} | ||
|
||
func (c *Converter) Convert() error { | ||
bt, err := basic.FromReader(c.FbcReader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
b, _ := json.MarshalIndent(bt, "", " ") | ||
if c.OutputFormat == "json" { | ||
fmt.Fprintln(os.Stdout, string(b)) | ||
} else { | ||
y, err := yaml.JSONToYAML(b) | ||
if err != nil { | ||
return err | ||
} | ||
y = append([]byte("---\n"), y...) | ||
fmt.Fprintln(os.Stdout, string(y)) | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package converttemplate | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/operator-framework/operator-registry/alpha/template/converter" | ||
"github.com/operator-framework/operator-registry/cmd/opm/internal/util" | ||
) | ||
|
||
func NewCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "convert-template", | ||
Short: "Convert existing FBC to a supported template type", | ||
} | ||
cmd.AddCommand( | ||
newBasicConvertCmd(), | ||
) | ||
return cmd | ||
} | ||
|
||
func newBasicConvertCmd() *cobra.Command { | ||
var ( | ||
converter converter.Converter | ||
output string | ||
) | ||
cmd := &cobra.Command{ | ||
Use: "basic [<fbc-file> | -]", | ||
Args: cobra.MaximumNArgs(1), | ||
Short: "Generate a basic template from existing FBC", | ||
Long: `Generate a basic template from existing FBC. | ||
|
||
This command outputs a basic catalog template to STDOUT from input FBC. | ||
If no argument is specified or is '-' input is assumed from STDIN. | ||
`, | ||
RunE: func(c *cobra.Command, args []string) error { | ||
|
||
switch output { | ||
case "yaml", "json": | ||
converter.OutputFormat = output | ||
default: | ||
log.Fatalf("invalid --output value %q, expected (json|yaml)", output) | ||
} | ||
|
||
reader, name, err := util.OpenFileOrStdin(c, args) | ||
if err != nil { | ||
return fmt.Errorf("unable to open input: %q", name) | ||
} | ||
|
||
converter.FbcReader = reader | ||
err = converter.Convert() | ||
if err != nil { | ||
return fmt.Errorf("converting: %v", err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)") | ||
|
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,27 @@ | ||
package template | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmd() *cobra.Command { | ||
var output string | ||
|
||
runCmd := &cobra.Command{ | ||
Use: "render-template", | ||
Short: "Render a catalog template type", | ||
Args: cobra.NoArgs, | ||
} | ||
|
||
runCmd.AddCommand(newBasicTemplateCmd()) | ||
runCmd.AddCommand(newSemverTemplateCmd()) | ||
bc := newBasicTemplateCmd() | ||
// bc.Hidden = true | ||
everettraven marked this conversation as resolved.
Show resolved
Hide resolved
|
||
runCmd.AddCommand(bc) | ||
|
||
return runCmd | ||
} | ||
sc := newSemverTemplateCmd() | ||
// sc.Hidden = true | ||
everettraven marked this conversation as resolved.
Show resolved
Hide resolved
|
||
runCmd.AddCommand(sc) | ||
|
||
func openFileOrStdin(cmd *cobra.Command, args []string) (io.ReadCloser, string, error) { | ||
if len(args) == 0 || args[0] == "-" { | ||
return io.NopCloser(cmd.InOrStdin()), "stdin", nil | ||
} | ||
reader, err := os.Open(args[0]) | ||
return reader, args[0], err | ||
runCmd.PersistentFlags().StringVarP(&output, "output", "o", "json", "Output format (json|yaml)") | ||
|
||
return runCmd | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ package util | |||||
|
||||||
import ( | ||||||
"errors" | ||||||
"io" | ||||||
"os" | ||||||
|
||||||
"github.com/spf13/cobra" | ||||||
|
@@ -66,3 +67,11 @@ func CreateCLIRegistry(cmd *cobra.Command) (*containerdregistry.Registry, error) | |||||
} | ||||||
return reg, nil | ||||||
} | ||||||
|
||||||
func OpenFileOrStdin(cmd *cobra.Command, args []string) (io.ReadCloser, string, error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: This is an internal function, and I know it was mostly a move, but the implementation only ever looks at the first argument. It seems reasonable to just make this a single string argument instead of a slice of strings
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has bugged me too, but it does actually check for empty args as a condition for reading from stdin instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel super strongly one way or the other, and saw the empty args check but figured an empty string would work equivalently. This shouldn't block this PR if you'd rather keep it as is. |
||||||
if len(args) == 0 || args[0] == "-" { | ||||||
return io.NopCloser(cmd.InOrStdin()), "stdin", nil | ||||||
} | ||||||
reader, err := os.Open(args[0]) | ||||||
return reader, args[0], err | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if this lacks context, or has already been discussed and discarded, but would it make sense to move this, and maybe other generally useful, FBC functions to the O-F API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once we get some more use/feedback/maturity in the interface, the plan is to eventually promote the catalog templates out of alpha and into pkg.