manually add dependency on go-getter

This commit is contained in:
Jingfang Liu
2018-08-14 14:20:19 -07:00
parent 70fb22cad6
commit b02f7775c5
270 changed files with 56453 additions and 0 deletions

514
vendor/github.com/ulikunitz/xz/cmd/gxz/file.go generated vendored Normal file
View File

@@ -0,0 +1,514 @@
// Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/ulikunitz/xz"
"github.com/ulikunitz/xz/internal/xlog"
"github.com/ulikunitz/xz/lzma"
)
// signalHandler establishes the signal handler for SIGTERM(1) and
// handles it in its own go routine. The returned quit channel must be
// closed to terminate the signal handler go routine.
func signalHandler(w *writer) chan<- struct{} {
quit := make(chan struct{})
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt, syscall.SIGPIPE)
go func() {
select {
case <-quit:
signal.Stop(sigch)
return
case <-sigch:
w.removeTmpFile()
os.Exit(7)
}
}()
return quit
}
// format defines the newCompressor and newDecompressor functions for a
// compression format.
type format struct {
newCompressor func(w io.Writer, opts *options) (c io.WriteCloser,
err error)
newDecompressor func(r io.Reader, opts *options) (d io.Reader,
err error)
validHeader func(br *bufio.Reader) bool
}
// dictCapExps maps preset values to exponent for dictionary capacity
// sizes.
var lzmaDictCapExps = []uint{18, 20, 21, 22, 22, 23, 23, 24, 25, 26}
// formats contains the formats supported by gxz.
var formats = map[string]*format{
"lzma": &format{
newCompressor: func(w io.Writer, opts *options,
) (c io.WriteCloser, err error) {
lc := lzma.WriterConfig{
Properties: &lzma.Properties{LC: 3, LP: 0,
PB: 2},
DictCap: 1 << lzmaDictCapExps[opts.preset],
}
return lc.NewWriter(w)
},
newDecompressor: func(r io.Reader, opts *options,
) (d io.Reader, err error) {
lc := lzma.ReaderConfig{
DictCap: 1 << lzmaDictCapExps[opts.preset],
}
return lc.NewReader(r)
},
validHeader: func(br *bufio.Reader) bool {
h, err := br.Peek(lzma.HeaderLen)
if err != nil {
return false
}
return lzma.ValidHeader(h)
},
},
"xz": &format{
newCompressor: func(w io.Writer, opts *options,
) (c io.WriteCloser, err error) {
cfg := xz.WriterConfig{
DictCap: 1 << lzmaDictCapExps[opts.preset],
}
return cfg.NewWriter(w)
},
newDecompressor: func(r io.Reader, opts *options,
) (d io.Reader, err error) {
cfg := xz.ReaderConfig{
DictCap: 1 << lzmaDictCapExps[opts.preset],
}
return cfg.NewReader(r)
},
validHeader: func(br *bufio.Reader) bool {
h, err := br.Peek(xz.HeaderLen)
if err != nil {
return false
}
return xz.ValidHeader(h)
},
},
}
var errBase = errors.New("name has no base part")
// targetName finds the correct target name taking the options into
// account.
func targetName(path string, opts *options) (target string, err error) {
if path == "-" {
panic("path name - not supported")
}
if len(path) == 0 {
return "", errors.New("empty file name not supported")
}
ext := "." + opts.format
tarExt := ".txz"
if opts.format == "lzma" {
tarExt = ".tlz"
}
if !opts.decompress {
if strings.HasSuffix(path, ext) {
return "", fmt.Errorf(
"%s: file has already %s suffix", path, ext)
}
if strings.HasSuffix(path, tarExt) {
return "", fmt.Errorf(
"%s: file has already %s suffix", path, tarExt)
}
return path + ext, nil
}
if strings.HasSuffix(path, ext) {
target = path[:len(path)-len(ext)]
if filepath.Base(target) == "" {
return "", &userPathError{path, errBase}
}
return target, nil
}
if strings.HasSuffix(path, tarExt) {
target = path[:len(path)-len(tarExt)]
if filepath.Base(target) == "" {
return "", &userPathError{path, errBase}
}
return target + ".tar", nil
}
return path, nil
}
// tmpName converts the path string into a temporary name by appending
// .decompress or .compress to the file path.
func tmpName(path string, decompress bool) string {
var ext string
if decompress {
ext = ".decompress"
} else {
ext = ".compress"
}
return path + ext
}
// writer is used as file writer for decompression and file compressor
// for compression.
type writer struct {
f *os.File
name string
bw *bufio.Writer
io.Writer
cmp io.WriteCloser
success bool
}
// writerFormat select the writer format.
func writerFormat(opts *options) (f *format, err error) {
var ok bool
if f, ok = formats[opts.format]; !ok {
return nil, fmt.Errorf("compression format %q not supported",
opts.format)
}
return f, nil
}
// newCompressor creates a compressor for the given writer.
func newCompressor(w io.Writer, opts *options) (cmp io.WriteCloser, err error) {
if opts.decompress {
panic("no compressor needed")
}
f, err := writerFormat(opts)
if err != nil {
return nil, err
}
if cmp, err = f.newCompressor(w, opts); err != nil {
return nil, err
}
return cmp, nil
}
// newWriter creates a new file writer. Note that options must contain
// the actual compression format supported and not just auto.
func newWriter(path string, perm os.FileMode, opts *options,
) (w *writer, err error) {
w = &writer{name: path}
if opts.stdout {
w.f = os.Stdout
w.name = "-"
} else {
name, err := targetName(path, opts)
if err != nil {
return nil, err
}
if _, err = os.Stat(name); !os.IsNotExist(err) {
if !opts.force {
return nil, &userPathError{
Path: name,
Err: errors.New("file exists")}
}
}
tmp := tmpName(name, opts.decompress)
if w.f, err = os.OpenFile(tmp,
os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm); err != nil {
return nil, err
}
w.name = name
}
w.bw = bufio.NewWriter(w.f)
if opts.decompress {
w.Writer = w.bw
return w, nil
}
w.cmp, err = newCompressor(w.bw, opts)
if err != nil {
return nil, &userPathError{w.name, err}
}
w.Writer = w.cmp
return w, nil
}
// isStdout checks whether the parameter refers to stdout.
func isStdout(f *os.File) bool {
return f.Fd() == uintptr(syscall.Stdout)
}
var errInval = errors.New("invalid value")
// Close closes the writer. Note that the behavior depends whether
// success has been set for the writer.
func (w *writer) Close() error {
var err error
if w.f == nil {
return errInval
}
defer func() { w.f = nil }()
if !w.success {
if isStdout(w.f) {
return nil
}
if err = w.f.Close(); err != nil {
return err
}
if err = os.Remove(w.f.Name()); err != nil {
return err
}
return nil
}
if w.cmp != nil {
if err = w.cmp.Close(); err != nil {
return err
}
}
if err = w.bw.Flush(); err != nil {
return err
}
if isStdout(w.f) {
return nil
}
if err = w.f.Close(); err != nil {
return err
}
if err = os.Rename(w.f.Name(), w.name); err != nil {
return err
}
return nil
}
// removeTmpFile removes the temporary file for the writer. It is used
// by the signal handler goroutine.
func (w *writer) removeTmpFile() {
os.Remove(w.f.Name())
}
// SetSuccess sets the success variable to true.
func (w *writer) SetSuccess() { w.success = true }
// reader is used as a file reader.
type reader struct {
f *os.File
io.Reader
success bool
keep bool
}
// errNoRegular indicates that a file is not regular.
var errNoRegular = errors.New("no regular file")
// specialBits contain the special bits, which are not supported by gxz.
const specialBits = os.ModeSetuid | os.ModeSetgid | os.ModeSticky
// openFile opens the given path with the given options.
func openFile(path string, opts *options) (f *os.File, err error) {
if path == "-" {
return os.Stdin, nil
}
fi, err := os.Lstat(path)
if err != nil {
return nil, err
}
fm := fi.Mode()
if !fm.IsRegular() {
if !opts.force || fm&os.ModeSymlink == 0 {
return nil, &userPathError{Path: path,
Err: errNoRegular}
}
}
if f, err = os.Open(path); err != nil {
return nil, err
}
if fi, err = f.Stat(); err != nil {
return nil, err
}
fm = fi.Mode()
if !fm.IsRegular() {
return nil, &userPathError{Path: path, Err: errNoRegular}
}
if fm&specialBits != 0 && !opts.force {
return nil, &userPathError{Path: path,
Err: errors.New("setuid, setgid and/or sticky bit set")}
}
return f, nil
}
var errInvalidFormat = errors.New("file format not recognized")
// readerFormat tries to determine the type of a file. Currently it
// checks for the XZ header magic and if it is not present assumes that
// the file has been encoded by LZMA. The format field in options is
// updated.
func readerFormat(br *bufio.Reader, opts *options) (f *format, err error) {
var ok bool
if f, ok = formats[opts.format]; ok {
if !f.validHeader(br) {
return nil, errInvalidFormat
}
return f, nil
}
if opts.format != "auto" {
return nil, fmt.Errorf("compression format %s not supported",
opts.format)
}
for format, f := range formats {
if f.validHeader(br) {
opts.format = format
return f, nil
}
}
return nil, errInvalidFormat
}
// newDecompressor creates a new decompressor.
func newDecompressor(br *bufio.Reader, opts *options) (dec io.Reader,
err error) {
if !opts.decompress {
panic("no decompressor needed")
}
f, err := readerFormat(br, opts)
if err != nil {
return nil, err
}
if dec, err = f.newDecompressor(br, opts); err != nil {
return nil, err
}
return dec, nil
}
// newReader creates a new reader for files.
func newReader(path string, opts *options) (r *reader, err error) {
f, err := openFile(path, opts)
if err != nil {
return nil, err
}
br := bufio.NewReader(f)
if !opts.decompress {
r = &reader{f: f, Reader: br, keep: opts.keep || opts.stdout}
return r, nil
}
dec, err := newDecompressor(br, opts)
if err != nil {
return nil, &userPathError{path, err}
}
r = &reader{f: f, Reader: dec, keep: opts.keep || opts.stdout}
return r, nil
}
// isStdin checks whether the given file reference is stdin.
func isStdin(f *os.File) bool {
return f.Fd() == uintptr(syscall.Stdin)
}
// Close closes the reader. The behavior can be influences by the
// success attribute of reader.
func (r *reader) Close() error {
if r.f == nil {
return errInval
}
defer func() { r.f = nil }()
if isStdin(r.f) {
return nil
}
if err := r.f.Close(); err != nil {
return err
}
if r.keep || !r.success {
return nil
}
if err := os.Remove(r.f.Name()); err != nil {
return err
}
return nil
}
func (r *reader) SetSuccess() { r.success = true }
func (r *reader) Perm() os.FileMode {
const defaultPerm os.FileMode = 0666
fi, err := r.f.Stat()
if err != nil {
return defaultPerm
}
return fi.Mode() & defaultPerm
}
// userPathError represents a path error presentable to a user. In
// difference to os.PathError it removes the information of the
// operation returning the error.
type userPathError struct {
Path string
Err error
}
// Error provides the error string for the path error.
func (e *userPathError) Error() string {
return e.Path + ": " + e.Err.Error()
}
// userError converts path error to an error message that is
// acceptable for gxz users. PathError provides information about the
// command that has created an error. For instance Lstat informs that
// lstat detected that a file didn't exist this information is not
// relevant for users of the gxz program. This function converts a
// path error into a generic error removing the operation information.
func userError(err error) error {
pe, ok := err.(*os.PathError)
if !ok {
return err
}
return &userPathError{Path: pe.Path, Err: pe.Err}
}
func printErr(err error) {
if err != nil {
xlog.Warn(userError(err))
}
}
// processFile process the file with the given path applying the
// provided options.
func processFile(path string, opts *options) (err error) {
r, err := newReader(path, opts)
if err != nil {
printErr(err)
return
}
defer r.Close()
w, err := newWriter(path, r.Perm(), opts)
if err != nil {
printErr(err)
return
}
defer w.Close()
quitSignalHandler := signalHandler(w)
if _, err = io.Copy(w, r); err != nil {
close(quitSignalHandler)
printErr(err)
return err
}
close(quitSignalHandler)
w.SetSuccess()
if err = w.Close(); err != nil {
printErr(err)
return err
}
r.SetSuccess()
if err = r.Close(); err != nil {
printErr(err)
return err
}
return nil
}

57
vendor/github.com/ulikunitz/xz/cmd/gxz/licenses.go generated vendored Normal file
View File

@@ -0,0 +1,57 @@
package main
const goLicense = `Copyright (c) 2009 The Go Authors. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
`
const xzLicense = `Copyright (c) 2014-2017 Ulrich Kunitz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* My name, Ulrich Kunitz, may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
`

240
vendor/github.com/ulikunitz/xz/cmd/gxz/main.go generated vendored Normal file
View File

@@ -0,0 +1,240 @@
// Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command gxz supports the compression and decompression of LZMA files.
//
// Use gxz -h to get information about supported flags.
package main
//go:generate xb cat -o licenses.go xzLicense:github.com/ulikunitz/xz/LICENSE goLicense:~/go/LICENSE
//go:generate xb version-file -o version.go
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime/pprof"
"strings"
"text/template"
"github.com/ulikunitz/xz/internal/gflag"
"github.com/ulikunitz/xz/internal/term"
"github.com/ulikunitz/xz/internal/xlog"
)
const (
usageStr = `Usage: gxz [OPTION]... [FILE]...
Compress or uncompress FILEs in the .lzma format (by default, compress FILES
in place).
-c, --stdout write to standard output and don't delete input files
-d, --decompress force decompression
-f, --force force overwrite of output file and compress links
-F, --format <format>
Specify the file format to compress or decompress.
auto Default format for compression is xz. For decompression
the file content is used to identify the format.
xz The xz file format.
lzma, alone Compress to the .lzma file format.
-h, --help give this help
-k, --keep keep (don't delete) input files
-L, --license display software license
-q, --quiet suppress all warnings
-v, --verbose verbose mode
-V, --version display version string
-z, --compress force compression
-0 ... -9 compression preset; default is 6
--cpuprofile <file>
create a cpuprofile that can be used with go tool pprof
With no file, or when FILE is -, read standard input.
Report bugs using <https://github.com/ulikunitz/xz/issues>.
`
)
func usage(w io.Writer) {
fmt.Fprint(w, usageStr)
}
func licenses(w io.Writer) {
out := `
github.com/ulikunitz/xz -- xz for Go
====================================
{{.xz}}
Go Programming Language
=======================
The gxz program contains the packages gflag and xlog that are
extensions of packages from the Go standard library. The packages may
contain code from those packages.
{{.go}}
`
out = strings.TrimLeft(out, " \n")
tmpl, err := template.New("licenses").Parse(out)
if err != nil {
xlog.Panicf("error %s parsing licenses template", err)
}
lmap := map[string]string{
"xz": strings.TrimSpace(xzLicense),
"go": strings.TrimSpace(goLicense),
}
if err = tmpl.Execute(w, lmap); err != nil {
xlog.Fatalf("error %s writing licenses template", err)
}
}
type options struct {
help bool
stdout bool
decompress bool
force bool
format string
keep bool
license bool
version bool
quiet int
verbose int
preset int
cpuprofile string
}
func (o *options) Init() {
if o.preset != 0 {
xlog.Panicf("options are already initialized")
}
gflag.BoolVarP(&o.help, "help", "h", false, "")
gflag.BoolVarP(&o.stdout, "stdout", "c", false, "")
gflag.BoolVarP(&o.decompress, "decompress", "d", false, "")
gflag.BoolVarP(&o.force, "force", "f", false, "")
gflag.StringVarP(&o.format, "format", "F", "auto", "")
gflag.BoolVarP(&o.keep, "keep", "k", false, "")
gflag.BoolVarP(&o.license, "license", "L", false, "")
gflag.BoolVarP(&o.version, "version", "V", false, "")
gflag.CounterVarP(&o.quiet, "quiet", "q", 0, "")
gflag.CounterVarP(&o.verbose, "verbose", "v", 0, "")
gflag.PresetVar(&o.preset, 0, 9, 6, "")
gflag.StringVarP(&o.cpuprofile, "cpuprofile", "", "", "")
}
// normalizeFormat normalizes the format field of options. If the
// function completes without error the format field will be "xz",
// "lzma" or "auto". The latter only if the option decompress is true.
func normalizeFormat(o *options) error {
switch o.format {
case "xz", "lzma":
case "auto":
if !o.decompress {
o.format = "xz"
}
case "alone":
o.format = "lzma"
default:
return fmt.Errorf("format %q unsupported", o.format)
}
return nil
}
func main() {
// setup logger
cmdName := filepath.Base(os.Args[0])
xlog.SetPrefix(fmt.Sprintf("%s: ", cmdName))
xlog.SetFlags(0)
// initialize flags
gflag.CommandLine = gflag.NewFlagSet(cmdName, gflag.ExitOnError)
gflag.Usage = func() { usage(os.Stderr); os.Exit(1) }
opts := options{}
opts.Init()
switch cmdName {
case "lzma", "glzma":
opts.format = "lzma"
case "lzcat", "glzcat":
opts.format = "lzma"
fallthrough
case "xzcat", "gxzcat":
opts.stdout = true
opts.decompress = true
case "unlzma", "unglzma":
opts.format = "lzma"
fallthrough
case "unxz", "ungxz":
opts.decompress = true
}
gflag.Parse()
if opts.help {
usage(os.Stdout)
os.Exit(0)
}
if opts.license {
licenses(os.Stdout)
os.Exit(0)
}
if opts.version {
xlog.Printf("version %s\n", version)
os.Exit(0)
}
flags := xlog.Flags()
switch {
case opts.verbose <= 0:
flags |= xlog.Lnoprint | xlog.Lnodebug
case opts.verbose == 1:
flags |= xlog.Lnodebug
}
switch {
case opts.quiet >= 2:
flags |= xlog.Lnoprint | xlog.Lnowarn | xlog.Lnodebug
flags |= xlog.Lnopanic | xlog.Lnofatal
case opts.quiet == 1:
flags |= xlog.Lnoprint | xlog.Lnowarn | xlog.Lnodebug
}
xlog.SetFlags(flags)
if opts.cpuprofile != "" {
f, err := os.Create(opts.cpuprofile)
if err != nil {
xlog.Fatal(err)
}
if err = pprof.StartCPUProfile(f); err != nil {
xlog.Fatal(err)
}
}
if err := normalizeFormat(&opts); err != nil {
pprof.StopCPUProfile()
xlog.Fatal(err)
}
var args []string
if gflag.NArg() == 0 {
opts.stdout = true
args = []string{"-"}
} else {
args = gflag.Args()
}
if opts.stdout && !opts.decompress && !opts.force &&
term.IsTerminal(os.Stdout.Fd()) {
pprof.StopCPUProfile()
xlog.Fatal(`Compressed data will not be written to a terminal
Use -f to force compression. For help type gxz -h.`)
}
exit := 0
for _, arg := range args {
if err := processFile(arg, &opts); err != nil {
exit = 1
}
}
pprof.StopCPUProfile()
os.Exit(exit)
}

3
vendor/github.com/ulikunitz/xz/cmd/gxz/version.go generated vendored Normal file
View File

@@ -0,0 +1,3 @@
package main
const version = "0.5.4"

191
vendor/github.com/ulikunitz/xz/cmd/xb/cat.go generated vendored Normal file
View File

@@ -0,0 +1,191 @@
// Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"text/template"
)
const catUsageString = `xb cat [options] <id>:<path>...
This xb command puts the contents of the files given as relative paths to
the GOPATH variable as string constants into a go file.
-h prints this message and exits
-p package name (default main)
-o file name of output
`
func catUsage(w io.Writer) {
fmt.Fprint(w, catUsageString)
}
type gopath struct {
p []string
i int
}
func newGopath() *gopath {
p := strings.Split(os.Getenv("GOPATH"), ":")
return &gopath{p: p}
}
type cpair struct {
id string
path string
}
func (p cpair) Read() (s string, err error) {
var r io.ReadCloser
if p.path == "-" {
r = os.Stdin
} else {
if r, err = os.Open(p.path); err != nil {
return
}
}
defer func() {
err = r.Close()
}()
b, err := ioutil.ReadAll(r)
if err != nil {
return
}
s = string(b)
return
}
func verifyPath(path string) error {
fi, err := os.Stat(path)
if err != nil {
return err
}
if !fi.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", path)
}
return nil
}
func (gp *gopath) find(arg string) (p cpair, err error) {
t := strings.SplitN(arg, ":", 2)
switch len(t) {
case 0:
err = fmt.Errorf("empty argument not supported")
return
case 1:
gp.i++
p = cpair{fmt.Sprintf("gocat%d", gp.i), t[0]}
case 2:
p = cpair{t[0], t[1]}
}
if p.path == "-" {
return
}
// substitute first ~ by $HOME
p.path = strings.Replace(p.path, "~", os.Getenv("HOME"), 1)
paths := make([]string, 0, len(gp.p)+1)
if filepath.IsAbs(p.path) {
paths = append(paths, filepath.Clean(p.path))
} else {
for _, q := range gp.p {
u := filepath.Join(q, "src", p.path)
paths = append(paths, filepath.Clean(u))
}
u := filepath.Join(".", p.path)
paths = append(paths, filepath.Clean(u))
}
for _, u := range paths {
if err = verifyPath(u); err != nil {
if os.IsNotExist(err) {
continue
}
return
}
p.path = u
return
}
err = fmt.Errorf("file %s not found", p.path)
return
}
// Gofile is used with the template gofileTmpl.
type Gofile struct {
Pkg string
Cmap map[string]string
}
var gofileTmpl = `package {{.Pkg}}
{{range $k, $v := .Cmap}}const {{$k}} = ` + "`{{$v}}`\n{{end}}"
func cat() {
var err error
cmdName := filepath.Base(os.Args[0])
log.SetPrefix(fmt.Sprintf("%s: ", cmdName))
log.SetFlags(0)
flag.CommandLine = flag.NewFlagSet(cmdName, flag.ExitOnError)
flag.Usage = func() { catUsage(os.Stderr); os.Exit(1) }
help := flag.Bool("h", false, "")
pkg := flag.String("p", "main", "")
out := flag.String("o", "", "")
flag.Parse()
if *help {
catUsage(os.Stdout)
os.Exit(0)
}
if *pkg == "" {
log.Fatal("option -p must not be empty")
}
w := os.Stdout
if *out != "" {
if w, err = os.Create(*out); err != nil {
log.Fatal(err)
}
}
gp := newGopath()
gofile := Gofile{
Pkg: *pkg,
Cmap: make(map[string]string, len(flag.Args())),
}
for _, arg := range flag.Args() {
p, err := gp.find(arg)
if err != nil {
log.Print(err)
continue
}
s, err := p.Read()
if err != nil {
log.Print(err)
continue
}
gofile.Cmap[p.id] = s
}
tmpl, err := template.New("gofile").Parse(gofileTmpl)
if err != nil {
log.Panicf("goFileTmpl error %s", err)
}
if err = tmpl.Execute(w, gofile); err != nil {
log.Fatal(err)
}
os.Exit(0)
}

158
vendor/github.com/ulikunitz/xz/cmd/xb/copyright.go generated vendored Normal file
View File

@@ -0,0 +1,158 @@
// Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
)
const crUsageString = `xb copyright [options] <path>....
The xb copyright command adds a copyright remark to all go files below path.
-h prints this message and exits
`
func crUsage(w io.Writer) {
fmt.Fprint(w, crUsageString)
}
const copyrightText = `
Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
`
func goComment(text string) string {
buf := new(bytes.Buffer)
scanner := bufio.NewScanner(strings.NewReader(text))
var err error
for scanner.Scan() {
s := strings.TrimSpace(scanner.Text())
if len(s) == 0 {
continue
}
if _, err = fmt.Fprintln(buf, "//", s); err != nil {
panic(err)
}
}
if err = scanner.Err(); err != nil {
panic(err)
}
if _, err = fmt.Fprintln(buf); err != nil {
panic(err)
}
return buf.String()
}
var goCopyright = goComment(copyrightText)
func addCopyright(path string) (err error) {
log.Printf("adding copyright to %s", path)
src, err := os.Open(path)
if err != nil {
return err
}
defer func() {
cerr := src.Close()
if cerr != nil && err == nil {
err = cerr
}
}()
newPath := path + ".new"
dst, err := os.Create(newPath)
if err != nil {
return err
}
defer func() {
cerr := dst.Close()
if cerr != nil && err == nil {
err = cerr
}
}()
out := bufio.NewWriter(dst)
fmt.Fprint(out, goCopyright)
scanner := bufio.NewScanner(src)
line := 0
del := false
for scanner.Scan() {
line++
txt := scanner.Text()
if line == 1 && strings.Contains(txt, "Copyright") {
del = true
continue
}
if del {
s := strings.TrimSpace(txt)
if len(s) == 0 {
del = false
}
continue
}
fmt.Fprintln(out, txt)
}
if err = scanner.Err(); err != nil {
return err
}
if err = out.Flush(); err != nil {
return
}
err = os.Rename(newPath, path)
return
}
func walkCopyrights(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(info.Name(), ".go") {
return nil
}
return addCopyright(path)
}
func copyright() {
cmdName := os.Args[0]
log.SetPrefix(fmt.Sprintf("%s: ", cmdName))
log.SetFlags(0)
flag.CommandLine = flag.NewFlagSet(cmdName, flag.ExitOnError)
flag.Usage = func() { crUsage(os.Stderr); os.Exit(1) }
help := flag.Bool("h", false, "")
flag.Parse()
if *help {
crUsage(os.Stdout)
os.Exit(0)
}
for _, path := range flag.Args() {
fi, err := os.Stat(path)
if err != nil {
log.Print(err)
continue
}
if !fi.IsDir() {
log.Printf("%s is not a directory", path)
continue
}
if err = filepath.Walk(path, walkCopyrights); err != nil {
log.Fatalf("%s error %s", path, err)
}
}
}

67
vendor/github.com/ulikunitz/xz/cmd/xb/main.go generated vendored Normal file
View File

@@ -0,0 +1,67 @@
// Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command xb supports the xz for Go project builds.
//
// Use xb help to get information about the supported commands.
package main
//go:generate xb version-file -o version.go
import (
"fmt"
"log"
"os"
)
const usage = `xb <command>
xb is a supporting building tool from the xz project for Go.
xb help -- prints this message
xb version-file -- generates go file with version information
xb cat -- generates go file that includes the given text files
xb copyright -- adds copyright statements to relevant files
xb version -- prints version information for xb
Report bugs using <https://github.com/ulikunitz/xz/issues>.
`
func updateArgs(cmd string) {
args := make([]string, 1, len(os.Args)-1)
args[0] = "xb " + cmd
os.Args = append(args, os.Args[2:]...)
}
func main() {
log.SetPrefix("xb: ")
log.SetFlags(0)
if len(os.Args) < 2 {
log.Fatal("to show help, use xb help")
}
switch os.Args[1] {
case "help", "-h":
fmt.Print(usage)
os.Exit(0)
case "version":
fmt.Printf("xb %s\n", version)
os.Exit(0)
case "cat":
updateArgs("cat")
cat()
os.Exit(0)
case "version-file":
updateArgs("version-file")
versionFile()
os.Exit(0)
case "copyright":
updateArgs("copyright")
copyright()
os.Exit(0)
default:
log.Fatalf("command %q not supported", os.Args[1])
}
}

85
vendor/github.com/ulikunitz/xz/cmd/xb/version-file.go generated vendored Normal file
View File

@@ -0,0 +1,85 @@
// Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
)
const vfUsage = `xb version-file [options] <id>:<path>...
The command creates go file with a version constant. The version string
contains the contents of the VERSION environment variable or the output
of git describe.
-h prints this message and exits
-p package name (default main)
-o file name of output
`
func versionFileUsage(w io.Writer) {
fmt.Fprint(w, vfUsage)
}
func versionFile() {
cmdName := filepath.Base(os.Args[0])
log.SetPrefix(fmt.Sprintf("%s: ", cmdName))
log.SetFlags(0)
flag.CommandLine = flag.NewFlagSet(cmdName, flag.ExitOnError)
flag.Usage = func() { versionFileUsage(os.Stderr); os.Exit(1) }
help := flag.Bool("h", false, "")
pkg := flag.String("p", "main", "")
out := flag.String("o", "", "")
flag.Parse()
if *help {
versionFileUsage(os.Stdout)
os.Exit(0)
}
if *pkg == "" {
log.Fatal("option -p must not be empty")
}
var err error
w := os.Stdout
if *out != "" {
if w, err = os.Create(*out); err != nil {
log.Fatal(err)
}
}
// get the version string
version := os.Getenv("VERSION")
if version == "" {
b, err := exec.Command("git", "describe").Output()
if err != nil {
log.Fatalf("error %s while executing git describe", err)
}
version = string(b)
}
version = strings.TrimSpace(version)
versionTmpl := `package main
const version = "{{.}}"
`
tmpl := template.Must(template.New("version").Parse(versionTmpl))
if err = tmpl.Execute(w, version); err != nil {
log.Fatal(err)
}
}

3
vendor/github.com/ulikunitz/xz/cmd/xb/version.go generated vendored Normal file
View File

@@ -0,0 +1,3 @@
package main
const version = "0.5.4"