diff --git a/Readme.md b/Readme.md index e95c073..80d4498 100644 --- a/Readme.md +++ b/Readme.md @@ -102,6 +102,38 @@ And all sprig functions ==> [Documentation](http://masterminds.github.io/sprig/) # Use inside Pipeline There is a [Dockerimage](https://hub.docker.com/r/fasibio/gomake) +# parallel execution + +You can use same stage for different commands + +``` +buildDocker: + stage: stagename + color: "{{$root.Colors.red}}" + script: + - docker build -t {{$root.Vars.dockername}}:{{$root.Vars.version}} -f {{$root.Vars.dockerfile}} . + on_failure: + - docker rmi {{$root.Vars.dockername}}:{{$root.Vars.version}} +``` +Now you can run by stagename instand of Command name with: + +``` +gomake srun stagename +``` + +**Hint** Color can help you make it better to read at + +Possible Colors: + - black + - red + - green + - yellow + - purple + - magenta + - teal + - white + + # Use Docker-Images diff --git a/command/commands.go b/command/commands.go index a24cbc9..ce12b14 100644 --- a/command/commands.go +++ b/command/commands.go @@ -13,6 +13,8 @@ type Operation struct { Script []string Image *DockerOperation On_Failure []string + Stage string + Color string } type DockerOperation struct { @@ -122,6 +124,8 @@ func (c *CommandHandler) GetExecutedCommandMakeScript(cmd string, data MakeStruc Script: commands, On_Failure: onFailer, Image: data[cmd].Image, + Color: data[cmd].Color, + Stage: data[cmd].Stage, } return res, nil } diff --git a/go.mod b/go.mod index d7e3dd0..aba8ddc 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.19 require ( github.com/Masterminds/sprig/v3 v3.2.3 + github.com/pkg/errors v0.9.1 github.com/schollz/closestmatch v2.1.0+incompatible github.com/urfave/cli/v2 v2.23.7 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index 76e856d..71bbf86 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,7 @@ github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBa github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -18,6 +19,9 @@ github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMK github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -29,6 +33,7 @@ github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/urfave/cli/v2 v2.23.7 h1:YHDQ46s3VghFHFf1DdF+Sh7H4RqhcM+t0TmZRJx4oJY= github.com/urfave/cli/v2 v2.23.7/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc= diff --git a/gomake.yml b/gomake.yml index e28326c..a1573ad 100644 --- a/gomake.yml +++ b/gomake.yml @@ -22,6 +22,8 @@ buildAll: {{include "buildDocker"}} build: + stage: b + color: "{{$root.Colors.purple}}" script: - mkdir {{.Vars.dist}} {{- range $goosKey, $goos := splitList "|" $root.Vars.gooss}} @@ -33,6 +35,8 @@ build: - rm -rf {{.Vars.dist}} buildDocker: + stage: b + color: "{{$root.Colors.red}}" script: - docker build -t {{$root.Vars.dockername}}:{{$root.Vars.version}} -f {{$root.Vars.dockerfile}} . on_failure: @@ -43,12 +47,12 @@ test: - docker run --rm {{$root.Vars.dockername}}:{{$root.Vars.version}} --help # Wrap build into a docker image buildContainer: + color: {{$root.Colors.yellow}} image: - name: golang:latest + name: alpine:latest volumes: - {{.Env.PWD}}:/build script: - cd /build - ls - {{include "build"}} on_failure: \ No newline at end of file diff --git a/interpreter/dryRun.go b/interpreter/dryRun.go index f8d9ed6..929bad8 100644 --- a/interpreter/dryRun.go +++ b/interpreter/dryRun.go @@ -12,11 +12,16 @@ type DryRunOutput struct { ExecutedCommand command.Operation } -func (i Interpreter) printDryRun(command command.MakeStruct, variables map[string]map[string]any) error { - out, err := yaml.Marshal(DryRunOutput{ - Vars: variables["vars"], - ExecutedCommand: command[i.ExecuteCommand], - }) +type SDryRunOutput map[string]any + +func (i Interpreter) printDryRun(command []StageOperationWrapper, variables map[string]map[string]any) error { + + outPutData := make(SDryRunOutput) + outPutData["vars"] = variables["vars"] + for _, c := range command { + outPutData[c.Name] = c.Command + } + out, err := yaml.Marshal(outPutData) if err != nil { return err } diff --git a/interpreter/interpreter.go b/interpreter/interpreter.go index 78fb732..75196dd 100644 --- a/interpreter/interpreter.go +++ b/interpreter/interpreter.go @@ -3,21 +3,44 @@ package interpreter import ( "bytes" "fmt" + "io" "log" "os" "os/exec" "strings" + "sync" "text/template" "github.com/Masterminds/sprig/v3" "github.com/fasibio/gomake/command" nearfinder "github.com/fasibio/gomake/nearFinder" + "github.com/pkg/errors" "gopkg.in/yaml.v2" ) +var colorsMap map[string]string = map[string]string{ + "black": "\033[1;30m%s\033[0m", + "red": "\033[1;31m%s\033[0m", + "green": "\033[1;32m%s\033[0m", + "yellow": "\033[1;33m%s\033[0m", + "purple": "\033[1;34m%s\033[0m", + "magenta": "\033[1;35m%s\033[0m", + "teal": "\033[1;36m%s\033[0m", + "white": "\033[1;37m%s\033[0m", +} + +func getColorKeyMap() map[string]string { + res := make(map[string]string) + for k := range colorsMap { + res[k] = k + } + return res +} + type TemplateData struct { - Vars map[string]any - Env map[string]string + Vars map[string]any + Env map[string]string + Colors map[string]string } type Interpreter struct { @@ -80,7 +103,7 @@ func (r *Interpreter) GetExecuteTemplate(file string, extraVariables map[string] } } - varStr, err := r.getParsedTemplate("gomake_vars", varCommandArr[0], TemplateData{Env: env, Vars: tempVar}) + varStr, err := r.getParsedTemplate("gomake_vars", varCommandArr[0], TemplateData{Env: env, Vars: tempVar, Colors: getColorKeyMap()}) if err != nil { return nil, nil, err @@ -105,7 +128,7 @@ func (r *Interpreter) GetExecuteTemplate(file string, extraVariables map[string] return nil, nil, err } - b, err := r.getParsedTemplate("gomake", varCommandArr[1], TemplateData{Vars: v, Env: env}) + b, err := r.getParsedTemplate("gomake", varCommandArr[1], TemplateData{Vars: v, Env: env, Colors: getColorKeyMap()}) return b, variables, err } @@ -128,7 +151,7 @@ func (r *Interpreter) Run() error { return err } if r.DryRun { - return r.printDryRun(command, variables) + return r.printDryRun([]StageOperationWrapper{{Name: r.ExecuteCommand, Command: command[r.ExecuteCommand]}}, variables) } cmd := r.cmdHandler.SliceCommands(command[r.ExecuteCommand].Script) @@ -137,11 +160,11 @@ func (r *Interpreter) Run() error { cmd = getDockerCmd(cmd, image) } - err = r.execCmd(executer, cmd) + err = r.execCmd(executer, cmd, log.Writer()) if err != nil { if len(command[r.ExecuteCommand].On_Failure) > 0 { fmt.Println("Script end with error so start onFailure Scripts ...") - err = r.execCmd(executer, r.cmdHandler.SliceCommands(command[r.ExecuteCommand].On_Failure)) + err = r.execCmd(executer, r.cmdHandler.SliceCommands(command[r.ExecuteCommand].On_Failure), log.Writer()) } else { fmt.Println("No onFailure Scripts found but got error") } @@ -149,11 +172,123 @@ func (r *Interpreter) Run() error { return err } -func (r *Interpreter) execCmd(executer, command string) error { +type StageOperationWrapper struct { + Name string + Command command.Operation +} + +type StageOperationWrapperError struct { + StageOperationWrapper + error +} + +func (w *StageOperationWrapper) Write(data []byte) (n int, err error) { + + colorFunc := func(d ...interface{}) string { return fmt.Sprint(d...) } + if w.Command.Color != "" { + colorFunc = w.Color(colorsMap[w.Command.Color]) + + } + fmt.Print(colorFunc(w.Name + ":\t" + string(data))) + // fmt.Printf("%s:\t%s", w.ServiceName, string(data)) + return len(data), nil +} + +func (w *StageOperationWrapper) Color(colorString string) func(...interface{}) string { + sprint := func(args ...interface{}) string { + return fmt.Sprintf(colorString, + fmt.Sprint(args...)) + } + return sprint +} + +func (r *Interpreter) GetStageMap() (map[string][]StageOperationWrapper, command.MakeStruct, map[string]map[string]any, error) { + explizitMakeFile, variables, err := r.GetExecuteTemplate(string(r.commandFile), make(map[string]any)) + if err != nil { + return nil, nil, nil, err + } + c1, err := r.getMakeScripts(explizitMakeFile) + if err != nil { + return nil, nil, nil, err + } + stagesMap := make(map[string][]StageOperationWrapper) + for k, c := range c1 { + if c.Stage != "" { + _, ok := stagesMap[c.Stage] + if !ok { + stagesMap[c.Stage] = make([]StageOperationWrapper, 0) + } + stagesMap[c.Stage] = append(stagesMap[c.Stage], StageOperationWrapper{Name: k, Command: c}) + } + } + return stagesMap, c1, variables, nil +} + +// Stage running +func (r *Interpreter) SRun() error { + stagesMap, c1, variables, err := r.GetStageMap() + if err != nil { + return err + } + + if _, ok := stagesMap[r.ExecuteCommand]; !ok { + return fmt.Errorf("no command with stage %s found at makefile, did you mean \n%s", r.ExecuteCommand, nearfinder.ClosestMatch(r.ExecuteCommand, nearfinder.GetKeysOfMap(stagesMap), 2)) + } + + commands := make([]StageOperationWrapper, 0) + for _, c := range stagesMap[r.ExecuteCommand] { + tmpc, err := r.cmdHandler.GetExecutedCommandMakeScript(c.Name, c1) + if err != nil { + return err + } + commands = append(commands, StageOperationWrapper{Name: c.Name, Command: tmpc[c.Name]}) + } + + if r.DryRun { + return r.printDryRun(commands, variables) + } + w := sync.WaitGroup{} + errList := make([]StageOperationWrapperError, 0) + for _, c := range commands { + w.Add(1) + go func(operator StageOperationWrapper) { + cmd := r.cmdHandler.SliceCommands(operator.Command.Script) + if image := operator.Command.Image; image != nil { + cmd = getDockerCmd(cmd, image) + } + err := r.execCmd(r.executer, cmd, &operator) + if err != nil { + errList = append(errList, StageOperationWrapperError{ + error: err, + StageOperationWrapper: operator, + }) + } + w.Done() + }(c) + } + w.Wait() + var errres error = nil + if len(errList) > 0 { + for _, e := range errList { + if len(e.Command.On_Failure) > 0 { + e.Write([]byte(fmt.Sprintf("%s end with error so start onFailure Scripts ...", e.Name))) + err := r.execCmd(r.executer, r.cmdHandler.SliceCommands(e.Command.On_Failure), &e) + if err != nil { + errres = errors.Wrap(errres, err.Error()) + } + } else { + e.Write([]byte(fmt.Sprintf("No OnFailer Script found but %s got error ", e.Name))) + } + } + } + return errres +} + +func (r *Interpreter) execCmd(executer, command string, writer io.Writer) error { cmd := exec.Command(executer, "-c", command) cmd.Stdin = os.Stdin - cmd.Stdout = log.Writer() - cmd.Stderr = log.Writer() + cmd.Stdout = writer + cmd.Stderr = writer return cmd.Run() } diff --git a/main.go b/main.go index e63a731..5e2f74f 100644 --- a/main.go +++ b/main.go @@ -122,6 +122,28 @@ func main() { Action: runner.Run, Before: runner.RunBefore, }, + { + ArgsUsage: "{executed command name}", + Name: "srun", + Usage: fmt.Sprintf("Run commands from %s file but it run all commands are inside the given stage and run this in parallel", GomakeDefaultFile), + BashComplete: runner.SRunBashComplete, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: DryRunCli, + EnvVars: []string{getFlagEnvByFlagName(DryRunCli)}, + Value: false, + Usage: fmt.Sprintf("Only show template paresed %s but not execute it", GomakeDefaultFile), + }, + &cli.StringSliceFlag{ + Name: VarsCli, + Aliases: []string{"v"}, + EnvVars: []string{getFlagEnvByFlagName(VarsCli)}, + Action: runner.ExtraVariables, + }, + }, + Action: runner.SRun, + Before: runner.RunBefore, + }, }, } @@ -186,6 +208,45 @@ func isFlagAtUseList(used []string, test string) bool { return false } +func (r *Runner) SRunBashComplete(c *cli.Context) { + r.Before(c) + r.RunBefore(c) + autoCompleteHelp := make([]string, 0) + + for _, cs := range c.App.Commands { + if cs.Name == "srun" { + for _, csf := range cs.Flags { + for _, csfn := range csf.Names() { + if csfn == "help" || csfn == "h" { + continue + } + if csfn == DryRunCli { + if isFlagAtUseList(c.FlagNames(), csfn) { + continue + } + } + minusStr := "-" + if len(csfn) > 1 { + minusStr = "--" + } + autoCompleteHelp = append(autoCompleteHelp, fmt.Sprintf("%s%s", minusStr, csfn)) + } + } + } + } + list, _, _, err := r.interpreter.GetStageMap() + if err != nil { + return + } + for k := range list { + autoCompleteHelp = append(autoCompleteHelp, k) + } + + for _, v := range autoCompleteHelp { + fmt.Println(v) + } +} + func (r *Runner) RunBashComplete(c *cli.Context) { r.Before(c) r.RunBefore(c) @@ -278,6 +339,10 @@ func (r *Runner) Run(c *cli.Context) error { return r.interpreter.Run() } +func (r *Runner) SRun(c *cli.Context) error { + return r.interpreter.SRun() +} + func (r *Runner) Init(c *cli.Context) error { _, err := os.Stat(GomakeDefaultFile) if err != nil { @@ -302,9 +367,18 @@ func (r *Runner) List(c *cli.Context) error { if err != nil { return err } - fmt.Println("List of executed Commands:") + fmt.Println("List of executed Commands (for run):") for k := range list { fmt.Println(k) } + + fmt.Println("\nList of executed Stages (for srun):") + stages, _, _, err := r.interpreter.GetStageMap() + if err != nil { + return err + } + for k := range stages { + fmt.Println(k) + } return nil } diff --git a/vendor/github.com/pkg/errors/.gitignore b/vendor/github.com/pkg/errors/.gitignore new file mode 100644 index 0000000..daf913b --- /dev/null +++ b/vendor/github.com/pkg/errors/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/vendor/github.com/pkg/errors/.travis.yml b/vendor/github.com/pkg/errors/.travis.yml new file mode 100644 index 0000000..9159de0 --- /dev/null +++ b/vendor/github.com/pkg/errors/.travis.yml @@ -0,0 +1,10 @@ +language: go +go_import_path: github.com/pkg/errors +go: + - 1.11.x + - 1.12.x + - 1.13.x + - tip + +script: + - make check diff --git a/vendor/github.com/pkg/errors/LICENSE b/vendor/github.com/pkg/errors/LICENSE new file mode 100644 index 0000000..835ba3e --- /dev/null +++ b/vendor/github.com/pkg/errors/LICENSE @@ -0,0 +1,23 @@ +Copyright (c) 2015, Dave Cheney +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. + +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. diff --git a/vendor/github.com/pkg/errors/Makefile b/vendor/github.com/pkg/errors/Makefile new file mode 100644 index 0000000..ce9d7cd --- /dev/null +++ b/vendor/github.com/pkg/errors/Makefile @@ -0,0 +1,44 @@ +PKGS := github.com/pkg/errors +SRCDIRS := $(shell go list -f '{{.Dir}}' $(PKGS)) +GO := go + +check: test vet gofmt misspell unconvert staticcheck ineffassign unparam + +test: + $(GO) test $(PKGS) + +vet: | test + $(GO) vet $(PKGS) + +staticcheck: + $(GO) get honnef.co/go/tools/cmd/staticcheck + staticcheck -checks all $(PKGS) + +misspell: + $(GO) get github.com/client9/misspell/cmd/misspell + misspell \ + -locale GB \ + -error \ + *.md *.go + +unconvert: + $(GO) get github.com/mdempsky/unconvert + unconvert -v $(PKGS) + +ineffassign: + $(GO) get github.com/gordonklaus/ineffassign + find $(SRCDIRS) -name '*.go' | xargs ineffassign + +pedantic: check errcheck + +unparam: + $(GO) get mvdan.cc/unparam + unparam ./... + +errcheck: + $(GO) get github.com/kisielk/errcheck + errcheck $(PKGS) + +gofmt: + @echo Checking code is gofmted + @test -z "$(shell gofmt -s -l -d -e $(SRCDIRS) | tee /dev/stderr)" diff --git a/vendor/github.com/pkg/errors/README.md b/vendor/github.com/pkg/errors/README.md new file mode 100644 index 0000000..54dfdcb --- /dev/null +++ b/vendor/github.com/pkg/errors/README.md @@ -0,0 +1,59 @@ +# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) [![Sourcegraph](https://sourcegraph.com/github.com/pkg/errors/-/badge.svg)](https://sourcegraph.com/github.com/pkg/errors?badge) + +Package errors provides simple error handling primitives. + +`go get github.com/pkg/errors` + +The traditional error handling idiom in Go is roughly akin to +```go +if err != nil { + return err +} +``` +which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error. + +## Adding context to an error + +The errors.Wrap function returns a new error that adds context to the original error. For example +```go +_, err := ioutil.ReadAll(r) +if err != nil { + return errors.Wrap(err, "read failed") +} +``` +## Retrieving the cause of an error + +Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`. +```go +type causer interface { + Cause() error +} +``` +`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example: +```go +switch err := errors.Cause(err).(type) { +case *MyError: + // handle specifically +default: + // unknown error +} +``` + +[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors). + +## Roadmap + +With the upcoming [Go2 error proposals](https://go.googlesource.com/proposal/+/master/design/go2draft.md) this package is moving into maintenance mode. The roadmap for a 1.0 release is as follows: + +- 0.9. Remove pre Go 1.9 and Go 1.10 support, address outstanding pull requests (if possible) +- 1.0. Final release. + +## Contributing + +Because of the Go2 errors changes, this package is not accepting proposals for new functionality. With that said, we welcome pull requests, bug fixes and issue reports. + +Before sending a PR, please discuss your change by raising an issue. + +## License + +BSD-2-Clause diff --git a/vendor/github.com/pkg/errors/appveyor.yml b/vendor/github.com/pkg/errors/appveyor.yml new file mode 100644 index 0000000..a932ead --- /dev/null +++ b/vendor/github.com/pkg/errors/appveyor.yml @@ -0,0 +1,32 @@ +version: build-{build}.{branch} + +clone_folder: C:\gopath\src\github.com\pkg\errors +shallow_clone: true # for startup speed + +environment: + GOPATH: C:\gopath + +platform: + - x64 + +# http://www.appveyor.com/docs/installed-software +install: + # some helpful output for debugging builds + - go version + - go env + # pre-installed MinGW at C:\MinGW is 32bit only + # but MSYS2 at C:\msys64 has mingw64 + - set PATH=C:\msys64\mingw64\bin;%PATH% + - gcc --version + - g++ --version + +build_script: + - go install -v ./... + +test_script: + - set PATH=C:\gopath\bin;%PATH% + - go test -v ./... + +#artifacts: +# - path: '%GOPATH%\bin\*.exe' +deploy: off diff --git a/vendor/github.com/pkg/errors/errors.go b/vendor/github.com/pkg/errors/errors.go new file mode 100644 index 0000000..161aea2 --- /dev/null +++ b/vendor/github.com/pkg/errors/errors.go @@ -0,0 +1,288 @@ +// Package errors provides simple error handling primitives. +// +// The traditional error handling idiom in Go is roughly akin to +// +// if err != nil { +// return err +// } +// +// which when applied recursively up the call stack results in error reports +// without context or debugging information. The errors package allows +// programmers to add context to the failure path in their code in a way +// that does not destroy the original value of the error. +// +// Adding context to an error +// +// The errors.Wrap function returns a new error that adds context to the +// original error by recording a stack trace at the point Wrap is called, +// together with the supplied message. For example +// +// _, err := ioutil.ReadAll(r) +// if err != nil { +// return errors.Wrap(err, "read failed") +// } +// +// If additional control is required, the errors.WithStack and +// errors.WithMessage functions destructure errors.Wrap into its component +// operations: annotating an error with a stack trace and with a message, +// respectively. +// +// Retrieving the cause of an error +// +// Using errors.Wrap constructs a stack of errors, adding context to the +// preceding error. Depending on the nature of the error it may be necessary +// to reverse the operation of errors.Wrap to retrieve the original error +// for inspection. Any error value which implements this interface +// +// type causer interface { +// Cause() error +// } +// +// can be inspected by errors.Cause. errors.Cause will recursively retrieve +// the topmost error that does not implement causer, which is assumed to be +// the original cause. For example: +// +// switch err := errors.Cause(err).(type) { +// case *MyError: +// // handle specifically +// default: +// // unknown error +// } +// +// Although the causer interface is not exported by this package, it is +// considered a part of its stable public interface. +// +// Formatted printing of errors +// +// All error values returned from this package implement fmt.Formatter and can +// be formatted by the fmt package. The following verbs are supported: +// +// %s print the error. If the error has a Cause it will be +// printed recursively. +// %v see %s +// %+v extended format. Each Frame of the error's StackTrace will +// be printed in detail. +// +// Retrieving the stack trace of an error or wrapper +// +// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are +// invoked. This information can be retrieved with the following interface: +// +// type stackTracer interface { +// StackTrace() errors.StackTrace +// } +// +// The returned errors.StackTrace type is defined as +// +// type StackTrace []Frame +// +// The Frame type represents a call site in the stack trace. Frame supports +// the fmt.Formatter interface that can be used for printing information about +// the stack trace of this error. For example: +// +// if err, ok := err.(stackTracer); ok { +// for _, f := range err.StackTrace() { +// fmt.Printf("%+s:%d\n", f, f) +// } +// } +// +// Although the stackTracer interface is not exported by this package, it is +// considered a part of its stable public interface. +// +// See the documentation for Frame.Format for more details. +package errors + +import ( + "fmt" + "io" +) + +// New returns an error with the supplied message. +// New also records the stack trace at the point it was called. +func New(message string) error { + return &fundamental{ + msg: message, + stack: callers(), + } +} + +// Errorf formats according to a format specifier and returns the string +// as a value that satisfies error. +// Errorf also records the stack trace at the point it was called. +func Errorf(format string, args ...interface{}) error { + return &fundamental{ + msg: fmt.Sprintf(format, args...), + stack: callers(), + } +} + +// fundamental is an error that has a message and a stack, but no caller. +type fundamental struct { + msg string + *stack +} + +func (f *fundamental) Error() string { return f.msg } + +func (f *fundamental) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + if s.Flag('+') { + io.WriteString(s, f.msg) + f.stack.Format(s, verb) + return + } + fallthrough + case 's': + io.WriteString(s, f.msg) + case 'q': + fmt.Fprintf(s, "%q", f.msg) + } +} + +// WithStack annotates err with a stack trace at the point WithStack was called. +// If err is nil, WithStack returns nil. +func WithStack(err error) error { + if err == nil { + return nil + } + return &withStack{ + err, + callers(), + } +} + +type withStack struct { + error + *stack +} + +func (w *withStack) Cause() error { return w.error } + +// Unwrap provides compatibility for Go 1.13 error chains. +func (w *withStack) Unwrap() error { return w.error } + +func (w *withStack) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + if s.Flag('+') { + fmt.Fprintf(s, "%+v", w.Cause()) + w.stack.Format(s, verb) + return + } + fallthrough + case 's': + io.WriteString(s, w.Error()) + case 'q': + fmt.Fprintf(s, "%q", w.Error()) + } +} + +// Wrap returns an error annotating err with a stack trace +// at the point Wrap is called, and the supplied message. +// If err is nil, Wrap returns nil. +func Wrap(err error, message string) error { + if err == nil { + return nil + } + err = &withMessage{ + cause: err, + msg: message, + } + return &withStack{ + err, + callers(), + } +} + +// Wrapf returns an error annotating err with a stack trace +// at the point Wrapf is called, and the format specifier. +// If err is nil, Wrapf returns nil. +func Wrapf(err error, format string, args ...interface{}) error { + if err == nil { + return nil + } + err = &withMessage{ + cause: err, + msg: fmt.Sprintf(format, args...), + } + return &withStack{ + err, + callers(), + } +} + +// WithMessage annotates err with a new message. +// If err is nil, WithMessage returns nil. +func WithMessage(err error, message string) error { + if err == nil { + return nil + } + return &withMessage{ + cause: err, + msg: message, + } +} + +// WithMessagef annotates err with the format specifier. +// If err is nil, WithMessagef returns nil. +func WithMessagef(err error, format string, args ...interface{}) error { + if err == nil { + return nil + } + return &withMessage{ + cause: err, + msg: fmt.Sprintf(format, args...), + } +} + +type withMessage struct { + cause error + msg string +} + +func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() } +func (w *withMessage) Cause() error { return w.cause } + +// Unwrap provides compatibility for Go 1.13 error chains. +func (w *withMessage) Unwrap() error { return w.cause } + +func (w *withMessage) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + if s.Flag('+') { + fmt.Fprintf(s, "%+v\n", w.Cause()) + io.WriteString(s, w.msg) + return + } + fallthrough + case 's', 'q': + io.WriteString(s, w.Error()) + } +} + +// Cause returns the underlying cause of the error, if possible. +// An error value has a cause if it implements the following +// interface: +// +// type causer interface { +// Cause() error +// } +// +// If the error does not implement Cause, the original error will +// be returned. If the error is nil, nil will be returned without further +// investigation. +func Cause(err error) error { + type causer interface { + Cause() error + } + + for err != nil { + cause, ok := err.(causer) + if !ok { + break + } + err = cause.Cause() + } + return err +} diff --git a/vendor/github.com/pkg/errors/go113.go b/vendor/github.com/pkg/errors/go113.go new file mode 100644 index 0000000..be0d10d --- /dev/null +++ b/vendor/github.com/pkg/errors/go113.go @@ -0,0 +1,38 @@ +// +build go1.13 + +package errors + +import ( + stderrors "errors" +) + +// Is reports whether any error in err's chain matches target. +// +// The chain consists of err itself followed by the sequence of errors obtained by +// repeatedly calling Unwrap. +// +// An error is considered to match a target if it is equal to that target or if +// it implements a method Is(error) bool such that Is(target) returns true. +func Is(err, target error) bool { return stderrors.Is(err, target) } + +// As finds the first error in err's chain that matches target, and if so, sets +// target to that error value and returns true. +// +// The chain consists of err itself followed by the sequence of errors obtained by +// repeatedly calling Unwrap. +// +// An error matches target if the error's concrete value is assignable to the value +// pointed to by target, or if the error has a method As(interface{}) bool such that +// As(target) returns true. In the latter case, the As method is responsible for +// setting target. +// +// As will panic if target is not a non-nil pointer to either a type that implements +// error, or to any interface type. As returns false if err is nil. +func As(err error, target interface{}) bool { return stderrors.As(err, target) } + +// Unwrap returns the result of calling the Unwrap method on err, if err's +// type contains an Unwrap method returning error. +// Otherwise, Unwrap returns nil. +func Unwrap(err error) error { + return stderrors.Unwrap(err) +} diff --git a/vendor/github.com/pkg/errors/stack.go b/vendor/github.com/pkg/errors/stack.go new file mode 100644 index 0000000..779a834 --- /dev/null +++ b/vendor/github.com/pkg/errors/stack.go @@ -0,0 +1,177 @@ +package errors + +import ( + "fmt" + "io" + "path" + "runtime" + "strconv" + "strings" +) + +// Frame represents a program counter inside a stack frame. +// For historical reasons if Frame is interpreted as a uintptr +// its value represents the program counter + 1. +type Frame uintptr + +// pc returns the program counter for this frame; +// multiple frames may have the same PC value. +func (f Frame) pc() uintptr { return uintptr(f) - 1 } + +// file returns the full path to the file that contains the +// function for this Frame's pc. +func (f Frame) file() string { + fn := runtime.FuncForPC(f.pc()) + if fn == nil { + return "unknown" + } + file, _ := fn.FileLine(f.pc()) + return file +} + +// line returns the line number of source code of the +// function for this Frame's pc. +func (f Frame) line() int { + fn := runtime.FuncForPC(f.pc()) + if fn == nil { + return 0 + } + _, line := fn.FileLine(f.pc()) + return line +} + +// name returns the name of this function, if known. +func (f Frame) name() string { + fn := runtime.FuncForPC(f.pc()) + if fn == nil { + return "unknown" + } + return fn.Name() +} + +// Format formats the frame according to the fmt.Formatter interface. +// +// %s source file +// %d source line +// %n function name +// %v equivalent to %s:%d +// +// Format accepts flags that alter the printing of some verbs, as follows: +// +// %+s function name and path of source file relative to the compile time +// GOPATH separated by \n\t (\n\t) +// %+v equivalent to %+s:%d +func (f Frame) Format(s fmt.State, verb rune) { + switch verb { + case 's': + switch { + case s.Flag('+'): + io.WriteString(s, f.name()) + io.WriteString(s, "\n\t") + io.WriteString(s, f.file()) + default: + io.WriteString(s, path.Base(f.file())) + } + case 'd': + io.WriteString(s, strconv.Itoa(f.line())) + case 'n': + io.WriteString(s, funcname(f.name())) + case 'v': + f.Format(s, 's') + io.WriteString(s, ":") + f.Format(s, 'd') + } +} + +// MarshalText formats a stacktrace Frame as a text string. The output is the +// same as that of fmt.Sprintf("%+v", f), but without newlines or tabs. +func (f Frame) MarshalText() ([]byte, error) { + name := f.name() + if name == "unknown" { + return []byte(name), nil + } + return []byte(fmt.Sprintf("%s %s:%d", name, f.file(), f.line())), nil +} + +// StackTrace is stack of Frames from innermost (newest) to outermost (oldest). +type StackTrace []Frame + +// Format formats the stack of Frames according to the fmt.Formatter interface. +// +// %s lists source files for each Frame in the stack +// %v lists the source file and line number for each Frame in the stack +// +// Format accepts flags that alter the printing of some verbs, as follows: +// +// %+v Prints filename, function, and line number for each Frame in the stack. +func (st StackTrace) Format(s fmt.State, verb rune) { + switch verb { + case 'v': + switch { + case s.Flag('+'): + for _, f := range st { + io.WriteString(s, "\n") + f.Format(s, verb) + } + case s.Flag('#'): + fmt.Fprintf(s, "%#v", []Frame(st)) + default: + st.formatSlice(s, verb) + } + case 's': + st.formatSlice(s, verb) + } +} + +// formatSlice will format this StackTrace into the given buffer as a slice of +// Frame, only valid when called with '%s' or '%v'. +func (st StackTrace) formatSlice(s fmt.State, verb rune) { + io.WriteString(s, "[") + for i, f := range st { + if i > 0 { + io.WriteString(s, " ") + } + f.Format(s, verb) + } + io.WriteString(s, "]") +} + +// stack represents a stack of program counters. +type stack []uintptr + +func (s *stack) Format(st fmt.State, verb rune) { + switch verb { + case 'v': + switch { + case st.Flag('+'): + for _, pc := range *s { + f := Frame(pc) + fmt.Fprintf(st, "\n%+v", f) + } + } + } +} + +func (s *stack) StackTrace() StackTrace { + f := make([]Frame, len(*s)) + for i := 0; i < len(f); i++ { + f[i] = Frame((*s)[i]) + } + return f +} + +func callers() *stack { + const depth = 32 + var pcs [depth]uintptr + n := runtime.Callers(3, pcs[:]) + var st stack = pcs[0:n] + return &st +} + +// funcname removes the path prefix component of a function's name reported by func.Name(). +func funcname(name string) string { + i := strings.LastIndex(name, "/") + name = name[i+1:] + i = strings.Index(name, ".") + return name[i+1:] +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 3a936b9..0a58252 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -25,6 +25,9 @@ github.com/mitchellh/copystructure # github.com/mitchellh/reflectwalk v1.0.0 ## explicit github.com/mitchellh/reflectwalk +# github.com/pkg/errors v0.9.1 +## explicit +github.com/pkg/errors # github.com/russross/blackfriday/v2 v2.1.0 ## explicit github.com/russross/blackfriday/v2