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 May 17, 2021
1 parent 5e63497 commit d4eab3b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
36 changes: 23 additions & 13 deletions cmd/par/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ func printUsageAndExit(name string, mask commandMask, err error) {
os.Exit(par2cmdline.ExitSuccess)
}

func printCreateErrorAndExit(err error, exitCode int) {
fmt.Printf("Create error: %s\n", err)
os.Exit(exitCode)
}

type decoder interface {
LoadFileData() error
LoadParityData() error
Expand Down Expand Up @@ -400,28 +405,33 @@ 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 {
printCreateErrorAndExit(err, 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 {
fmt.Printf("Write parity error: %s\n", err)
os.Exit(par2cmdline.ExitFileIOError)
if err != nil {
printCreateErrorAndExit(err, par2.ExitCodeForCreateErrorPar2CmdLine(err))
}
os.Exit(par2cmdline.ExitSuccess)

default:
printCreateErrorAndExit(fmt.Errorf("unknown extension %s", ext), 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 d4eab3b

Please sign in to comment.