Skip to content

Commit

Permalink
refactor: Convert capitalization to TextVar flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cetteup committed Jan 5, 2024
1 parent eb011d8 commit a2c2f18
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmd/update-plex-ipv6-access-url/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Config struct {
AddrPreference handler.AddrPreference
ConfigPath string
Token string
Capitalization string
Capitalization handler.IPv6URLCapitalization
Timeout int
}

Expand All @@ -38,7 +38,7 @@ func Init() *Config {
flag.TextVar(&cfg.AddrPreference, "use", handler.AddrPreferenceFirst, "Which IPv6 address(es) to use if multiple are found on the interface (first|last|all)")
flag.StringVar(&cfg.ConfigPath, "config", "", "Path to Plex config (Preferences.xml)")
flag.StringVar(&cfg.Token, "token", "", "Plex access token (X-Plex-Token) [required if 'config' flag is/cannot be provided]")
flag.StringVar(&cfg.Capitalization, "capitalization", "", "Capitalization to use for dashed IPv6 address in Plex custom access URL (upper|lower)")
flag.TextVar(&cfg.Capitalization, "capitalization", handler.IPv6URLCapitalizationLower, "Capitalization to use for dashed IPv6 address in Plex custom access URL (upper|lower)")
flag.IntVar(&cfg.Timeout, "timeout", 5, "Plex API request timeout (in seconds)")
flag.Parse()
return cfg
Expand Down
42 changes: 42 additions & 0 deletions cmd/update-plex-ipv6-access-url/internal/handler/capitalization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package handler

import (
"fmt"
)

type IPv6URLCapitalization string

const (
IPv6URLCapitalizationLower IPv6URLCapitalization = "lower"
IPv6URLCapitalizationUpper IPv6URLCapitalization = "upper"
)

//goland:noinspection GoMixedReceiverTypes
func (c IPv6URLCapitalization) String() string {
return string(c)
}

//goland:noinspection GoMixedReceiverTypes
func (c *IPv6URLCapitalization) UnmarshalText(text []byte) error {
if len(text) == 0 {
*c = ""
return nil
}

s := string(text)
switch s {
case string(IPv6URLCapitalizationLower):
*c = IPv6URLCapitalizationLower
case string(IPv6URLCapitalizationUpper):
*c = IPv6URLCapitalizationUpper
default:
return fmt.Errorf("invalid IPv6 URL capitalization: %s", s)
}

return nil
}

//goland:noinspection GoMixedReceiverTypes
func (c IPv6URLCapitalization) MarshalText() (text []byte, err error) {
return []byte(c), nil
}
7 changes: 0 additions & 7 deletions cmd/update-plex-ipv6-access-url/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ import (
"github.com/cetteup/update-plex-ipv6-access-url/internal/plex"
)

type IPv6URLCapitalization string

const (
IPv6URLCapitalizationLower IPv6URLCapitalization = "lower"
IPv6URLCapitalizationUpper IPv6URLCapitalization = "upper"
)

type ApiClient interface {
GetIdentity() (plex.IdentityDTO, error)
GetResources() (plex.ResourcesDTO, error)
Expand Down
4 changes: 2 additions & 2 deletions cmd/update-plex-ipv6-access-url/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
remoteClient := plex.NewApiClient(plex.BaseURL, cfg.Token, cfg.Timeout)
h := handler.NewHandler(localClient, remoteClient)

selectedAddrs, err := h.SelectAddrs(interfaceAddrs, handler.AddrPreference(cfg.AddrPreference))
selectedAddrs, err := h.SelectAddrs(interfaceAddrs, cfg.AddrPreference)
if err != nil {
log.Fatal().
Err(err).
Expand All @@ -86,7 +86,7 @@ func main() {
Msg("Selected IPv6 addresses")
}

err = h.UpdateIPv6CustomAccessURLs(selectedAddrs, handler.IPv6URLCapitalization(cfg.Capitalization))
err = h.UpdateIPv6CustomAccessURLs(selectedAddrs, cfg.Capitalization)
if err != nil {
log.Fatal().
Err(err).
Expand Down

0 comments on commit a2c2f18

Please sign in to comment.