Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor command execution to separate stdout and stderr #28

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
32 changes: 23 additions & 9 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 Expand Up @@ -200,7 +214,7 @@ func unmarshalInterfacesData(data string) (Interfaces, error) {
var addresses Interfaces
err := json.Unmarshal([]byte(data), &addresses)
if err != nil {
return nil, err
return nil, &UnmarshalError{Msg: err.Error(), Content: data}
}

return addresses, nil
Expand Down
9 changes: 9 additions & 0 deletions iproute2/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ func (e *OperationNotPermittedError) Error() string {
return e.Msg
}

type UnmarshalError struct {
Msg string
Content string
}

func (e *UnmarshalError) Error() string {
return fmt.Sprint(e.Msg, ": ", e.Content)
}

type UnknownError struct {
Msg string
}
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)
}