From 9fd6245323e54f9a3d229db874812b74d73192f5 Mon Sep 17 00:00:00 2001 From: Frederick Akalin Date: Sun, 21 Mar 2021 01:47:49 -0500 Subject: [PATCH] Refactor create code in main function --- cmd/par/main.go | 33 +++++++++++++++++++++------------ par2/create.go | 12 ++++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cmd/par/main.go b/cmd/par/main.go index cdfc207d..ab8552dd 100644 --- a/cmd/par/main.go +++ b/cmd/par/main.go @@ -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 diff --git a/par2/create.go b/par2/create.go index 7eac7e40..c7bb1010 100644 --- a/par2/create.go +++ b/par2/create.go @@ -5,6 +5,7 @@ import ( "path" "path/filepath" + "github.com/akalin/gopar/par2cmdline" "github.com/akalin/gopar/rsec16" ) @@ -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 +}