46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package internal
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
log "github.com/sirupsen/logrus"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// runOciCli execute binary formatted like this `{ociCli} {ociVerb} {ociOpt} {registry:port/image} {args}`
|
|
func runOciCli(ociCli string, ociVerb string, ociCliOpt string, registry Registry, image Image, args ...string) (string, error) {
|
|
if reg := registry.String(); reg != "" {
|
|
args = append([]string{fmt.Sprintf("%s/%s", registry.String(), image.String())}, args...)
|
|
} else {
|
|
args = append([]string{image.String()}, args...)
|
|
}
|
|
|
|
if ociCliOpt != "" {
|
|
args = append(strings.Split(ociCliOpt, " "), args...)
|
|
}
|
|
|
|
return runCli(ociCli, ociVerb, args...)
|
|
}
|
|
|
|
// runCli execute binary formatted like this `{bin} {verb} {args}`
|
|
func runCli(binary string, verb string, args ...string) (string, error) {
|
|
if verb != "" {
|
|
args = append(strings.Split(verb, " "), args...)
|
|
}
|
|
|
|
log.Infof(" %s %s\n", binary, strings.Join(args, " "))
|
|
|
|
cmd := exec.Command(binary, args...)
|
|
var out bytes.Buffer
|
|
var stderr bytes.Buffer
|
|
cmd.Stdout = &out
|
|
cmd.Stderr = &stderr
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
log.Warnf("Executing binary with argument : `%s %s`\n", binary, strings.Join(args, " "))
|
|
return stderr.String(), err
|
|
}
|
|
return out.String(), nil
|
|
}
|