diff --git a/internal/cmd/internal/olmv1/catalog/alpha_inspect.go b/internal/cmd/internal/olmv1/catalog/alpha_inspect.go index c0d927a..e5f3a67 100644 --- a/internal/cmd/internal/olmv1/catalog/alpha_inspect.go +++ b/internal/cmd/internal/olmv1/catalog/alpha_inspect.go @@ -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, @@ -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 +} diff --git a/internal/cmd/internal/olmv1/catalog/alpha_list.go b/internal/cmd/internal/olmv1/catalog/alpha_list.go index 52d3251..2b0f80b 100644 --- a/internal/cmd/internal/olmv1/catalog/alpha_list.go +++ b/internal/cmd/internal/olmv1/catalog/alpha_list.go @@ -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 { @@ -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()) +} diff --git a/internal/cmd/internal/olmv1/catalog/alpha_search.go b/internal/cmd/internal/olmv1/catalog/alpha_search.go index 1ef9e25..190daa8 100644 --- a/internal/cmd/internal/olmv1/catalog/alpha_search.go +++ b/internal/cmd/internal/olmv1/catalog/alpha_search.go @@ -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()) +}