refactor: remove all calls to common.NewShellCmdWithArgs

This commit is contained in:
Jose Diaz-Gonzalez
2024-02-12 20:58:51 -05:00
parent 3c6cc68086
commit d8401a4d86
4 changed files with 76 additions and 56 deletions

View File

@@ -18,6 +18,19 @@ func ContainerIsRunning(containerID string) bool {
return strings.TrimSpace(string(b[:])) == "true"
}
// ContainerStart runs 'docker container start' against an existing container
func ContainerStart(containerID string) bool {
result, err := CallExecCommand(ExecCommandInput{
Command: DockerBin(),
Args: []string{"container", "start", containerID},
StreamStderr: true,
})
if err != nil {
return false
}
return result.ExitCode == 0
}
// ContainerRemove runs 'docker container remove' against an existing container
func ContainerRemove(containerID string) bool {
result, err := CallExecCommand(ExecCommandInput{
@@ -43,6 +56,19 @@ func ContainerExists(containerID string) bool {
return result.ExitCode == 0
}
// ContainerWait runs 'docker container wait' against an existing container
func ContainerWait(containerID string) bool {
result, err := CallExecCommand(ExecCommandInput{
Command: DockerBin(),
Args: []string{"container", "wait", containerID},
StreamStderr: true,
})
if err != nil {
return false
}
return result.ExitCode == 0
}
// ContainerWaitTilReady will wait timeout seconds and then check if a container is running
// returning an error if it is not running at the end of the timeout
func ContainerWaitTilReady(containerID string, timeout time.Duration) error {

View File

@@ -42,6 +42,12 @@ type ExecCommandInput struct {
// StreamStderr prints stderr directly to os.Stderr as the command runs.
StreamStderr bool
// StdoutWriter is the writer to write stdout to
StdoutWriter io.Writer
// StderrWriter is the writer to write stderr to
StderrWriter io.Writer
// Sudo runs the command with sudo -n -u root
Sudo bool
}
@@ -137,6 +143,12 @@ func CallExecCommandWithContext(ctx context.Context, input ExecCommandInput) (Ex
if input.StreamStderr {
cmd.StdErrWriter = os.Stderr
}
if input.StdoutWriter != nil {
cmd.StdOutWriter = input.StdoutWriter
}
if input.StderrWriter != nil {
cmd.StdErrWriter = input.StderrWriter
}
res, err := cmd.Execute(ctx)
if err != nil {

View File

@@ -174,22 +174,27 @@ func LogVerboseQuietContainerLogsTail(containerID string, lines int, tail bool)
args = append(args, "--follow")
}
sc := NewShellCmdWithArgs(DockerBin(), args...)
var mu sync.Mutex
sc.Command.Stdout = &writer{
mu: &mu,
source: "stdout",
}
sc.Command.Stderr = &writer{
mu: &mu,
source: "stderr",
}
result, err := CallExecCommand(ExecCommandInput{
Command: DockerBin(),
Args: args,
DisableStdioBuffer: true,
StdoutWriter: &writer{
mu: &mu,
source: "stdout",
},
StderrWriter: &writer{
mu: &mu,
source: "stderr",
},
})
if err := sc.Command.Start(); err != nil {
if err != nil {
LogExclaim(fmt.Sprintf("Failed to fetch container logs: %s", containerID))
return
}
if err := sc.Command.Wait(); err != nil {
if !tail && result.ExitCode != 0 {
LogExclaim(fmt.Sprintf("Failed to fetch container logs: %s", containerID))
}
}