feat(commands/midjourney/collections): add list and get commands

This commit is contained in:
2022-12-11 22:21:08 +00:00
parent 270565b2de
commit 8947d48592
3 changed files with 112 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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)
}
}

View File

@@ -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)
}
}