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