-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide required schema for basic template and fbc conversion (#1335)
- Loading branch information
Showing
10 changed files
with
218 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
runCmd.AddCommand(bc) | ||
|
||
return runCmd | ||
} | ||
sc := newSemverTemplateCmd() | ||
// sc.Hidden = true | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters