Skip to content

Commit

Permalink
chore: Replace some logic with utils library
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Nov 13, 2024
1 parent b95f8a6 commit 4559631
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 162 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"gabe565.com/ascii-movie/cmd/ls"
"gabe565.com/ascii-movie/cmd/play"
"gabe565.com/ascii-movie/cmd/serve"
"gabe565.com/ascii-movie/cmd/util"
"gabe565.com/ascii-movie/internal/config"
"gabe565.com/utils/cobrax"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)

func NewCommand(opts ...util.Option) *cobra.Command {
func NewCommand(opts ...cobrax.Option) *cobra.Command {
cmd := &cobra.Command{
Use: "ascii-movie",
Short: "Command line ASCII movie player.",
Expand Down
10 changes: 4 additions & 6 deletions cmd/get/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"net/url"

"gabe565.com/ascii-movie/cmd/get/stream"
"gabe565.com/ascii-movie/cmd/util"
"gabe565.com/ascii-movie/internal/config"
"gabe565.com/ascii-movie/internal/server"
"gabe565.com/utils/cobrax"
"gabe565.com/utils/must"
"github.com/spf13/cobra"
)

func NewCommand(opts ...util.Option) *cobra.Command {
func NewCommand(opts ...cobrax.Option) *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Fetches data from a running server.",
Expand All @@ -31,10 +32,7 @@ func NewCommand(opts ...util.Option) *cobra.Command {
}

func preRun(cmd *cobra.Command, _ []string) error {
apiAddr, err := cmd.Flags().GetString(server.APIFlagPrefix + server.AddressFlag)
if err != nil {
panic(err)
}
apiAddr := must.Must2(cmd.Flags().GetString(server.APIFlagPrefix + server.AddressFlag))

u, err := url.Parse(apiAddr)
if err != nil {
Expand Down
16 changes: 6 additions & 10 deletions cmd/get/stream/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"text/tabwriter"
"time"

"gabe565.com/ascii-movie/cmd/util"
"gabe565.com/ascii-movie/internal/config"
"gabe565.com/ascii-movie/internal/server"
"gabe565.com/utils/cobrax"
"gabe565.com/utils/must"
"github.com/spf13/cobra"
)

Expand All @@ -21,7 +22,7 @@ const (
CountTotal = "total"
)

func NewCommand(opts ...util.Option) *cobra.Command {
func NewCommand(opts ...cobrax.Option) *cobra.Command {
cmd := &cobra.Command{
Use: "stream",
Aliases: []string{"streams", "connection", "connections", "client", "clients"},
Expand Down Expand Up @@ -59,9 +60,7 @@ func preRun(cmd *cobra.Command, args []string) error {
count = "total"
}
if count != "" {
if err := cmd.Flags().Set("count", count); err != nil {
panic(err)
}
must.Must(cmd.Flags().Set("count", count))
}
}
return nil
Expand All @@ -74,17 +73,14 @@ var (
)

func run(cmd *cobra.Command, _ []string) error {
countFlag, err := cmd.Flags().GetString("count")
if err != nil {
panic(err)
}
countFlag := must.Must2(cmd.Flags().GetString("count"))

u, ok := cmd.Context().Value(config.URLContextKey).(*url.URL)
if !ok {
panic(fmt.Errorf("%w: %v", ErrInvalidURL, u))
}

u, err = u.Parse("/streams")
u, err := u.Parse("/streams")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ls/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"text/tabwriter"
"time"

cmdutil "gabe565.com/ascii-movie/cmd/util"
"gabe565.com/ascii-movie/internal/movie"
"gabe565.com/ascii-movie/internal/util"
"gabe565.com/utils/cobrax"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)

func NewCommand(opts ...cmdutil.Option) *cobra.Command {
func NewCommand(opts ...cobrax.Option) *cobra.Command {
cmd := &cobra.Command{
Use: "ls [PATH]...",
Aliases: []string{"ls-embedded"},
Expand Down
4 changes: 2 additions & 2 deletions cmd/play/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package play
import (
"log/slog"

"gabe565.com/ascii-movie/cmd/util"
"gabe565.com/ascii-movie/internal/config"
"gabe565.com/ascii-movie/internal/movie"
"gabe565.com/ascii-movie/internal/player"
"gabe565.com/utils/cobrax"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)

func NewCommand(opts ...util.Option) *cobra.Command {
func NewCommand(opts ...cobrax.Option) *cobra.Command {
cmd := &cobra.Command{
Use: "play [movie]",
Short: "Play an ASCII movie locally.",
Expand Down
8 changes: 4 additions & 4 deletions cmd/serve/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"os/signal"
"syscall"

"gabe565.com/ascii-movie/cmd/util"
"gabe565.com/ascii-movie/internal/movie"
"gabe565.com/ascii-movie/internal/server"
"gabe565.com/utils/cobrax"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

func NewCommand(opts ...util.Option) *cobra.Command {
func NewCommand(opts ...cobrax.Option) *cobra.Command {
cmd := &cobra.Command{
Use: "serve [movie]",
Aliases: []string{"server", "listen"},
Expand All @@ -43,8 +43,8 @@ var ErrAllDisabled = errors.New("all server types are disabled")
func run(cmd *cobra.Command, args []string) error {
if parent := cmd.Parent(); parent != nil {
slog.Info("ASCII Movie",
"version", parent.Annotations["version"],
"commit", parent.Annotations["commit"],
"version", cobrax.GetVersion(cmd),
"commit", cobrax.GetCommit(cmd),
)
}

Expand Down
21 changes: 0 additions & 21 deletions cmd/util/options.go

This file was deleted.

31 changes: 0 additions & 31 deletions cmd/util/version.go

This file was deleted.

12 changes: 0 additions & 12 deletions cmd/util/version_test.go

This file was deleted.

5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module gabe565.com/ascii-movie
go 1.23.3

require (
gabe565.com/utils v0.0.0-20241111053222-0f59399cbb3c
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/bubbletea v1.2.2
github.com/charmbracelet/lipgloss v1.0.0
Expand All @@ -12,7 +13,6 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/lmittmann/tint v1.0.5
github.com/lrstanley/bubblezone v0.0.0-20240914071701-b48c55a5e78e
github.com/mattn/go-isatty v0.0.20
github.com/muesli/termenv v0.15.3-0.20240509142007-81b8f94111d5
github.com/prometheus/client_golang v1.20.5
github.com/spf13/cobra v1.8.1
Expand All @@ -36,14 +36,15 @@ require (
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/charmbracelet/x/termios v0.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/creack/pty v1.1.21 // indirect
github.com/creack/pty v1.1.24 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
gabe565.com/utils v0.0.0-20241111053222-0f59399cbb3c h1:xKxZ9fGvYYihnqbyt1OE5Giz+rDQf5junHUxmvkFBfw=
gabe565.com/utils v0.0.0-20241111053222-0f59399cbb3c/go.mod h1:ekV9VNVFXI1E8niHsgfb76RQHP2oJH2zYaCw1cEWrhA=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
Expand Down Expand Up @@ -35,8 +37,8 @@ github.com/charmbracelet/x/termios v0.1.0/go.mod h1:H/EVv/KRnrYjz+fCYa9bsKdqF3S8
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dmarkham/enumer v1.5.10 h1:ygL0L6quiTiH1jpp68DyvsWaea6MaZLZrTTkIS++R0M=
Expand Down
54 changes: 21 additions & 33 deletions internal/config/log.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package config

import (
"errors"
"io"
"log/slog"
"os"
"strconv"
"strings"
"time"

"gabe565.com/utils/must"
"gabe565.com/utils/termx"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
)

Expand All @@ -35,32 +34,26 @@ func RegisterLogFlags(cmd *cobra.Command) {
cmd.PersistentFlags().StringP(LogLevelFlag, "l", strings.ToLower(slog.LevelInfo.String()), "log level (one of debug, info, warn, error)")
cmd.PersistentFlags().String(LogFormatFlag, FormatAuto.String(), "log formatter (one of "+strings.Join(LogFormatStrings(), ", ")+")")

if err := errors.Join(
cmd.RegisterFlagCompletionFunc(LogLevelFlag,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{
strings.ToLower(slog.LevelDebug.String()),
strings.ToLower(slog.LevelInfo.String()),
strings.ToLower(slog.LevelWarn.String()),
strings.ToLower(slog.LevelError.String()),
}, cobra.ShellCompDirectiveNoFileComp
},
),
cmd.RegisterFlagCompletionFunc(LogFormatFlag,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return LogFormatStrings(), cobra.ShellCompDirectiveNoFileComp
},
),
); err != nil {
panic(err)
}
must.Must(cmd.RegisterFlagCompletionFunc(LogLevelFlag,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{
strings.ToLower(slog.LevelDebug.String()),
strings.ToLower(slog.LevelInfo.String()),
strings.ToLower(slog.LevelWarn.String()),
strings.ToLower(slog.LevelError.String()),
}, cobra.ShellCompDirectiveNoFileComp
},
))

must.Must(cmd.RegisterFlagCompletionFunc(LogFormatFlag,
func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return LogFormatStrings(), cobra.ShellCompDirectiveNoFileComp
},
))
}

func InitLogCmd(cmd *cobra.Command) {
levelStr, err := cmd.Flags().GetString("log-level")
if err != nil {
panic(err)
}
levelStr := must.Must2(cmd.Flags().GetString("log-level"))
var level slog.Level
if v, err := strconv.Atoi(levelStr); err == nil {
level = slog.Level(v)
Expand All @@ -71,10 +64,7 @@ func InitLogCmd(cmd *cobra.Command) {
level = slog.LevelInfo
}

formatStr, err := cmd.Flags().GetString("log-format")
if err != nil {
panic(err)
}
formatStr := must.Must2(cmd.Flags().GetString("log-format"))
var format LogFormat
if err := format.UnmarshalText([]byte(formatStr)); err != nil {
defer func() {
Expand All @@ -96,9 +86,7 @@ func InitLog(w io.Writer, level slog.Level, format LogFormat) {
var color bool
switch format {
case FormatAuto:
if f, ok := w.(*os.File); ok {
color = isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
}
color = termx.IsColor(w)
case FormatColor:
color = true
}
Expand Down
4 changes: 2 additions & 2 deletions internal/generate/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"

"gabe565.com/ascii-movie/cmd"
"gabe565.com/ascii-movie/cmd/util"
"gabe565.com/ascii-movie/internal/config"
"gabe565.com/utils/cobrax"
"github.com/spf13/cobra/doc"
)

Expand All @@ -31,7 +31,7 @@ func run() error {
return fmt.Errorf("failed to create output directory: %w", err)
}

rootCmd := cmd.NewCommand(util.WithVersion("beta"))
rootCmd := cmd.NewCommand(cobrax.WithVersion("beta"))
if err := doc.GenMarkdownTree(rootCmd, output); err != nil {
return fmt.Errorf("failed to generate docs: %w", err)
}
Expand Down
Loading

0 comments on commit 4559631

Please sign in to comment.