chore: remove all direct usage of go-sh outside of plugin trigger setup

The new way is to use CallExecCommand instead.
This commit is contained in:
Jose Diaz-Gonzalez
2024-02-12 19:28:23 -05:00
parent 688ee518af
commit 8a940c6e47
11 changed files with 99 additions and 99 deletions

View File

@@ -7,8 +7,6 @@ import (
"strings"
"github.com/dokku/dokku/plugins/common"
sh "github.com/codeskyblue/go-sh"
)
// attachAppToNetwork attaches a container to a network
@@ -60,20 +58,23 @@ func attachAppToNetwork(containerID string, networkName string, appName string,
// isContainerInNetwork returns true if the container is already attached to the specified network
func isContainerInNetwork(containerID string, networkName string) bool {
b, err := sh.Command(
common.DockerBin(),
"container",
"inspect",
"--format",
"{{range $net, $v := .NetworkSettings.Networks}}{{println $net}}{{end}}",
containerID,
).Output()
result, err := common.CallExecCommand(common.ExecCommandInput{
Command: common.DockerBin(),
Args: []string{"container", "inspect", "--format", "{{range $net, $v := .NetworkSettings.Networks}}{{println $net}}{{end}}", containerID},
CaptureOutput: true,
StreamStdio: false,
})
if err != nil {
common.LogVerboseQuiet(fmt.Sprintf("Error checking container networking status:%v", err.Error()))
common.LogVerboseQuiet(fmt.Sprintf("Error checking container networking status: %v", err.Error()))
return false
}
if result.ExitCode != 0 {
common.LogVerboseQuiet(fmt.Sprintf("Error checking container networking status: %v", result.StderrContents()))
return false
}
for _, line := range strings.Split(strings.TrimSpace(string(b[:])), "\n") {
for _, line := range strings.Split(result.StdoutContents(), "\n") {
network := strings.TrimSpace(line)
if network == "" {
continue
@@ -128,15 +129,21 @@ func networkExists(networkName string) (bool, error) {
// listNetworks returns a list of docker networks
func listNetworks() ([]string, error) {
b, err := sh.Command(common.DockerBin(), "network", "ls", "--format", "{{ .Name }}").Output()
output := strings.TrimSpace(string(b[:]))
networks := []string{}
result, err := common.CallExecCommand(common.ExecCommandInput{
Command: common.DockerBin(),
Args: []string{"network", "ls", "--format", "{{ .Name }}"},
CaptureOutput: true,
StreamStdio: false,
})
if err != nil {
common.LogVerboseQuiet(output)
return networks, err
common.LogVerboseQuiet(result.StderrContents())
return []string{}, err
}
if result.ExitCode != 0 {
common.LogVerboseQuiet(result.StderrContents())
return []string{}, fmt.Errorf("Unable to list networks")
}
networks = strings.Split(output, "\n")
networks := strings.Split(result.StdoutContents(), "\n")
return networks, nil
}

View File

@@ -3,7 +3,6 @@ module github.com/dokku/dokku/plugins/network
go 1.21
require (
github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27
github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000
github.com/spf13/pflag v1.0.5
)
@@ -11,6 +10,7 @@ require (
require (
github.com/alexellis/go-execute/v2 v2.2.1 // indirect
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect