Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go-arcadium-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
go-arcadium-test:
strategy:
matrix:
go: [ '1.26.x' ]
go: [ '1.27.x' ]

runs-on: 'ubuntu-latest'

Expand Down
2 changes: 1 addition & 1 deletion src/go/arcadium/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,5 @@ dist/$(os)/$(arch):

clean:
@printf "\nClean...\n"
-go clean -testcache -cache
-go clean -i -r -cache -testcache -modcache -fuzzcache
-rm -rf dist
101 changes: 17 additions & 84 deletions src/go/arcadium/entropylex/cmd/entropylex/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ package main

import (
"context"
"errors"
"fmt"
"net/mail"
"os"
"path/filepath"

"github.com/AlphaPixel/EntropyLex/src/go/arcadium/build"
"github.com/urfave/cli/v3"

"github.com/AlphaPixel/EntropyLex/src/go/arcadium/build"
)

func NewCommand(info build.Information) *cli.Command {
Expand Down Expand Up @@ -76,13 +76,6 @@ non-empty output file exists, it can be overwritten using the -force option.
Aliases: []string{"b"},
Usage: "encoder bit depth, possible values are 8, 12, 14 or 16",
Value: 8,
Validator: func(i uint) error {
switch i {
case 8, 12, 14, 16:
return nil
}
return errors.New("possible values are 8, 12, 14 or 16")
},
},
&cli.StringFlag{
Name: "output",
Expand All @@ -97,13 +90,22 @@ non-empty output file exists, it can be overwritten using the -force option.
},

Action: func(ctx context.Context, cmd *cli.Command) error {
// Validate the bit depth.
bitDepth := cmd.Uint("bit-depth")
switch bitDepth {
case 8:
break
case 12, 14, 16:
return fmt.Errorf("%w: bit depth %d unimplemented", ErrUnimplemented, bitDepth)
default:
return fmt.Errorf("%w: invalid bit depth \"%d\", possible values are 8, 12, 14 or 16", ErrUsage, bitDepth)
}

// Setup the output.
outfile := os.Stdout
f, err := OutputFile(cmd.String("output"), cmd.Bool("force"))
if err != nil {
fmt.Fprintf(os.Stderr, "Incorrect Usage: %v\n\n", err)
_ = cli.DefaultShowRootCommandHelp(cmd)
return err
return fmt.Errorf("%w: %w", ErrUsage, err)
}
if f != nil {
outfile = f
Expand All @@ -114,16 +116,12 @@ non-empty output file exists, it can be overwritten using the -force option.
infile := os.Stdin
switch {
case cmd.NArg() > 1:
fmt.Fprintf(os.Stderr, "Incorrect Usage: extra input file \"%s\"\n\n", cmd.Args().Get(1))
_ = cli.DefaultShowRootCommandHelp(cmd)
return errors.New("usage error")
return fmt.Errorf("%w: extra input file \"%s\"", ErrUsage, cmd.Args().Get(1))
case cmd.NArg() == 1:
filename := cmd.Args().Get(0)
f, err := InputFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Incorrect Usage: %v\n\n", err)
_ = cli.DefaultShowRootCommandHelp(cmd)
return err
return fmt.Errorf("%w: %w", ErrUsage, err)
}
if f != nil {
infile = f
Expand All @@ -132,20 +130,13 @@ non-empty output file exists, it can be overwritten using the -force option.
}

// Are we encoding or decoding?
decode := cmd.Bool("decode")

var el runner
bitDepth := cmd.Uint("bit-depth")
switch bitDepth {
case 8:
el, err = NewEntropyLex8(infile, outfile, decode)
el, err = NewEntropyLex8(infile, outfile, cmd.Bool("decode"))
if err != nil {
return err
}
case 12, 14, 16:
return fmt.Errorf("bit depth %d unimplemented", bitDepth)
default:
return errors.New("usage error")
}

return el.Run(ctx)
Expand All @@ -154,61 +145,3 @@ non-empty output file exists, it can be overwritten using the -force option.

return cmd
}

// OutputFile creates an output file give the output filename and the force flag.
func OutputFile(filename string, force bool) (*os.File, error) {
if filename == "" {
return nil, nil
}

// See if the file exists. If not Create it.
fs, err := os.Stat(filename)
if err != nil {
return os.Create(filename)
}
mode := fs.Mode()

// If the file exists and it isn't a regular file, return an error.
if !mode.IsRegular() {
errmsg := fmt.Sprintf("output file \"%s\" is not a regular file", filename)
if mode.IsDir() {
errmsg = fmt.Sprintf("output file \"%s\" is a directory", filename)
}
return nil, errors.New(errmsg)
}

// If the file exists and it's a regular file, create it if the size is 0.
if fs.Size() == 0 {
return os.Create(filename)
}

// If the size is non-zero and the force flag isn't present, return an error.
if !force {
return nil, errors.New("a non-empty output file exist, to overwrite use the --force option")
}

return os.Create(filename)
}

// InputFile opens the input file for reading given the input filename.
func InputFile(filename string) (*os.File, error) {
if filename == "" || filename == "-" {
return nil, nil
}

fs, err := os.Stat(filename)
if err != nil {
return nil, err
}
mode := fs.Mode()

if !mode.IsRegular() {
errmsg := fmt.Sprintf("input file \"%s\" is not a regular file", filename)
if mode.IsDir() {
errmsg = fmt.Sprintf("input file \"%s\" is a directory", filename)
}
return nil, errors.New(errmsg)
}

return os.Open(filename)
}
Loading