github.com/Songmu/goxz@v0.9.1/cli.go (about)

     1  package goxz
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"runtime"
     9  )
    10  
    11  type cli struct {
    12  	outStream, errStream io.Writer
    13  }
    14  
    15  func (cl *cli) run(args []string) error {
    16  	log.SetOutput(cl.errStream)
    17  	log.SetPrefix("[goxz] ")
    18  	log.SetFlags(0)
    19  
    20  	gx, err := cl.parseArgs(args)
    21  	if err != nil {
    22  		return err
    23  	}
    24  	return gx.run()
    25  }
    26  
    27  func (cl *cli) parseArgs(args []string) (*goxz, error) {
    28  	gx := &goxz{}
    29  	fs := flag.NewFlagSet("goxz", flag.ContinueOnError)
    30  	fs.SetOutput(cl.errStream)
    31  	fs.Usage = func() {
    32  		fs.SetOutput(cl.outStream)
    33  		defer fs.SetOutput(cl.errStream)
    34  		fmt.Fprintf(cl.outStream, `goxz - Just do cross building and archiving go tools conventionally
    35  
    36  Version: %s (rev: %s/%s)
    37  
    38  Synopsis:
    39      %% goxz -pv 0.0.1 -os=linux,darwin -arch=amd64 ./cmd/mytool [...]
    40  
    41  Options:
    42  `, version, revision, runtime.Version())
    43  		fs.PrintDefaults()
    44  	}
    45  
    46  	fs.StringVar(&gx.name, "n", "", "Application name. By default this is the directory name.")
    47  	fs.StringVar(&gx.dest, "d", "goxz", "Destination directory")
    48  	fs.StringVar(&gx.version, "pv", "", "Package version (optional)")
    49  	fs.StringVar(&gx.output, "o", "", "output (optional)")
    50  	fs.StringVar(&gx.os, "os", "", "Specify OS (default is 'linux darwin windows')")
    51  	fs.StringVar(&gx.arch, "arch", "", "Specify Arch (default is 'amd64,arm64')")
    52  	fs.StringVar(&gx.include, "include", "", "Include additional resources in archives")
    53  	fs.StringVar(&gx.buildLdFlags, "build-ldflags", "", "arguments to pass on each go tool link invocation")
    54  	fs.StringVar(&gx.buildTags, "build-tags", "", "a space-separated list of build `tags`")
    55  	fs.StringVar(&gx.buildInstallSuffix, "build-installsuffix", "", "a space-separated list of build `installsuffix`")
    56  	fs.BoolVar(&gx.zipAlways, "z", false, "zip always")
    57  	fs.StringVar(&gx.projDir, "C", "", "specify the project directory. cwd by default")
    58  
    59  	fs.BoolVar(&gx.static, "static", false, "build statically linked binary")
    60  	fs.BoolVar(&gx.work, "work", false, "[for debug] print the name of the temporary work directory and do not delete it when exiting.")
    61  	fs.BoolVar(&gx.trimpath, "trimpath", true, "remove all file system paths from the resulting executable. requires Go 1.13 or later.")
    62  
    63  	err := fs.Parse(args)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	gx.pkgs = fs.Args()
    68  	return gx, nil
    69  }