Skip to content

Commit

Permalink
Refactor create code in main function
Browse files Browse the repository at this point in the history
  • Loading branch information
akalin committed Apr 4, 2021
1 parent 5fecbc6 commit 9fd6245
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
33 changes: 21 additions & 12 deletions cmd/par/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,28 +400,37 @@ func main() {
allFiles := createFlagSet.Args()
parFile, filePaths := allFiles[0], allFiles[1:]

ext := path.Ext(parFile)
if ext == ".par" {
err = par1.Create(parFile, filePaths, par1.CreateOptions{
switch ext := path.Ext(parFile); ext {
case ".par":
err := par1.Create(parFile, filePaths, par1.CreateOptions{

NumParityFiles: createFlags.numParityShards,
CreateDelegate: par1LogCreateDelegate{},
})
} else if ext == ".par2" {
err = par2.Create(parFile, filePaths, par2.CreateOptions{
if err != nil {
fmt.Printf("Write parity error: %s\n", err)
os.Exit(par2cmdline.ExitLogicError)
}
os.Exit(par2cmdline.ExitSuccess)

case ".par2":
err := par2.Create(parFile, filePaths, par2.CreateOptions{
SliceByteCount: createFlags.sliceByteCount,
NumParityShards: createFlags.numParityShards,
NumGoroutines: globalFlags.numGoroutines,
CreateDelegate: par2LogCreateDelegate{},
})
} else {
err = fmt.Errorf("unknown extension %s", ext)
}
// TODO: Pull this out into a utility function.
if err != nil {
if err != nil {
fmt.Printf("Write parity error: %s\n", err)
os.Exit(par2.ExitCodeForCreateErrorPar2CmdLine(err))
}
os.Exit(par2cmdline.ExitSuccess)

default:
err := fmt.Errorf("unknown extension %s", ext)
fmt.Printf("Write parity error: %s\n", err)
os.Exit(par2cmdline.ExitFileIOError)
os.Exit(par2cmdline.ExitLogicError)
}
os.Exit(par2cmdline.ExitSuccess)

case "v":
fallthrough
Expand Down
12 changes: 12 additions & 0 deletions par2/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path"
"path/filepath"

"github.com/akalin/gopar/par2cmdline"
"github.com/akalin/gopar/rsec16"
)

Expand Down Expand Up @@ -125,3 +126,14 @@ func create(fileIO fileIO, parPath string, filePaths []string, options CreateOpt
}
return encoder.Write(parPath)
}

// ExitCodeForCreateErrorPar2CmdLine returns the error code
// par2cmdline would have returned for the given error returned by
// Create.
func ExitCodeForCreateErrorPar2CmdLine(err error) int {
if err != nil {
// Map everything to eFileIOError for now.
return par2cmdline.ExitFileIOError
}
return par2cmdline.ExitSuccess
}

0 comments on commit 9fd6245

Please sign in to comment.