From 7bbf90e01b22bc836083910c410a6207549e774d Mon Sep 17 00:00:00 2001 From: Phillip Date: Sun, 23 Aug 2020 19:29:03 -0700 Subject: [PATCH] Added usage of "raisin" and "grape" as default commands Migrated commands to flags --- .travis.yml | 4 +-- Dockerfile | 2 +- main.go | 99 ++++++++++++++++++++++++++++++++++------------------- 3 files changed, 67 insertions(+), 38 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5b6d7c6..f5de8c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ go: - 1.15 before_install: - - docker build . -t compressor + - docker build . -t raisin - go get github.com/go-playground/overalls - go get github.com/mattn/goveralls @@ -16,7 +16,7 @@ script: - overalls -project=github.com/go-compression/raisin -covermode=atomic -- -coverpkg=./... - $GOPATH/bin/goveralls -coverprofile=overalls.coverprofile -service=travis-ci - find . -name '*.coverprofile' -delete - - docker run --cidfile="machine.id" -it compressor ./main benchmark -generate -algorithm=lzss,dmc,huffman,flate,gzip,lzw,zlib,arithmetic,[lzss,huffman],[lzss,arithmetic],[arithmetic,huffman] alice29.txt,asyoulik.txt,cp.html,fields.c,grammar.lsp,kennedy.xls,lcet10.txt,plrabn12.txt,ptt5,sum,xargs.1 + - docker run --cidfile="machine.id" -it raisin ./raisin -benchmark -generate -algorithm=lzss,dmc,huffman,flate,gzip,lzw,zlib,arithmetic,[lzss,huffman],[lzss,arithmetic],[arithmetic,huffman] alice29.txt,asyoulik.txt,cp.html,fields.c,grammar.lsp,kennedy.xls,lcet10.txt,plrabn12.txt,ptt5,sum,xargs.1 - docker cp $(cat machine.id):/go/src/github.com/go-compression/raisin/index.html ./index.html - rm machine.id diff --git a/Dockerfile b/Dockerfile index f9c9fca..8059ff5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ ADD . /go/src/github.com/go-compression/raisin WORKDIR /go/src/github.com/go-compression/raisin RUN go get RUN go install -RUN go build -o main . +RUN go build # RUN wget "https://data.wprdc.org/dataset/9e0ce87d-07b8-420c-a8aa-9de6104f61d6/resource/96474373-bcdb-42cf-af5d-3683e326e227/download/sales-validation-codes-dictionary.pdf" -O sales.pdf # RUN wget "https://data.cityofnewyork.us/api/views/zt9s-n5aj/rows.json?accessType=DOWNLOAD" -O rows.json # RUN wget "https://data.cityofchicago.org/api/geospatial/bbvz-uum9?method=export&format=Shapefile" -O boundaries.zip diff --git a/main.go b/main.go index 454d090..ffc464b 100644 --- a/main.go +++ b/main.go @@ -24,24 +24,40 @@ func main() { } func mainBehavior() []engine.Result { - compressCmd := flag.NewFlagSet("compress", flag.ExitOnError) + application := os.Args[0] - decompressCmd := flag.NewFlagSet("decompress", flag.ExitOnError) + compressCmd := flag.Bool("compress", false, "Compress file") + decompressCmd := flag.CommandLine.Bool("decompress", false, "Decompress file") + benchmarkCmd := flag.CommandLine.Bool("benchmark", false, "Benchmark file") + helpCmd := flag.Bool("help", false, "Help") - benchmarkCmd := flag.NewFlagSet("benchmark", flag.ExitOnError) + commandArgs := make([]string, len(os.Args)) + copy(commandArgs, os.Args) + commandArgs = append(commandArgs[1:2], "") + flag.CommandLine.Parse(commandArgs) - generateHTML := benchmarkCmd.Bool("generate", false, "Compile benchmark results as an html file") + var generateHTML *bool + if *benchmarkCmd { + generateHTML = flag.Bool("generate", false, "Compile benchmark results as an html file") + } + + commandsSelected := boolsTrue([]bool{*compressCmd, *decompressCmd, *benchmarkCmd, *helpCmd}) - flag.Parse() - command := flag.Arg(0) - if command == "" { + if commandsSelected > 1 { errorWithMsg(fmt.Sprintf( - "Please provide a valid command, possible commands include: \n\t %s\n", strings.Join(Commands[:], ", "))) + "Please specify a single command. \n")) + } else if commandsSelected < 1 { + True := true + if strings.HasSuffix(application, "grape") { + decompressCmd = &True + } else { + compressCmd = &True + } + // errorWithMsg(fmt.Sprintf( + // "Please specify at least one command. \n")) } - // Non compression commands - switch command { - case "help": + if *helpCmd { fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Valid commands include: \n\t %s\n", strings.Join(Commands[:], ", ")) fmt.Fprintf(os.Stderr, "Flags:\n") @@ -50,13 +66,19 @@ func mainBehavior() []engine.Result { } // Get flag argument that is not a flag "-algorithm..." - file := flag.Arg(1) + file := os.Args[1] for i := 2; len(file) > 0 && file[0] == '-'; i++ { - file = flag.Arg(i) + file = os.Args[i] } if file == "" && !strings.Contains(file, ",") { - errorWithMsg("Please provide a file to be compressed/decompressed\n") + if *compressCmd { + errorWithMsg("Please provide a file to be compressed\n") + } else if *benchmarkCmd { + errorWithMsg("Please provide a file to be benchmarked\n") + } else { + errorWithMsg("Please provide a file to be decompressed\n") + } } else if strings.Contains(file, ",") { for _, filename := range strings.Split(file, ",") { if _, err := os.Stat(filename); os.IsNotExist(err) { @@ -67,9 +89,8 @@ func mainBehavior() []engine.Result { errorWithMsg(fmt.Sprintf("Could not open file (likely does not exist): %s\n", file)) } - switch command { - case "compress", "c": - algorithm := compressCmd.String("algorithm", "lzss,arithmetic", + if *compressCmd { + algorithm := flag.String("algorithm", "lzss,arithmetic", fmt.Sprintf("Which algorithm(s) to use, choices include: \n\t%s", strings.Join(engine.Engines[:], ", "))) files := strings.Split(file, ",") @@ -79,15 +100,14 @@ func mainBehavior() []engine.Result { var output, outputExtension *string if len(files) == 1 { - output = compressCmd.String("out", files[0]+".compressed", fmt.Sprintf("File name to output to")) + output = flag.String("out", files[0]+".compressed", fmt.Sprintf("File name to output to")) } else { - outputExtension = compressCmd.String("outext", "compressed", fmt.Sprintf("File extension used for the result")) + outputExtension = flag.String("outext", "compressed", fmt.Sprintf("File extension used for the result")) } - deleteAfter := compressCmd.Bool("delete", false, fmt.Sprintf("Delete file after compression")) + deleteAfter := flag.Bool("delete", false, fmt.Sprintf("Delete file after compression")) - posAfterCommand := getPosAfterCommand("compress", os.Args) - compressCmd.Parse(os.Args[posAfterCommand:]) + flag.Parse() algorithms := strings.Split(*algorithm, ",") for i := range files { @@ -103,8 +123,8 @@ func mainBehavior() []engine.Result { if *deleteAfter { deleteFiles(files) } - case "decompress", "d": - algorithm := decompressCmd.String("algorithm", "lzss,arithmetic", + } else if *decompressCmd { + algorithm := flag.String("algorithm", "lzss,arithmetic", fmt.Sprintf("Which algorithm(s) to use, choices include: \n\t%s", strings.Join(engine.Engines[:], ", "))) files := strings.Split(file, ",") @@ -116,15 +136,14 @@ func mainBehavior() []engine.Result { if len(files) == 1 { ext := filepath.Ext(files[0]) path := strings.TrimSuffix(files[0], ext) - output = decompressCmd.String("out", path, fmt.Sprintf("File name to output to")) + output = flag.String("out", path, fmt.Sprintf("File name to output to")) } else { - outputExtension = decompressCmd.String("outext", "", fmt.Sprintf("File extension used for the result")) + outputExtension = flag.String("outext", "", fmt.Sprintf("File extension used for the result")) } - deleteAfter := decompressCmd.Bool("delete", true, fmt.Sprintf("Delete file after compression")) + deleteAfter := flag.Bool("delete", true, fmt.Sprintf("Delete file after compression")) - posAfterCommand := getPosAfterCommand("decompress", os.Args) - decompressCmd.Parse(os.Args[posAfterCommand:]) + flag.Parse() algorithms := strings.Split(*algorithm, ",") for i := range files { @@ -140,12 +159,11 @@ func mainBehavior() []engine.Result { if *deleteAfter { deleteFiles(files) } - case "benchmark": - algorithm := benchmarkCmd.String("algorithm", "lzss,arithmetic,huffman,[lzss,arithmetic],gzip", + } else if *benchmarkCmd { + algorithm := flag.String("algorithm", "lzss,arithmetic,huffman,[lzss,arithmetic],gzip", fmt.Sprintf("Which algorithm(s) to use, choices include: \n\t%s", strings.Join(engine.Engines[:], ", "))) - posAfterCommand := getPosAfterCommand("benchmark", os.Args) - benchmarkCmd.Parse(os.Args[posAfterCommand:]) + flag.Parse() if file == "help" { fmt.Fprintf(os.Stderr, "Flags:\n") @@ -167,11 +185,11 @@ func mainBehavior() []engine.Result { fmt.Println("Wrote table to index.html") } return results - default: + } else { errorWithMsg(fmt.Sprintf( "'%s' is not a valid command, "+ "please provide a valid command, "+ - "possible commands include: \n\t %s\n", command, strings.Join(Commands[:], ", "))) + "possible commands include: \n\t %s\n", "", strings.Join(Commands[:], ", "))) } return nil } @@ -213,6 +231,17 @@ func deleteFiles(files []string) { } } +func boolsTrue(bools []bool) int { + found := 0 + for _, boolean := range bools { + if boolean { + found++ + } + } + return found + +} + func getPosAfterCommand(command string, args []string) int { for i, s := range args { if s == command {