mirror of
https://github.com/openfaas/faasd.git
synced 2025-06-21 14:23:34 +00:00
Upgrade to go-execute/v2
Upgrades to go-execute/v2 and updates various other dependencies. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
This commit is contained in:
1
vendor/github.com/alexellis/go-execute/v2/.DEREK.yml
generated
vendored
Normal file
1
vendor/github.com/alexellis/go-execute/v2/.DEREK.yml
generated
vendored
Normal file
@ -0,0 +1 @@
|
||||
redirect: https://raw.githubusercontent.com/openfaas/faas/master/.DEREK.yml
|
2
vendor/github.com/alexellis/go-execute/v2/.gitignore
generated
vendored
Normal file
2
vendor/github.com/alexellis/go-execute/v2/.gitignore
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/go-execute
|
||||
main.go
|
21
vendor/github.com/alexellis/go-execute/v2/LICENSE
generated
vendored
Normal file
21
vendor/github.com/alexellis/go-execute/v2/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 Alex Ellis
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
2
vendor/github.com/alexellis/go-execute/v2/Makefile
generated
vendored
Normal file
2
vendor/github.com/alexellis/go-execute/v2/Makefile
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
all:
|
||||
go test -v ./...
|
143
vendor/github.com/alexellis/go-execute/v2/README.md
generated
vendored
Normal file
143
vendor/github.com/alexellis/go-execute/v2/README.md
generated
vendored
Normal file
@ -0,0 +1,143 @@
|
||||
## go-execute
|
||||
|
||||
A wrapper for Go's command execution packages.
|
||||
|
||||
`go get github.com/alexellis/go-execute/v2`
|
||||
|
||||
## Docs
|
||||
|
||||
See docs at pkg.go.dev: [github.com/alexellis/go-execute](https://pkg.go.dev/github.com/alexellis/go-execute)
|
||||
|
||||
## go-execute users
|
||||
|
||||
[Used by dozens of projects as identified by GitHub](https://github.com/alexellis/go-execute/network/dependents), notably:
|
||||
|
||||
* [alexellis/arkade](https://github.com/alexellis/arkade)
|
||||
* [openfaas/faas-cli](https://github.com/openfaas/faas-cli)
|
||||
* [inlets/inletsctl](https://github.com/inlets/inletsctl)
|
||||
* [inlets/cloud-provision](https://github.com/inlets/cloud-provision)
|
||||
* [alexellis/k3sup](https://github.com/alexellis/k3sup)
|
||||
* [openfaas/connector-sdk](https://github.com/openfaas/connector-sdk)
|
||||
* [openfaas-incubator/ofc-bootstrap](https://github.com/openfaas-incubator/ofc-bootstrap)
|
||||
|
||||
Community examples:
|
||||
|
||||
* [dokku/lambda-builder](https://github.com/dokku/lambda-builder)
|
||||
* [027xiguapi/pear-rec](https://github.com/027xiguapi/pear-rec)
|
||||
* [cnrancher/autok3s](https://github.com/cnrancher/autok3s)
|
||||
* [ainsleydev/hupi](https://github.com/ainsleydev/hupi)
|
||||
* [andiwork/andictl](https://github.com/andiwork/andictl)
|
||||
* [tonit/rekind](https://github.com/tonit/rekind)
|
||||
* [lucasrod16/ec2-k3s](https://github.com/lucasrod16/ec2-k3s)
|
||||
* [seaweedfs/seaweed-up](https://github.com/seaweedfs/seaweed-up)
|
||||
* [jsiebens/inlets-on-fly](https://github.com/jsiebens/inlets-on-fly)
|
||||
* [jsiebens/hashi-up](https://github.com/jsiebens/hashi-up)
|
||||
* [edgego/ecm](https://github.com/edgego/ecm)
|
||||
* [ministryofjustice/cloud-platform-terraform-upgrade](https://github.com/ministryofjustice/cloud-platform-terraform-upgrade)
|
||||
* [mattcanty/go-ffmpeg-transcode](https://github.com/mattcanty/go-ffmpeg-transcode)
|
||||
* [Popoola-Opeyemi/meeseeks](https://github.com/Popoola-Opeyemi/meeseeks)
|
||||
* [aidun/minicloud](https://github.com/aidun/minicloud)
|
||||
|
||||
Feel free to add a link to your own projects in a PR.
|
||||
|
||||
## Main options
|
||||
|
||||
* `DisableStdioBuffer` - Discard Stdio, rather than buffering into memory
|
||||
* `StreamStdio` - Stream stderr and stdout to the console, useful for debugging and testing
|
||||
* `Shell` - Use bash as a shell to execute the command, rather than exec a binary directly
|
||||
* `StdOutWriter` - an additional writer for stdout, useful for mutating or filtering the output
|
||||
* `StdErrWriter` - an additional writer for stderr, useful for mutating or filtering the output
|
||||
* `PrintCommand` - print the command to stdout before executing it
|
||||
|
||||
## Example of exec without streaming to STDIO
|
||||
|
||||
This example captures the values from stdout and stderr without relaying to the console. This means the values can be inspected and used for automation.
|
||||
|
||||
```golang
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
execute "github.com/alexellis/go-execute/v2"
|
||||
"context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cmd := execute.ExecTask{
|
||||
Command: "docker",
|
||||
Args: []string{"version"},
|
||||
StreamStdio: false,
|
||||
}
|
||||
|
||||
res, err := cmd.Execute(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if res.ExitCode != 0 {
|
||||
panic("Non-zero exit code: " + res.Stderr)
|
||||
}
|
||||
|
||||
fmt.Printf("stdout: %s, stderr: %s, exit-code: %d\n", res.Stdout, res.Stderr, res.ExitCode)
|
||||
}
|
||||
```
|
||||
|
||||
## Example with "shell" and exit-code 0
|
||||
|
||||
```golang
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
execute "github.com/alexellis/go-execute/v2"
|
||||
"context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ls := execute.ExecTask{
|
||||
Command: "ls",
|
||||
Args: []string{"-l"},
|
||||
Shell: true,
|
||||
}
|
||||
res, err := ls.Execute(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("stdout: %q, stderr: %q, exit-code: %d\n", res.Stdout, res.Stderr, res.ExitCode)
|
||||
}
|
||||
```
|
||||
|
||||
## Example with "shell" and exit-code 1
|
||||
|
||||
```golang
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"context"
|
||||
execute "github.com/alexellis/go-execute/v2"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ls := execute.ExecTask{
|
||||
Command: "exit 1",
|
||||
Shell: true,
|
||||
}
|
||||
res, err := ls.Execute(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("stdout: %q, stderr: %q, exit-code: %d\n", res.Stdout, res.Stderr, res.ExitCode)
|
||||
}
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Commits must be signed off with `git commit -s`
|
||||
|
||||
License: MIT
|
193
vendor/github.com/alexellis/go-execute/v2/exec.go
generated
vendored
Normal file
193
vendor/github.com/alexellis/go-execute/v2/exec.go
generated
vendored
Normal file
@ -0,0 +1,193 @@
|
||||
package execute
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ExecTask struct {
|
||||
// Command is the command to execute. This can be the path to an executable
|
||||
// or the executable with arguments. The arguments are detected by looking for
|
||||
// a space.
|
||||
//
|
||||
// Any arguments must be given via Args
|
||||
Command string
|
||||
|
||||
// Args are the arguments to pass to the command. These are ignored if the
|
||||
// Command contains arguments.
|
||||
Args []string
|
||||
|
||||
// Shell run the command in a bash shell.
|
||||
// Note that the system must have `bash` installed in the PATH or in /bin/bash
|
||||
Shell bool
|
||||
|
||||
// Env is a list of environment variables to add to the current environment,
|
||||
// these are used to override any existing environment variables.
|
||||
Env []string
|
||||
|
||||
// Cwd is the working directory for the command
|
||||
Cwd string
|
||||
|
||||
// Stdin connect a reader to stdin for the command
|
||||
// being executed.
|
||||
Stdin io.Reader
|
||||
|
||||
// PrintCommand prints the command before executing
|
||||
PrintCommand bool
|
||||
|
||||
// StreamStdio prints stdout and stderr directly to os.Stdout/err as
|
||||
// the command runs.
|
||||
StreamStdio bool
|
||||
|
||||
// DisableStdioBuffer prevents any output from being saved in the
|
||||
// TaskResult, which is useful for when the result is very large, or
|
||||
// when you want to stream the output to another writer exclusively.
|
||||
DisableStdioBuffer bool
|
||||
|
||||
// StdoutWriter when set will receive a copy of stdout from the command
|
||||
StdOutWriter io.Writer
|
||||
|
||||
// StderrWriter when set will receive a copy of stderr from the command
|
||||
StdErrWriter io.Writer
|
||||
}
|
||||
|
||||
type ExecResult struct {
|
||||
Stdout string
|
||||
Stderr string
|
||||
ExitCode int
|
||||
Cancelled bool
|
||||
}
|
||||
|
||||
func (et ExecTask) Execute(ctx context.Context) (ExecResult, error) {
|
||||
argsSt := ""
|
||||
if len(et.Args) > 0 {
|
||||
argsSt = strings.Join(et.Args, " ")
|
||||
}
|
||||
|
||||
if et.PrintCommand {
|
||||
fmt.Println("exec: ", et.Command, argsSt)
|
||||
}
|
||||
|
||||
// don't try to run if the context is already cancelled
|
||||
if ctx.Err() != nil {
|
||||
return ExecResult{
|
||||
// the exec package returns -1 for cancelled commands
|
||||
ExitCode: -1,
|
||||
Cancelled: ctx.Err() == context.Canceled,
|
||||
}, ctx.Err()
|
||||
}
|
||||
|
||||
var command string
|
||||
var commandArgs []string
|
||||
if et.Shell {
|
||||
|
||||
// On a NixOS system, /bin/bash doesn't exist at /bin/bash
|
||||
// the default behavior of exec.Command is to look for the
|
||||
// executable in PATH.
|
||||
|
||||
command = "bash"
|
||||
// There is a chance that PATH is not populate or propagated, therefore
|
||||
// when bash cannot be resolved, set it to /bin/bash instead.
|
||||
if _, err := exec.LookPath(command); err != nil {
|
||||
command = "/bin/bash"
|
||||
}
|
||||
|
||||
if len(et.Args) == 0 {
|
||||
// use Split and Join to remove any extra whitespace?
|
||||
startArgs := strings.Split(et.Command, " ")
|
||||
script := strings.Join(startArgs, " ")
|
||||
commandArgs = append([]string{"-c"}, script)
|
||||
|
||||
} else {
|
||||
script := strings.Join(et.Args, " ")
|
||||
commandArgs = append([]string{"-c"}, fmt.Sprintf("%s %s", et.Command, script))
|
||||
}
|
||||
} else {
|
||||
|
||||
command = et.Command
|
||||
commandArgs = et.Args
|
||||
|
||||
// AE: This had to be removed to fix: #117 where Windows users
|
||||
// have spaces in their paths, which are misinterpreted as
|
||||
// arguments for the command.
|
||||
// if strings.Contains(et.Command, " ") {
|
||||
// parts := strings.Split(et.Command, " ")
|
||||
// command = parts[0]
|
||||
// commandArgs = parts[1:]
|
||||
// }
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, command, commandArgs...)
|
||||
cmd.Dir = et.Cwd
|
||||
|
||||
if len(et.Env) > 0 {
|
||||
overrides := map[string]bool{}
|
||||
for _, env := range et.Env {
|
||||
key := strings.Split(env, "=")[0]
|
||||
overrides[key] = true
|
||||
cmd.Env = append(cmd.Env, env)
|
||||
}
|
||||
|
||||
for _, env := range os.Environ() {
|
||||
key := strings.Split(env, "=")[0]
|
||||
|
||||
if _, ok := overrides[key]; !ok {
|
||||
cmd.Env = append(cmd.Env, env)
|
||||
}
|
||||
}
|
||||
}
|
||||
if et.Stdin != nil {
|
||||
cmd.Stdin = et.Stdin
|
||||
}
|
||||
|
||||
stdoutBuff := bytes.Buffer{}
|
||||
stderrBuff := bytes.Buffer{}
|
||||
|
||||
var stdoutWriters []io.Writer
|
||||
var stderrWriters []io.Writer
|
||||
|
||||
if !et.DisableStdioBuffer {
|
||||
stdoutWriters = append(stdoutWriters, &stdoutBuff)
|
||||
stderrWriters = append(stderrWriters, &stderrBuff)
|
||||
}
|
||||
|
||||
if et.StreamStdio {
|
||||
stdoutWriters = append(stdoutWriters, os.Stdout)
|
||||
stderrWriters = append(stderrWriters, os.Stderr)
|
||||
}
|
||||
|
||||
if et.StdOutWriter != nil {
|
||||
stdoutWriters = append(stdoutWriters, et.StdOutWriter)
|
||||
}
|
||||
if et.StdErrWriter != nil {
|
||||
stderrWriters = append(stderrWriters, et.StdErrWriter)
|
||||
}
|
||||
|
||||
cmd.Stdout = io.MultiWriter(stdoutWriters...)
|
||||
cmd.Stderr = io.MultiWriter(stderrWriters...)
|
||||
|
||||
startErr := cmd.Start()
|
||||
if startErr != nil {
|
||||
return ExecResult{}, startErr
|
||||
}
|
||||
|
||||
exitCode := 0
|
||||
execErr := cmd.Wait()
|
||||
if execErr != nil {
|
||||
if exitError, ok := execErr.(*exec.ExitError); ok {
|
||||
exitCode = exitError.ExitCode()
|
||||
}
|
||||
}
|
||||
|
||||
return ExecResult{
|
||||
Stdout: stdoutBuff.String(),
|
||||
Stderr: stderrBuff.String(),
|
||||
ExitCode: exitCode,
|
||||
Cancelled: ctx.Err() == context.Canceled,
|
||||
}, ctx.Err()
|
||||
}
|
Reference in New Issue
Block a user