mirror of
https://github.com/jimeh/mje.git
synced 2026-02-19 09:56:41 +00:00
refactor: rename from mj2n to mje
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/jimeh/mj2n/midjourney"
|
||||
"github.com/jimeh/go-midjourney"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -9,15 +9,22 @@ func NewMidjourney(mc *midjourney.Client) (*cobra.Command, error) {
|
||||
cmd := &cobra.Command{
|
||||
Use: "midjourney",
|
||||
Aliases: []string{"mj"},
|
||||
Short: "MidJourney specific commands",
|
||||
Short: "Query the Midjourney API directly",
|
||||
}
|
||||
|
||||
recentJobsCmd, err := NewMidjourneyRecentJobs(mc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wordsCmd, err := NewMidjourneyWords(mc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmd.AddCommand(recentJobsCmd)
|
||||
cmd.AddCommand(
|
||||
recentJobsCmd,
|
||||
wordsCmd,
|
||||
)
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/jimeh/mj2n/midjourney"
|
||||
"time"
|
||||
|
||||
"github.com/jimeh/go-midjourney"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -18,7 +20,8 @@ func NewMidjourneyRecentJobs(mc *midjourney.Client) (*cobra.Command, error) {
|
||||
cmd.Flags().StringP("type", "t", "", "type of jobs to list")
|
||||
cmd.Flags().StringP("order", "o", "new", "either \"new\" or \"oldest\"")
|
||||
cmd.Flags().StringP("user-id", "u", "", "user ID to list jobs for")
|
||||
cmd.Flags().StringP("page", "p", "", "page to fetch")
|
||||
cmd.Flags().IntP("page", "p", 0, "page to fetch")
|
||||
cmd.Flags().StringP("prompt", "s", "", "prompt text to search for")
|
||||
cmd.Flags().Bool("dedupe", true, "dedupe results")
|
||||
|
||||
return cmd, nil
|
||||
@@ -44,6 +47,9 @@ func midjourneyRecentJobsRunE(mc *midjourney.Client) runEFunc {
|
||||
if v, err := fs.GetInt("page"); err == nil && v != 0 {
|
||||
q.Page = v
|
||||
}
|
||||
if v, err := fs.GetString("prompt"); err == nil && v != "" {
|
||||
q.Prompt = v
|
||||
}
|
||||
if v, err := fs.GetBool("dedupe"); err == nil {
|
||||
q.Dedupe = v
|
||||
}
|
||||
@@ -59,7 +65,7 @@ func midjourneyRecentJobsRunE(mc *midjourney.Client) runEFunc {
|
||||
ID: j.ID,
|
||||
Status: string(j.CurrentStatus),
|
||||
Type: string(j.Type),
|
||||
EnqueueTime: j.EnqueueTime,
|
||||
EnqueueTime: j.EnqueueTime.Time,
|
||||
Prompt: j.Prompt,
|
||||
ImagePaths: j.ImagePaths,
|
||||
IsPublished: j.IsPublished,
|
||||
@@ -76,15 +82,15 @@ func midjourneyRecentJobsRunE(mc *midjourney.Client) runEFunc {
|
||||
}
|
||||
|
||||
type MidjourneyJob struct {
|
||||
ID string `json:"id,omitempty" yaml:"id,omitempty"`
|
||||
Status string `json:"current_status,omitempty" yaml:"current_status,omitempty"`
|
||||
Type string `json:"type,omitempty" yaml:"type,omitempty"`
|
||||
EnqueueTime string `json:"enqueue_time,omitempty" yaml:"enqueue_time,omitempty"`
|
||||
Prompt string `json:"prompt,omitempty" yaml:"prompt,omitempty"`
|
||||
ImagePaths []string `json:"image_paths,omitempty" yaml:"image_paths,omitempty"`
|
||||
IsPublished bool `json:"is_published,omitempty" yaml:"is_published,omitempty"`
|
||||
UserID string `json:"user_id,omitempty" yaml:"user_id,omitempty"`
|
||||
Username string `json:"username,omitempty" yaml:"username,omitempty"`
|
||||
FullCommand string `json:"full_command,omitempty" yaml:"full_command,omitempty"`
|
||||
ReferenceJobID string `json:"reference_job_id,omitempty" yaml:"reference_job_id,omitempty"`
|
||||
ID string `json:"id,omitempty" yaml:"id,omitempty"`
|
||||
Status string `json:"current_status,omitempty" yaml:"current_status,omitempty"`
|
||||
Type string `json:"type,omitempty" yaml:"type,omitempty"`
|
||||
EnqueueTime time.Time `json:"enqueue_time,omitempty" yaml:"enqueue_time,omitempty"`
|
||||
Prompt string `json:"prompt,omitempty" yaml:"prompt,omitempty"`
|
||||
ImagePaths []string `json:"image_paths,omitempty" yaml:"image_paths,omitempty"`
|
||||
IsPublished bool `json:"is_published,omitempty" yaml:"is_published,omitempty"`
|
||||
UserID string `json:"user_id,omitempty" yaml:"user_id,omitempty"`
|
||||
Username string `json:"username,omitempty" yaml:"username,omitempty"`
|
||||
FullCommand string `json:"full_command,omitempty" yaml:"full_command,omitempty"`
|
||||
ReferenceJobID string `json:"reference_job_id,omitempty" yaml:"reference_job_id,omitempty"`
|
||||
}
|
||||
|
||||
70
commands/midjourney_words.go
Normal file
70
commands/midjourney_words.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/jimeh/go-midjourney"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewMidjourneyWords(mc *midjourney.Client) (*cobra.Command, error) {
|
||||
cmd := &cobra.Command{
|
||||
Use: "words",
|
||||
Aliases: []string{"w"},
|
||||
Short: "Get dictionary words",
|
||||
RunE: midjourneyWordsRunE(mc),
|
||||
}
|
||||
|
||||
cmd.Flags().StringP("format", "f", "", "output format (yaml or json)")
|
||||
cmd.Flags().StringP("query", "q", "", "query to search for")
|
||||
cmd.Flags().IntP("amount", "a", 50, "amount of words to fetch")
|
||||
cmd.Flags().IntP("page", "p", 0, "page to fetch")
|
||||
cmd.Flags().IntP("seed", "s", 0, "seed")
|
||||
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
func midjourneyWordsRunE(mc *midjourney.Client) runEFunc {
|
||||
return func(cmd *cobra.Command, _ []string) error {
|
||||
fs := cmd.Flags()
|
||||
q := &midjourney.WordsQuery{}
|
||||
|
||||
if v, err := fs.GetString("query"); err == nil && v != "" {
|
||||
q.Query = v
|
||||
}
|
||||
if v, err := fs.GetInt("amount"); err == nil && v > 0 {
|
||||
q.Amount = v
|
||||
}
|
||||
if v, err := fs.GetInt("page"); err == nil && v != 0 {
|
||||
q.Page = v
|
||||
}
|
||||
if v, err := fs.GetInt("seed"); err == nil && v != 0 {
|
||||
q.Seed = v
|
||||
}
|
||||
|
||||
words, err := mc.Words(cmd.Context(), q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r := []*MidjourneyWord{}
|
||||
for _, w := range words {
|
||||
r = append(r, &MidjourneyWord{
|
||||
Word: w.Word,
|
||||
ImageURL: w.ImageURL(),
|
||||
})
|
||||
}
|
||||
|
||||
format := flagString(cmd, "format")
|
||||
sort.SliceStable(r, func(i, j int) bool {
|
||||
return r[i].Word < r[j].Word
|
||||
})
|
||||
|
||||
return render(cmd.OutOrStdout(), format, r)
|
||||
}
|
||||
}
|
||||
|
||||
type MidjourneyWord struct {
|
||||
Word string `json:"word"`
|
||||
ImageURL string `json:"image_url"`
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/jimeh/mj2n/midjourney"
|
||||
"github.com/jimeh/go-midjourney"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -12,27 +13,43 @@ import (
|
||||
|
||||
type runEFunc func(cmd *cobra.Command, _ []string) error
|
||||
|
||||
func NewMJ2N() (*cobra.Command, error) {
|
||||
mc, err := midjourney.New(midjourney.WithUserAgent("mj2n/0.0.1-dev"))
|
||||
type Info struct {
|
||||
Version string
|
||||
Commit string
|
||||
Date string
|
||||
}
|
||||
|
||||
func New(info Info) (*cobra.Command, error) {
|
||||
if info.Version == "" {
|
||||
info.Version = "0.0.0-dev"
|
||||
}
|
||||
|
||||
mc, err := midjourney.New(midjourney.WithUserAgent("mje/" + info.Version))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "mj2n",
|
||||
Short: "MidJourney to Notion importer",
|
||||
Use: "mje",
|
||||
Short: "MidJourney exporter",
|
||||
Version: info.Version,
|
||||
PersistentPreRunE: persistentPreRunE(mc),
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().StringP(
|
||||
"log-level", "l", "info",
|
||||
cmd.PersistentFlags().String(
|
||||
"log-level", "info",
|
||||
"one of: trace, debug, info, warn, error, fatal, panic",
|
||||
)
|
||||
cmd.PersistentFlags().StringP(
|
||||
"mj-token", "m", "", "MidJourney API token",
|
||||
cmd.PersistentFlags().String(
|
||||
"log-format", "plain",
|
||||
"one of: plain, json",
|
||||
)
|
||||
cmd.PersistentFlags().String(
|
||||
"mj-api-url", midjourney.DefaultAPIURL.String(), "MidJourney API URL",
|
||||
"token", "", "MidJourney token",
|
||||
)
|
||||
cmd.PersistentFlags().String(
|
||||
"api-url", midjourney.DefaultAPIURL.String(),
|
||||
"MidJourney API URL",
|
||||
)
|
||||
|
||||
midjourneyCmd, err := NewMidjourney(mc)
|
||||
@@ -66,13 +83,13 @@ func setupMidJourney(cmd *cobra.Command, mc *midjourney.Client) error {
|
||||
midjourney.WithLogger(log.Logger),
|
||||
}
|
||||
|
||||
if f := cmd.Flag("mj-token"); f.Changed {
|
||||
if f := cmd.Flag("token"); f != nil && f.Changed {
|
||||
opts = append(opts, midjourney.WithAuthToken(f.Value.String()))
|
||||
} else if v := os.Getenv("MIDJOURNEY_TOKEN"); v != "" {
|
||||
opts = append(opts, midjourney.WithAuthToken(v))
|
||||
}
|
||||
|
||||
apiURL := flagString(cmd, "mj-api-url")
|
||||
apiURL := flagString(cmd, "api-url")
|
||||
if apiURL == "" {
|
||||
apiURL = os.Getenv("MIDJOURNEY_API_URL")
|
||||
}
|
||||
@@ -85,11 +102,16 @@ func setupMidJourney(cmd *cobra.Command, mc *midjourney.Client) error {
|
||||
|
||||
func setupZerolog(cmd *cobra.Command) error {
|
||||
var levelStr string
|
||||
if v := os.Getenv("MJ2N_DEBUG"); v != "" {
|
||||
var logFormat string
|
||||
|
||||
if v := os.Getenv("MJE_DEBUG"); v != "" {
|
||||
levelStr = "debug"
|
||||
} else if v := os.Getenv("MJ2N_LOG_LEVEL"); v != "" {
|
||||
} else if v := os.Getenv("MJE_LOG_LEVEL"); v != "" {
|
||||
levelStr = v
|
||||
}
|
||||
if v := os.Getenv("MJE_LOG_FORMAT"); v != "" {
|
||||
logFormat = v
|
||||
}
|
||||
|
||||
var out io.Writer = os.Stderr
|
||||
|
||||
@@ -99,27 +121,33 @@ func setupZerolog(cmd *cobra.Command) error {
|
||||
if fl != nil && (fl.Changed || levelStr == "") {
|
||||
levelStr = fl.Value.String()
|
||||
}
|
||||
|
||||
fl = cmd.Flag("log-format")
|
||||
if fl != nil && (fl.Changed || logFormat == "") {
|
||||
logFormat = fl.Value.String()
|
||||
}
|
||||
}
|
||||
|
||||
if levelStr == "" {
|
||||
levelStr = "info"
|
||||
}
|
||||
|
||||
level, err := zerolog.ParseLevel(levelStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
zerolog.SetGlobalLevel(level)
|
||||
zerolog.TimeFieldFormat = ""
|
||||
|
||||
output := zerolog.ConsoleWriter{Out: out}
|
||||
output.FormatTimestamp = func(i interface{}) string {
|
||||
return ""
|
||||
switch logFormat {
|
||||
case "plain":
|
||||
output := zerolog.ConsoleWriter{Out: out}
|
||||
output.FormatTimestamp = func(i interface{}) string { return "" }
|
||||
log.Logger = zerolog.New(output).With().Logger()
|
||||
case "json":
|
||||
log.Logger = zerolog.New(out).With().Timestamp().Logger()
|
||||
default:
|
||||
return fmt.Errorf("unknown log-format: %s", logFormat)
|
||||
}
|
||||
|
||||
log.Logger = zerolog.New(output).With().Timestamp().Logger()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func render(w io.Writer, format string, v interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return renderYAML(w, v)
|
||||
return renderJSON(w, v)
|
||||
}
|
||||
|
||||
func renderYAML(w io.Writer, v interface{}) error {
|
||||
|
||||
Reference in New Issue
Block a user