Add convenience wrappers to shell out to any binary in Go

https://github.com/containers/toolbox/pull/318
This commit is contained in:
Debarshi Ray 2020-05-07 13:21:11 +02:00
parent 53f4d0c2f0
commit 19081b5d4a
4 changed files with 83 additions and 0 deletions

View File

@ -1,3 +1,5 @@
module github.com/containers/toolbox
go 1.13
require github.com/sirupsen/logrus v1.5.0

12
src/go.sum Normal file
View File

@ -0,0 +1,12 @@
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/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
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/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q=
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -4,6 +4,7 @@ go_build_wrapper_program = find_program('go-build-wrapper')
sources = files(
'toolbox.go',
'cmd/root.go',
'pkg/shell/shell.go',
'pkg/version/version.go',
)

68
src/pkg/shell/shell.go Normal file
View File

@ -0,0 +1,68 @@
/*
* Copyright © 2019 2020 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package shell
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"github.com/sirupsen/logrus"
)
func Run(name string, stdin io.Reader, stdout, stderr io.Writer, arg ...string) error {
exitCode, err := RunWithExitCode(name, stdin, stdout, stderr, arg...)
if exitCode != 0 && err == nil {
err = fmt.Errorf("failed to invoke %s(1)", name)
}
if err != nil {
return err
}
return nil
}
func RunWithExitCode(name string, stdin io.Reader, stdout, stderr io.Writer, arg ...string) (int, error) {
logLevel := logrus.GetLevel()
if stderr == nil && logLevel >= logrus.DebugLevel {
stderr = os.Stderr
}
cmd := exec.Command(name, arg...)
cmd.Stdin = stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
if errors.Is(err, exec.ErrNotFound) {
return 1, fmt.Errorf("%s(1) not found", name)
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
exitCode := exitErr.ExitCode()
return exitCode, nil
}
return 1, fmt.Errorf("failed to invoke %s(1)", name)
}
return 0, nil
}