go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/cmd/ugz/main.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package main
     9  
    10  import (
    11  	"compress/gzip"
    12  	"flag"
    13  	"fmt"
    14  	"io"
    15  	"os"
    16  
    17  	"go.charczuk.com/sdk/cliutil"
    18  )
    19  
    20  var flagDecompress = flag.Bool("decompress", false, "if we should decompress the data")
    21  
    22  func main() {
    23  	flag.Parse()
    24  	if *flagDecompress {
    25  		if len(flag.Args()) > 0 {
    26  			fmt.Fprintln(os.Stderr, "decompress", os.ExpandEnv(flag.Arg(0)))
    27  			f, err := os.Open(os.ExpandEnv(flag.Arg(0)))
    28  			if err != nil {
    29  				cliutil.Fatal(err)
    30  			}
    31  			defer f.Close()
    32  			gzr, err := gzip.NewReader(f)
    33  			if err != nil {
    34  				cliutil.Fatal(err)
    35  			}
    36  			contents, err := io.ReadAll(gzr)
    37  			if err != nil {
    38  				cliutil.Fatal(err)
    39  			}
    40  			fmt.Println(string(contents))
    41  			return
    42  		}
    43  		gzr, err := gzip.NewReader(os.Stdin)
    44  		if err != nil {
    45  			cliutil.Fatal(err)
    46  		}
    47  		_, err = io.Copy(os.Stdout, gzr)
    48  		if err != nil {
    49  			cliutil.Fatal(err)
    50  		}
    51  		return
    52  	}
    53  }