Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: everettraven <[email protected]>
  • Loading branch information
everettraven committed Jan 31, 2024
1 parent 2755ba8 commit 332ffcd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 81 deletions.
69 changes: 36 additions & 33 deletions internal/cmd/internal/olmv1/catalog/alpha_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import (
"github.com/spf13/cobra"
)

var (
inspectCatalog = ""
inspectSchema = ""
inspectPackage = ""
inspectOutput = "json"
)
type InspectCommandOptions struct {
experimentalaction.CatalogInspectOptions
Output string
}

func NewInspectCommand(cfg *action.Configuration) *cobra.Command {
i := experimentalaction.NewCatalogInspect(cfg,
Expand All @@ -32,48 +30,53 @@ func NewInspectCommand(cfg *action.Configuration) *cobra.Command {
)
i.Logf = log.Printf

inspectOpts := InspectCommandOptions{}

cmd := &cobra.Command{
Use: "inspect [name]",
Short: "inspect catalog objects",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts := &experimentalaction.CatalogInspectOptions{
Name: args[0],
Catalog: inspectCatalog,
Package: inspectPackage,
Schema: inspectSchema,
}
inspectOpts.Name = args[0]

metas, err := i.Run(cmd.Context(), *opts)
metas, err := i.Run(cmd.Context(), inspectOpts.CatalogInspectOptions)
if err != nil {
return err
}

for _, meta := range metas {
var outBytes []byte
switch inspectOutput {
case "json":
outBytes, err = json.MarshalIndent(meta.Meta, "", " ")
if err != nil {
return fmt.Errorf("marshalling output: %w", err)
}
case "yaml":
outBytes, err = yaml.Marshal(meta.Meta)
if err != nil {
return fmt.Errorf("marshalling output: %w", err)
}
}

fmt.Println(string(outBytes))
if err := renderInspectOutput(metas, inspectOpts.Output); err != nil {
return err
}
return nil
},
}

cmd.Flags().StringVar(&inspectCatalog, "catalog", "", "filter results to only be from the specified catalog")
cmd.Flags().StringVar(&inspectSchema, "schema", "", "filter results to only be FBC objects that have the specified schema")
cmd.Flags().StringVar(&inspectPackage, "package", "", "filter results to only be FBC objects that belong to the specified package")
cmd.Flags().StringVar(&inspectOutput, "output", "json", "the format in which output should be. One of [json, yaml]")
cmd.Flags().StringVar(&inspectOpts.Catalog, "catalog", "", "filter results to only be from the specified catalog")
cmd.Flags().StringVar(&inspectOpts.Schema, "schema", "", "filter results to only be FBC objects that have the specified schema")
cmd.Flags().StringVar(&inspectOpts.Package, "package", "", "filter results to only be FBC objects that belong to the specified package")
cmd.Flags().StringVar(&inspectOpts.Output, "output", "json", "the format in which output should be. One of [json, yaml]")

return cmd
}

func renderInspectOutput(metas []experimentalaction.Meta, format string) error {
for _, meta := range metas {
var outBytes []byte
var err error
switch format {
case "json":
outBytes, err = json.MarshalIndent(meta.Meta, "", " ")
if err != nil {
return fmt.Errorf("marshalling output: %w", err)
}
case "yaml":
outBytes, err = yaml.Marshal(meta.Meta)
if err != nil {
return fmt.Errorf("marshalling output: %w", err)
}
}

fmt.Println(string(outBytes))
}
return nil
}
48 changes: 20 additions & 28 deletions internal/cmd/internal/olmv1/catalog/alpha_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ import (
"github.com/spf13/cobra"
)

var (
listCatalog = ""
listSchema = ""
listPackage = ""
listName = "json"
)

func NewListCommand(cfg *action.Configuration) *cobra.Command {
i := experimentalaction.NewCatalogList(cfg,
func(c *action.Configuration) experimentalaction.CatalogFetcher {
Expand All @@ -31,39 +24,38 @@ func NewListCommand(cfg *action.Configuration) *cobra.Command {
)
i.Logf = log.Printf

listOpts := experimentalaction.CatalogListOptions{}

cmd := &cobra.Command{
Use: "list",
Short: "list catalog objects",
RunE: func(cmd *cobra.Command, args []string) error {
opts := &experimentalaction.CatalogListOptions{
Catalog: listCatalog,
Package: listPackage,
Schema: listSchema,
Name: listName,
}

metas, err := i.Run(cmd.Context(), *opts)
metas, err := i.Run(cmd.Context(), listOpts)
if err != nil {
return err
}

out := strings.Builder{}
for _, meta := range metas {
out.WriteString(CatalogNameStyle.Render(meta.Catalog) + " ")
out.WriteString(SchemaNameStyle.Render(meta.Schema) + " ")
out.WriteString(PackageNameStyle.Render(meta.Package) + " ")
out.WriteString(NameStyle.Render(meta.Name))
out.WriteString("\n")
}
fmt.Print(out.String())
renderListOutput(metas)
return nil
},
}

cmd.Flags().StringVar(&listCatalog, "catalog", "", "filter results to only be from the specified catalog")
cmd.Flags().StringVar(&listSchema, "schema", "", "filter results to only be FBC objects that have the specified schema")
cmd.Flags().StringVar(&listPackage, "package", "", "filter results to only be FBC objects that belong to the specified package")
cmd.Flags().StringVar(&listName, "name", "", "filter results to only be FBC objects with the specified name")
cmd.Flags().StringVar(&listOpts.Catalog, "catalog", "", "filter results to only be from the specified catalog")
cmd.Flags().StringVar(&listOpts.Schema, "schema", "", "filter results to only be FBC objects that have the specified schema")
cmd.Flags().StringVar(&listOpts.Package, "package", "", "filter results to only be FBC objects that belong to the specified package")
cmd.Flags().StringVar(&listOpts.Name, "name", "", "filter results to only be FBC objects with the specified name")

return cmd
}

func renderListOutput(metas []experimentalaction.Meta) {
out := strings.Builder{}
for _, meta := range metas {
out.WriteString(CatalogNameStyle.Render(meta.Catalog) + " ")
out.WriteString(SchemaNameStyle.Render(meta.Schema) + " ")
out.WriteString(PackageNameStyle.Render(meta.Package) + " ")
out.WriteString(NameStyle.Render(meta.Name))
out.WriteString("\n")
}
fmt.Print(out.String())
}
40 changes: 20 additions & 20 deletions internal/cmd/internal/olmv1/catalog/alpha_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,39 @@ func NewSearchCommand(cfg *action.Configuration) *cobra.Command {

i.Logf = log.Printf

searchOpts := experimentalaction.CatalogSearchOptions{}

cmd := &cobra.Command{
Use: "search [query]",
Short: "search for catalog objects",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts := &experimentalaction.CatalogSearchOptions{
Query: args[0],
Catalog: searchCatalog,
Schema: searchSchema,
Package: searchPackage,
}

metas, err := i.Run(cmd.Context(), *opts)
searchOpts.Query = args[0]
metas, err := i.Run(cmd.Context(), searchOpts)
if err != nil {
return err
}

out := strings.Builder{}
for _, meta := range metas {
out.WriteString(CatalogNameStyle.Render(meta.Catalog) + " ")
out.WriteString(SchemaNameStyle.Render(meta.Schema) + " ")
out.WriteString(PackageNameStyle.Render(meta.Package) + " ")
out.WriteString(NameStyle.Render(meta.Name))
out.WriteString("\n")
}
fmt.Print(out.String())
renderSearchOutput(metas)
return nil
},
}

cmd.Flags().StringVar(&searchCatalog, "catalog", "", "filter results to only be from the specified catalog")
cmd.Flags().StringVar(&searchSchema, "schema", "", "filter results to only be FBC objects that have the specified schema")
cmd.Flags().StringVar(&searchPackage, "package", "", "filter results to only be FBC objects that belong to the specified package")
cmd.Flags().StringVar(&searchOpts.Catalog, "catalog", "", "filter results to only be from the specified catalog")
cmd.Flags().StringVar(&searchOpts.Schema, "schema", "", "filter results to only be FBC objects that have the specified schema")
cmd.Flags().StringVar(&searchOpts.Package, "package", "", "filter results to only be FBC objects that belong to the specified package")

return cmd
}

func renderSearchOutput(metas []experimentalaction.Meta) {
out := strings.Builder{}
for _, meta := range metas {
out.WriteString(CatalogNameStyle.Render(meta.Catalog) + " ")
out.WriteString(SchemaNameStyle.Render(meta.Schema) + " ")
out.WriteString(PackageNameStyle.Render(meta.Package) + " ")
out.WriteString(NameStyle.Render(meta.Name))
out.WriteString("\n")
}
fmt.Print(out.String())
}

0 comments on commit 332ffcd

Please sign in to comment.