Skip to content

Commit

Permalink
Refactor command execution to separate stdout and stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Apr 18, 2024
1 parent 9256567 commit 44910a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func SetupVethDevices(netns string, devices map[string]config.VethDevice) error
peerNetns := values.Peer.Netns

slog.Debug("setup veth device", "netns", netns, "name", name, "addresses", values.Addresses, "routes", values.Routes,
"peer name", peerName, "peer netns", peerNetns, "peer addresses", values.Peer.Addresses, "peer routes", values.Peer.Routes)
"peer name", peerName, "peer netns", peerNetns, "peer addresses", values.Peer.Addresses, "peer routes", values.Peer.Routes)

// check if device is already exists in netns
_, err := n.ShowLink(name)
Expand Down Expand Up @@ -332,7 +332,7 @@ func RunPostScript(netns string, script string) error {
if err != nil {
return err
}
slog.Debug("post script output", "netns", netns, "script", script, "output", out)
slog.Debug("post script output", "netns", netns, "script", script, "stdout", out.Stdout, "stderr", out.Stderr)

return nil
}
30 changes: 22 additions & 8 deletions iproute2/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ SOFTWARE.
package iproute2

import (
"bytes"
"encoding/json"
"io"
"log/slog"
"os/exec"
"strings"
"syscall"
Expand All @@ -34,6 +36,11 @@ type BaseCommand struct {
prepend []string
}

type CommandOut struct {
Stdout string
Stderr string
}

func (b *BaseCommand) run(args ...string) error {
_, err := b.runIpCommand(args...)
return err
Expand All @@ -43,7 +50,11 @@ func (b *BaseCommand) runIpCommand(args ...string) (string, error) {
cmd := append([]string{b.path}, args...)
out, err := b.runCommand(cmd, nil)
if err == nil {
return out, nil
if out.Stderr != "" {
slog.Warn("ip command warning", "msg", out.Stderr)
}

return out.Stdout, nil
}

msg := err.Error()
Expand All @@ -58,7 +69,7 @@ func (b *BaseCommand) runIpCommand(args ...string) (string, error) {
return "", &UnknownError{Msg: err.Error()}
}

func (b *BaseCommand) runCommand(cmd []string, input *string) (string, error) {
func (b *BaseCommand) runCommand(cmd []string, input *string) (*CommandOut, error) {
if b.prepend != nil {
cmd = append(b.prepend, cmd...)
}
Expand All @@ -73,24 +84,27 @@ func (b *BaseCommand) runCommand(cmd []string, input *string) (string, error) {
if input != nil {
stdin, err := c.StdinPipe()
if err != nil {
return "", err
return nil, err
}
go func() {
defer stdin.Close()
io.WriteString(stdin, *input)
}()
}

out, err := c.CombinedOutput()
if err != nil {
var stdout, stderr bytes.Buffer
c.Stdout = &stdout
c.Stderr = &stderr

if err := c.Run(); err != nil {
exitErr, _ := err.(*exec.ExitError)
status, _ := exitErr.Sys().(syscall.WaitStatus)
return "", &CommandError{
return nil, &CommandError{
ExitStatus: status.ExitStatus(),
Msg: string(out),
Msg: stderr.String(),
}
}
return string(out), nil
return &CommandOut{Stdout: stdout.String(), Stderr: stderr.String()}, nil
}

func (b *BaseCommand) AddLink(name string, linkType string, options ...string) error {
Expand Down
2 changes: 1 addition & 1 deletion iproute2/iproute2.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (i *IpCmdWithNetns) Netns() string {
return i.netns
}

func (i *IpCmdWithNetns) ExecuteCommand(cmd string) (string, error) {
func (i *IpCmdWithNetns) ExecuteCommand(cmd string) (*CommandOut, error) {
shell := []string{"/bin/bash"}
return i.runCommand(shell, &cmd)
}

0 comments on commit 44910a1

Please sign in to comment.