diff --git a/commands/midjourney/collections.go b/commands/midjourney/collections.go new file mode 100644 index 0000000..deb90de --- /dev/null +++ b/commands/midjourney/collections.go @@ -0,0 +1,29 @@ +package midjourney + +import ( + "github.com/jimeh/go-midjourney" + "github.com/spf13/cobra" +) + +func NewCollections(mc *midjourney.Client) (*cobra.Command, error) { + cmd := &cobra.Command{ + Use: "collections", + Aliases: []string{"collection", "col"}, + Short: "Query collections", + } + + listCmd, err := NewCollectionsList(mc) + if err != nil { + return nil, err + } + getCmd, err := NewCollectionsGet(mc) + if err != nil { + return nil, err + } + cmd.AddCommand( + listCmd, + getCmd, + ) + + return cmd, nil +} diff --git a/commands/midjourney/collections_get.go b/commands/midjourney/collections_get.go new file mode 100644 index 0000000..c9cb475 --- /dev/null +++ b/commands/midjourney/collections_get.go @@ -0,0 +1,37 @@ +package midjourney + +import ( + "github.com/jimeh/go-midjourney" + "github.com/jimeh/mje/commands/render" + "github.com/jimeh/mje/commands/shared" + "github.com/spf13/cobra" +) + +func NewCollectionsGet(mc *midjourney.Client) (*cobra.Command, error) { + cmd := &cobra.Command{ + Use: "get collection_id", + Short: "Get a collection", + RunE: collectionsGetRunE(mc), + ArgAliases: []string{"collection_id"}, + Args: cobra.ExactArgs(1), + } + + return cmd, nil +} + +func collectionsGetRunE(mc *midjourney.Client) shared.RunEFunc { + return func(cmd *cobra.Command, args []string) error { + q := &midjourney.CollectionsQuery{ + CollectionID: args[0], + } + + cols, err := mc.Collections(cmd.Context(), q) + if err != nil { + return err + } + + format := shared.FlagString(cmd, "format") + + return render.Render(cmd.OutOrStdout(), format, cols) + } +} diff --git a/commands/midjourney/collections_list.go b/commands/midjourney/collections_list.go new file mode 100644 index 0000000..4a95d98 --- /dev/null +++ b/commands/midjourney/collections_list.go @@ -0,0 +1,46 @@ +package midjourney + +import ( + "github.com/jimeh/go-midjourney" + "github.com/jimeh/mje/commands/render" + "github.com/jimeh/mje/commands/shared" + "github.com/spf13/cobra" +) + +func NewCollectionsList(mc *midjourney.Client) (*cobra.Command, error) { + cmd := &cobra.Command{ + Use: "list", + Aliases: []string{"ls"}, + Short: "List collections", + RunE: collectionsListRunE(mc), + } + + cmd.Flags().StringP("user-id", "u", "", "user ID to list jobs for") + cmd.Flags().StringP( + "collection-id", "c", "", "collection ID to list jobs for", + ) + + return cmd, nil +} + +func collectionsListRunE(mc *midjourney.Client) shared.RunEFunc { + return func(cmd *cobra.Command, _ []string) error { + fs := cmd.Flags() + q := &midjourney.CollectionsQuery{} + + if v, err := fs.GetString("user-id"); err == nil && v != "" { + q.UserID = v + } + if v, err := fs.GetString("collection-id"); err == nil && v != "" { + q.CollectionID = v + } + cols, err := mc.Collections(cmd.Context(), q) + if err != nil { + return err + } + + format := shared.FlagString(cmd, "format") + + return render.Render(cmd.OutOrStdout(), format, cols) + } +}