Skip to content

Commit

Permalink
chore(cmd)!: Remove --file-format flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 15, 2024
1 parent 2be8e5c commit dcace3b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 41 deletions.
5 changes: 2 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
1 change: 0 additions & 1 deletion docs/cli-of-life.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions internal/config/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
3 changes: 0 additions & 3 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.")
Expand Down
24 changes: 6 additions & 18 deletions internal/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit dcace3b

Please sign in to comment.