From 323a7b849257eb7b8f78621152d15d47fcf4434a Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Sun, 30 Nov 2014 22:18:02 -0800 Subject: [PATCH] Refactoring input parsing for erasure-demo --- cmd/erasure-demo/decode.go | 33 ++++++-------------------- cmd/erasure-demo/encode.go | 31 ++++--------------------- cmd/erasure-demo/main.go | 47 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/cmd/erasure-demo/decode.go b/cmd/erasure-demo/decode.go index db96c2833..a36190b03 100644 --- a/cmd/erasure-demo/decode.go +++ b/cmd/erasure-demo/decode.go @@ -5,7 +5,6 @@ import ( "log" "os" "strconv" - "strings" "github.com/codegangsta/cli" "github.com/minio-io/minio/pkgs/erasure" @@ -18,39 +17,21 @@ func decode(c *cli.Context) { return } - // get input path - inputFilePath := c.Args().Get(0) - - // get output path - outputFilePath := inputFilePath - if c.String("output") != "" { - outputFilePath = c.String("output") - } - - protectionLevel := c.String("protection-level") - protectionLevelSplit := strings.Split(protectionLevel, ",") - if len(protectionLevelSplit) != 2 { - log.Fatal("Malformed input for protection-level") - } - - k, err := strconv.Atoi(protectionLevelSplit[0]) - if err != nil { - log.Fatal(err) - } - - m, err := strconv.Atoi(protectionLevelSplit[1]) + config, err := parseInput(c) if err != nil { log.Fatal(err) } + k := config.k + m := config.m // get chunks chunks := make([][]byte, k+m) for i := 0; i < k+m; i++ { - chunks[i], _ = ioutil.ReadFile(inputFilePath + "." + strconv.Itoa(i)) + chunks[i], _ = ioutil.ReadFile(config.input + "." + strconv.Itoa(i)) } // get length - lengthBytes, err := ioutil.ReadFile(inputFilePath + ".length") + lengthBytes, err := ioutil.ReadFile(config.input + ".length") if err != nil { log.Fatal(err) } @@ -70,8 +51,8 @@ func decode(c *cli.Context) { } // write decode data out - if _, err := os.Stat(outputFilePath); os.IsNotExist(err) { - ioutil.WriteFile(outputFilePath, decodedData, 0600) + if _, err := os.Stat(config.output); os.IsNotExist(err) { + ioutil.WriteFile(config.output, decodedData, 0600) } else { log.Fatal("Output file already exists") } diff --git a/cmd/erasure-demo/encode.go b/cmd/erasure-demo/encode.go index cd2e67685..72e13ac75 100644 --- a/cmd/erasure-demo/encode.go +++ b/cmd/erasure-demo/encode.go @@ -5,7 +5,6 @@ import ( "log" "os" "strconv" - "strings" "github.com/codegangsta/cli" "github.com/minio-io/minio/pkgs/erasure" @@ -18,33 +17,13 @@ func encode(c *cli.Context) { return } - // get input path - inputFilePath := c.Args().Get(0) - - // get output path - outputFilePath := inputFilePath - if c.String("output") != "" { - outputFilePath = c.String("output") - } - - protectionLevel := c.String("protection-level") - protectionLevelSplit := strings.Split(protectionLevel, ",") - if len(protectionLevelSplit) != 2 { - log.Fatal("Malformed input for protection-level") - } - - k, err := strconv.Atoi(protectionLevelSplit[0]) - if err != nil { - log.Fatal(err) - } - - m, err := strconv.Atoi(protectionLevelSplit[1]) + config, err := parseInput(c) if err != nil { log.Fatal(err) } // get file - inputFile, err := os.Open(inputFilePath) + inputFile, err := os.Open(config.input) if err != nil { log.Fatal(err) } @@ -56,13 +35,13 @@ func encode(c *cli.Context) { } // set up encoder - erasureParameters, _ := erasure.ParseEncoderParams(k, m, erasure.CAUCHY) + erasureParameters, _ := erasure.ParseEncoderParams(config.k, config.m, erasure.CAUCHY) // encode data encodedData, length := erasure.Encode(input, erasureParameters) // write encoded data out for key, data := range encodedData { - ioutil.WriteFile(outputFilePath+"."+strconv.Itoa(key), data, 0600) + ioutil.WriteFile(config.output+"."+strconv.Itoa(key), data, 0600) } - ioutil.WriteFile(outputFilePath+".length", []byte(strconv.Itoa(length)), 0600) + ioutil.WriteFile(config.output+".length", []byte(strconv.Itoa(length)), 0600) } diff --git a/cmd/erasure-demo/main.go b/cmd/erasure-demo/main.go index 0fd257596..244608cd2 100644 --- a/cmd/erasure-demo/main.go +++ b/cmd/erasure-demo/main.go @@ -1,7 +1,10 @@ package main import ( + "errors" "os" + "strconv" + "strings" "github.com/codegangsta/cli" ) @@ -48,3 +51,47 @@ func main() { } app.Run(os.Args) } + +// config representing cli input +type inputConfig struct { + input string + output string + k int + m int + blockSize int +} + +// parses input and returns an inputConfig with parsed input +func parseInput(c *cli.Context) (inputConfig, error) { + // get input path + inputFilePath := c.Args().Get(0) + + // get output path + outputFilePath := inputFilePath + if c.String("output") != "" { + outputFilePath = c.String("output") + } + + protectionLevel := c.String("protection-level") + protectionLevelSplit := strings.Split(protectionLevel, ",") + if len(protectionLevelSplit) != 2 { + return inputConfig{}, errors.New("Malformed input for protection-level") + } + + k, err := strconv.Atoi(protectionLevelSplit[0]) + if err != nil { + return inputConfig{}, err + } + + m, err := strconv.Atoi(protectionLevelSplit[1]) + if err != nil { + return inputConfig{}, err + } + + return inputConfig{ + input: inputFilePath, + output: outputFilePath, + k: k, + m: m, + }, nil +}