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 Dec 15, 2024
1 parent 01e745b commit 6dbf960
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ builds:
ldflags:
- -s
- -w
- -X main.version=v{{.Version}}
- -X main.version={{.Version}}
goarch:
- amd64
- arm
Expand Down
12 changes: 3 additions & 9 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
"gabe565.com/castsponsorskip/internal/config"
"gabe565.com/castsponsorskip/internal/device"
"gabe565.com/castsponsorskip/internal/youtube"
"gabe565.com/utils/cobrax"
"github.com/spf13/cobra"
)

//go:embed description.md
var long string

func New(opts ...Option) *cobra.Command {
func New(opts ...cobrax.Option) *cobra.Command {
cmd := &cobra.Command{
Use: "castsponsorskip",
Short: "Skip sponsored YouTube segments on local Cast devices",
Expand All @@ -37,7 +38,6 @@ func New(opts ...Option) *cobra.Command {
config.InitLog(cmd.ErrOrStderr(), slog.LevelInfo, config.FormatAuto)
config.RegisterFlags(cmd)
config.RegisterCompletions(cmd)
CompletionFlag(cmd)

for _, opt := range opts {
opt(cmd)
Expand All @@ -57,15 +57,9 @@ func preRun(cmd *cobra.Command, _ []string) error {
}

func run(cmd *cobra.Command, _ []string) error {
if shell, err := cmd.Flags().GetString("completion"); err != nil {
panic(err)
} else if shell != "" {
return completion(cmd, shell)
}

conf := config.FromContext(cmd.Context())

slog.Info("CastSponsorSkip", "version", cmd.Annotations[VersionKey], "commit", cmd.Annotations[CommitKey])
slog.Info("CastSponsorSkip", "version", cobrax.GetVersion(cmd), "commit", cobrax.GetCommit(cmd))

ctx, cancel := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()
Expand Down
25 changes: 0 additions & 25 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"bytes"
"math/rand"
"net"
"strconv"
Expand Down Expand Up @@ -133,27 +132,3 @@ func TestSBCEnvs(t *testing.T) {
assert.Equal(t, []string{"a", "b", "c"}, conf.Categories)
assert.Equal(t, "AIzaSyDaGmWKa4JsXZ-HjGw7ISLn_3namBGewQe", conf.YouTubeAPIKey)
}

func TestCompletionFlag(t *testing.T) {
tests := []struct {
shell string
wantErr require.ErrorAssertionFunc
}{
{"bash", require.NoError},
{"zsh", require.NoError},
{"fish", require.NoError},
{"powershell", require.NoError},
{"invalid", require.Error},
}
for _, tt := range tests {
t.Run(tt.shell, func(t *testing.T) {
cmd := New()
cmd.SetArgs([]string{"--completion", tt.shell})

var buf bytes.Buffer
cmd.SetOut(&buf)
tt.wantErr(t, cmd.Execute())
assert.NotZero(t, buf.Bytes())
})
}
}
46 changes: 0 additions & 46 deletions cmd/flag_completion.go

This file was deleted.

22 changes: 0 additions & 22 deletions cmd/options.go

This file was deleted.

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

This file was deleted.

2 changes: 1 addition & 1 deletion docs/castsponsorskip.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ castsponsorskip [flags]
```
--action-types strings SponsorBlock action types to handle. Shorter segments that overlap with content can be muted instead of skipped. (default [skip,mute])
-c, --categories strings Comma-separated list of SponsorBlock categories to skip (default [sponsor])
--completion string Output command-line completion code for the specified shell. Can be 'bash', 'zsh', 'fish', or 'powershell'.
--completion string Generate the autocompletion script for the specified shell (one of bash, zsh, fish, powershell)
--config string Config file path
--devices strings Comma-separated list of device addresses. This will disable discovery and is not recommended unless discovery fails
--discover-interval duration Interval to restart the DNS discovery client (default 5m0s)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module gabe565.com/castsponsorskip
go 1.23.4

require (
gabe565.com/utils v0.0.0-20241213205714-152b8de1d3fe
github.com/buger/jsonparser v1.1.1
github.com/dmarkham/enumer v1.5.10
github.com/jedib0t/go-pretty/v6 v6.6.5
Expand All @@ -13,7 +14,6 @@ require (
github.com/knadh/koanf/providers/structs v0.1.0
github.com/knadh/koanf/v2 v2.1.2
github.com/lmittmann/tint v1.0.5
github.com/mattn/go-isatty v0.0.20
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.10.0
Expand Down Expand Up @@ -44,6 +44,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/miekg/dns v1.1.62 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ cloud.google.com/go/auth/oauth2adapt v0.2.6 h1:V6a6XDu2lTwPZWOawrAa9HUK+DB2zfJyT
cloud.google.com/go/auth/oauth2adapt v0.2.6/go.mod h1:AlmsELtlEBnaNTL7jCj8VQFLy6mbZv0s4Q7NGBeQ5E8=
cloud.google.com/go/compute/metadata v0.5.2 h1:UxK4uu/Tn+I3p2dYWTfiX4wva7aYlKixAHn3fyqngqo=
cloud.google.com/go/compute/metadata v0.5.2/go.mod h1:C66sj2AluDcIqakBq/M8lw8/ybHgOZqin2obFxa/E5k=
gabe565.com/utils v0.0.0-20241213205714-152b8de1d3fe h1:/QXzT2uIjZJBhJ8eADVxXMPZT716LpRVLj4TeZm+Qz0=
gabe565.com/utils v0.0.0-20241213205714-152b8de1d3fe/go.mod h1:6FZP8OeF0k3hTsPClyDGEILcBg3hvGcSp4u4pmjMusY=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
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.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down
52 changes: 24 additions & 28 deletions internal/config/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,40 @@ package config
import (
"context"
"encoding/json"
"errors"
"net"
"net/http"
"strings"
"time"

"gabe565.com/castsponsorskip/internal/config/names"
"gabe565.com/utils/must"
"github.com/spf13/cobra"
)

func RegisterCompletions(cmd *cobra.Command) {
if err := errors.Join(
cmd.RegisterFlagCompletionFunc(names.FlagLogLevel, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"debug", "info", "warn", "error", "none"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}),
cmd.RegisterFlagCompletionFunc(names.FlagNetworkInterface, completeNetworkInterface),
cmd.RegisterFlagCompletionFunc(names.FlagDiscoverInterval, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"5m", "10m", "15m"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}),
cmd.RegisterFlagCompletionFunc(names.FlagPausedInterval, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"1m", "2m", "5m", "10m", "30m", "1h"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}),
cmd.RegisterFlagCompletionFunc(names.FlagPlayingInterval, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"1s", "2s"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}),
cmd.RegisterFlagCompletionFunc(names.FlagSkipDelay, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"500ms", "1s", "2s", "3s", "5s", "10s"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}),
cmd.RegisterFlagCompletionFunc(names.FlagIgnoreSegmentDuration, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"30s", "1m", "2m", "5m"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}),
cmd.RegisterFlagCompletionFunc(names.FlagCategories, completeCategories),
cmd.RegisterFlagCompletionFunc(names.FlagActionTypes, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"skip", "mute"}, cobra.ShellCompDirectiveNoFileComp
}),
); err != nil {
panic(err)
}
must.Must(cmd.RegisterFlagCompletionFunc(names.FlagLogLevel, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"debug", "info", "warn", "error", "none"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}))
must.Must(cmd.RegisterFlagCompletionFunc(names.FlagNetworkInterface, completeNetworkInterface))
must.Must(cmd.RegisterFlagCompletionFunc(names.FlagDiscoverInterval, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"5m", "10m", "15m"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}))
must.Must(cmd.RegisterFlagCompletionFunc(names.FlagPausedInterval, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"1m", "2m", "5m", "10m", "30m", "1h"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}))
must.Must(cmd.RegisterFlagCompletionFunc(names.FlagPlayingInterval, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"1s", "2s"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}))
must.Must(cmd.RegisterFlagCompletionFunc(names.FlagSkipDelay, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"500ms", "1s", "2s", "3s", "5s", "10s"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}))
must.Must(cmd.RegisterFlagCompletionFunc(names.FlagIgnoreSegmentDuration, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"30s", "1m", "2m", "5m"}, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder
}))
must.Must(cmd.RegisterFlagCompletionFunc(names.FlagCategories, completeCategories))
must.Must(cmd.RegisterFlagCompletionFunc(names.FlagActionTypes, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"skip", "mute"}, cobra.ShellCompDirectiveNoFileComp
}))
}

func completeNetworkInterface(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

"gabe565.com/castsponsorskip/internal/config/names"
"gabe565.com/utils/cobrax"
"gabe565.com/utils/must"
"github.com/spf13/cobra"
castdns "github.com/vishen/go-chromecast/dns"
)
Expand Down Expand Up @@ -55,6 +57,7 @@ func New() *Config {
func RegisterFlags(cmd *cobra.Command) {
fs := cmd.Flags()
c := New()
must.Must(cobrax.RegisterCompletionFlag(cmd))

fs.String(names.FlagConfig, "", "Config file path")
fs.String(names.FlagLogLevel, c.LogLevel, "Log level (one of: debug, info, warn, error, none)")
Expand Down
5 changes: 2 additions & 3 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"gabe565.com/castsponsorskip/internal/config/names"
"gabe565.com/castsponsorskip/internal/config/sponsorblockcast"
"gabe565.com/utils/must"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
Expand All @@ -38,9 +39,7 @@ func Load(cmd *cobra.Command) (*Config, error) {
// Find config file
cfgFiles := make([]string, 0, 4)
var fileRequired bool
if cfgFile, err := cmd.Flags().GetString(names.FlagConfig); err != nil {
panic(err)
} else if cfgFile != "" {
if cfgFile := must.Must2(cmd.Flags().GetString(names.FlagConfig)); cfgFile != "" {
cfgFiles = append(cfgFiles, cfgFile)
fileRequired = true
} else {
Expand Down
7 changes: 2 additions & 5 deletions internal/config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package config
import (
"io"
"log/slog"
"os"
"time"

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

//go:generate go run github.com/dmarkham/enumer -type LogFormat -trimprefix Format -transform lower -text
Expand Down Expand Up @@ -51,9 +50,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
3 changes: 2 additions & 1 deletion internal/generate/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"gabe565.com/castsponsorskip/cmd"
"gabe565.com/castsponsorskip/internal/config"
"gabe565.com/castsponsorskip/internal/config/names"
"gabe565.com/utils/cobrax"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
Expand All @@ -32,7 +33,7 @@ func main() {
os.Exit(1)
}

root := cmd.New(cmd.WithVersion("beta"))
root := cmd.New(cobrax.WithVersion("beta"))

if err := errors.Join(
generateFlagDoc(root, output),
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"os"

"gabe565.com/castsponsorskip/cmd"
"gabe565.com/utils/cobrax"
)

var version = "beta"

func main() {
rootCmd := cmd.New(cmd.WithVersion(version))
rootCmd := cmd.New(cobrax.WithVersion(version))
if err := rootCmd.Execute(); err != nil {
slog.Error(err.Error())
os.Exit(1)
Expand Down

0 comments on commit 6dbf960

Please sign in to comment.