From dcace3b97bf696cb7e3ab7c1d294737b281bb08c Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Mon, 15 Jul 2024 18:22:39 -0500 Subject: [PATCH] chore(cmd)!: Remove `--file-format` flag --- cmd/cmd.go | 5 ++--- docs/cli-of-life.md | 1 - internal/config/completion.go | 5 ----- internal/config/config.go | 22 +++++++++++----------- internal/config/flags.go | 3 --- internal/pattern/pattern.go | 24 ++++++------------------ 6 files changed, 19 insertions(+), 41 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 2038c1a..f271442 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -51,16 +51,15 @@ func run(cmd *cobra.Command, _ []string) error { pat := pattern.Pattern{ Rule: rule, } - format := pattern.Format(conf.FileFormat) switch { case conf.File != "": var err error - if pat, err = pattern.UnmarshalFile(conf.File, format); err != nil { + if pat, err = pattern.UnmarshalFile(conf.File); err != nil { return err } case conf.URL != "": var err error - if pat, err = pattern.UnmarshalURL(context.Background(), conf.URL, format); err != nil { + if pat, err = pattern.UnmarshalURL(context.Background(), conf.URL); err != nil { return err } } diff --git a/docs/cli-of-life.md b/docs/cli-of-life.md index 3016c22..60d0eb7 100644 --- a/docs/cli-of-life.md +++ b/docs/cli-of-life.md @@ -11,7 +11,6 @@ cli-of-life [flags] ``` --completion string Output command-line completion code for the specified shell (one of: bash, zsh, fish, powershell) -f, --file string Load a pattern file - --file-format string File format (one of: auto, rle, plaintext) (default "auto") --height uint Board height. Will be ignored if a larger pattern is loaded. (default 600) -h, --help help for cli-of-life --play Play on startup diff --git a/internal/config/completion.go b/internal/config/completion.go index 411bf3f..14c049b 100644 --- a/internal/config/completion.go +++ b/internal/config/completion.go @@ -25,11 +25,6 @@ func RegisterCompletion(cmd *cobra.Command) error { return []string{pattern.ExtRLE, pattern.ExtPlaintext}, cobra.ShellCompDirectiveFilterFileExt }, ), - cmd.RegisterFlagCompletionFunc(FileFormatFlag, - func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { - return pattern.FormatStrings(), cobra.ShellCompDirectiveNoFileComp - }, - ), cmd.RegisterFlagCompletionFunc(RuleStringFlag, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { return []string{pattern.GameOfLife().String(), pattern.HighLife().String()}, cobra.ShellCompDirectiveNoFileComp diff --git a/internal/config/config.go b/internal/config/config.go index 99d25b4..a98f1a2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,22 +3,22 @@ package config import "github.com/gabe565/cli-of-life/internal/pattern" type Config struct { - File string - URL string - FileFormat string - RuleString string - Play bool - Width uint - Height uint + File string + URL string + PatternFormat string + RuleString string + Play bool + Width uint + Height uint Completion string } func New() *Config { return &Config{ - FileFormat: "auto", - RuleString: pattern.GameOfLife().String(), - Width: 600, - Height: 600, + PatternFormat: "auto", + RuleString: pattern.GameOfLife().String(), + Width: 600, + Height: 600, } } diff --git a/internal/config/flags.go b/internal/config/flags.go index 4f4398d..a9bc3f8 100644 --- a/internal/config/flags.go +++ b/internal/config/flags.go @@ -3,14 +3,12 @@ package config import ( "strings" - "github.com/gabe565/cli-of-life/internal/pattern" "github.com/spf13/pflag" ) const ( FileFlag = "file" URLFlag = "url" - FileFormatFlag = "file-format" RuleStringFlag = "rule-string" PlayFlag = "play" WidthFlag = "width" @@ -21,7 +19,6 @@ const ( func (c *Config) RegisterFlags(fs *pflag.FlagSet) { fs.StringVarP(&c.File, FileFlag, "f", c.File, "Load a pattern file") fs.StringVar(&c.URL, URLFlag, c.URL, "Load a pattern URL") - fs.StringVar(&c.FileFormat, FileFormatFlag, c.FileFormat, "File format (one of: "+strings.Join(pattern.FormatStrings(), ", ")+")") fs.StringVar(&c.RuleString, RuleStringFlag, c.RuleString, "Rule string to use. This will be ignored if a pattern file is loaded.") fs.BoolVar(&c.Play, PlayFlag, c.Play, "Play on startup") fs.UintVar(&c.Width, WidthFlag, c.Width, "Board width. Will be ignored if a larger pattern is loaded.") diff --git a/internal/pattern/pattern.go b/internal/pattern/pattern.go index 4c3bb4c..1388ec2 100644 --- a/internal/pattern/pattern.go +++ b/internal/pattern/pattern.go @@ -22,18 +22,6 @@ type Pattern struct { Rule Rule } -type Format string - -const ( - FormatAuto Format = "auto" - FormatRLE Format = "rle" - FormatPlaintext Format = "plaintext" -) - -func FormatStrings() []string { - return []string{string(FormatAuto), string(FormatRLE), string(FormatPlaintext)} -} - var ( ErrInvalidHeader = errors.New("invalid header") ErrPatternTooBig = errors.New("pattern too big") @@ -46,7 +34,7 @@ const ( ExtPlaintext = ".cells" ) -func UnmarshalFile(path string, format Format) (Pattern, error) { +func UnmarshalFile(path string) (Pattern, error) { f, err := os.Open(path) if err != nil { return Pattern{}, err @@ -57,9 +45,9 @@ func UnmarshalFile(path string, format Format) (Pattern, error) { ext := filepath.Ext(path) switch { - case format == FormatRLE, ext == ExtRLE: + case ext == ExtRLE: return UnmarshalRLE(f) - case format == FormatPlaintext, ext == ExtPlaintext: + case ext == ExtPlaintext: return UnmarshalPlaintext(f) default: pattern, err := Unmarshal(f) @@ -72,7 +60,7 @@ func UnmarshalFile(path string, format Format) (Pattern, error) { var ErrResponse = errors.New("received an error response") -func UnmarshalURL(ctx context.Context, url string, format Format) (Pattern, error) { +func UnmarshalURL(ctx context.Context, url string) (Pattern, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return Pattern{}, err @@ -93,9 +81,9 @@ func UnmarshalURL(ctx context.Context, url string, format Format) (Pattern, erro ext := path.Ext(url) switch { - case format == FormatRLE, ext == ExtRLE: + case ext == ExtRLE: return UnmarshalRLE(resp.Body) - case format == FormatPlaintext, ext == ExtPlaintext: + case ext == ExtPlaintext: return UnmarshalPlaintext(resp.Body) default: pattern, err := Unmarshal(resp.Body)