gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/cmds/core/gzip/gzip.go (about)

     1  // Copyright 2012-2017 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  // gzip compresses files using gzip compression.
     6  package main
     7  
     8  import (
     9  	"flag"
    10  	"fmt"
    11  	"os"
    12  	"path/filepath"
    13  
    14  	"github.com/u-root/u-root/pkg/gzip"
    15  )
    16  
    17  var cmdLine = flag.CommandLine
    18  
    19  func usage() {
    20  	fmt.Fprintf(os.Stderr, "Usage of %s:\n", filepath.Base(os.Args[0]))
    21  	cmdLine.PrintDefaults()
    22  }
    23  
    24  func main() {
    25  	var opts gzip.Options
    26  
    27  	cmdLine.Usage = usage
    28  
    29  	if err := opts.ParseArgs(os.Args, cmdLine); err != nil {
    30  		fmt.Fprintf(os.Stderr, "%s\n", err)
    31  		cmdLine.Usage()
    32  		os.Exit(2)
    33  	}
    34  
    35  	var input []string
    36  	if opts.Stdin {
    37  		input = []string{"/dev/stdin"}
    38  	} else {
    39  		input = cmdLine.Args()
    40  	}
    41  
    42  	for _, path := range input {
    43  		f := gzip.File{Path: path, Options: &opts}
    44  		if err := f.CheckPath(); err != nil {
    45  			if !opts.Quiet {
    46  				fmt.Fprintf(os.Stderr, "%s\n", err)
    47  			}
    48  			continue
    49  		}
    50  
    51  		if err := f.CheckOutputStdout(); err != nil {
    52  			if !opts.Quiet {
    53  				fmt.Fprintf(os.Stderr, "%s\n", err)
    54  			}
    55  			os.Exit(1)
    56  		}
    57  
    58  		if err := f.CheckOutputPath(); err != nil {
    59  			if !opts.Quiet {
    60  				fmt.Fprintf(os.Stderr, "%s\n", err)
    61  			}
    62  			continue
    63  		}
    64  
    65  		if err := f.Process(); err != nil {
    66  			if !opts.Quiet {
    67  				fmt.Fprintf(os.Stderr, "%s\n", err)
    68  			}
    69  		}
    70  
    71  		if err := f.Cleanup(); err != nil {
    72  			if !opts.Quiet {
    73  				fmt.Fprintf(os.Stderr, "%s\n", err)
    74  			}
    75  			continue
    76  		}
    77  	}
    78  }