github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/gzip/options.go (about) 1 // Copyright 2017-2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gzip 6 7 import ( 8 "errors" 9 "flag" 10 "fmt" 11 "os" 12 "path/filepath" 13 "runtime" 14 15 "github.com/klauspost/pgzip" 16 ) 17 18 // Options represents the CLI options possible, controlling how 19 // gzip operates on the given input data. 20 type Options struct { 21 Blocksize int 22 Level int 23 Processes int 24 Decompress bool 25 Force bool 26 Help bool 27 Keep bool 28 Quiet bool 29 Stdin bool 30 Stdout bool 31 Test bool 32 Verbose bool 33 Suffix string 34 } 35 36 // ParseArgs takes CLI args and parses them via a Flagset into fields in 37 // the Options struct. Returns any errors from parsing and validating options. 38 func (o *Options) ParseArgs(args []string, cmdLine *flag.FlagSet) error { 39 var levels [10]bool 40 41 cmdLine.IntVar(&o.Blocksize, "b", 128, "Set compression block size in KiB") 42 cmdLine.BoolVar(&o.Decompress, "d", false, "Decompress the compressed input") 43 cmdLine.BoolVar(&o.Force, "f", false, "Force overwrite of output file and compress links") 44 cmdLine.BoolVar(&o.Help, "h", false, "Display a help screen and quit") 45 cmdLine.BoolVar(&o.Keep, "k", false, "Do not delete original file after processing") 46 // TODO: implement list option here 47 cmdLine.IntVar(&o.Processes, "p", runtime.NumCPU(), "Allow up to n compression threads") 48 cmdLine.BoolVar(&o.Quiet, "q", false, "Print no messages, even on error") 49 // TODO: implement recursive option here 50 cmdLine.BoolVar(&o.Stdout, "c", false, "Write all processed output to stdout (won't delete)") 51 cmdLine.StringVar(&o.Suffix, "S", ".gz", "Specify suffix for compression") 52 cmdLine.BoolVar(&o.Test, "t", false, "Test the integrity of the compressed input") 53 cmdLine.BoolVar(&o.Verbose, "v", false, "Produce more verbose output") 54 cmdLine.BoolVar(&levels[1], "1", false, "Compression Level 1") 55 cmdLine.BoolVar(&levels[2], "2", false, "Compression Level 2") 56 cmdLine.BoolVar(&levels[3], "3", false, "Compression Level 3") 57 cmdLine.BoolVar(&levels[4], "4", false, "Compression Level 4") 58 cmdLine.BoolVar(&levels[5], "5", false, "Compression Level 5") 59 cmdLine.BoolVar(&levels[6], "6", false, "Compression Level 6") 60 cmdLine.BoolVar(&levels[7], "7", false, "Compression Level 7") 61 cmdLine.BoolVar(&levels[8], "8", false, "Compression Level 8") 62 cmdLine.BoolVar(&levels[9], "9", false, "Compression Level 9") 63 64 if err := cmdLine.Parse(args[1:]); err != nil { 65 return err 66 } 67 68 var err error 69 o.Level, err = parseLevels(levels) 70 if err != nil { 71 return err 72 } 73 74 return o.validate(len(cmdLine.Args()) > 0) 75 } 76 77 // Validate checks options. 78 // Forces decompression to be enabled when test mode is enabled. 79 // It further modifies options if the running binary is named 80 // gunzip or gzcat to allow for expected behavor. Checks if there is piped stdin data. 81 func (o *Options) validate(moreArgs bool) error { 82 if !moreArgs && !o.Force { 83 return fmt.Errorf("gzip: standard output is a terminal -- ignoring") 84 } 85 86 if o.Help { 87 // Return an empty errorString so the CLI app does not continue 88 return errors.New("") 89 } 90 91 if o.Test { 92 o.Decompress = true 93 } 94 95 // Support gunzip and gzcat symlinks 96 if filepath.Base(os.Args[0]) == "gunzip" { 97 o.Decompress = true 98 } else if filepath.Base(os.Args[0]) == "gzcat" { 99 o.Decompress = true 100 o.Stdout = true 101 } 102 103 // no args passed compress stdin to stdout 104 if !moreArgs { 105 o.Stdin = true 106 // Since there's no filename to derive the output path from, only support 107 // outputting to stdout when data is piped from stdin 108 o.Stdout = true 109 } 110 111 return nil 112 } 113 114 // parseLevels loops through a [10]bool and returns the index of the element 115 // that's true. If more than one element is true it returns an error. If no 116 // element is true, it returns the constant pgzip.DefaultCompression (-1). 117 func parseLevels(levels [10]bool) (int, error) { 118 var level int 119 120 for i, l := range levels { 121 if l && level != 0 { 122 return 0, fmt.Errorf("error: multiple compression levels specified") 123 } else if l { 124 level = i 125 } 126 } 127 128 if level == 0 { 129 return pgzip.DefaultCompression, nil 130 } 131 132 return level, nil 133 }