2023-12-10 20:21:54 +01:00
|
|
|
// cmd is a package for running commands in the different operating systems implementations
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// CmdRunner: run cmd commands when instantiating a network
|
2023-12-10 20:21:54 +01:00
|
|
|
type CmdRunner interface {
|
|
|
|
RunCommands(commands ...string) error
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// UnixCmdRunner: Run UNIX commands
|
2023-12-10 20:21:54 +01:00
|
|
|
type UnixCmdRunner struct{}
|
|
|
|
|
|
|
|
// RunCommand: runs the unix command. It splits the command into fields
|
|
|
|
// and then runs the command accordingly
|
|
|
|
func RunCommand(cmd string) error {
|
|
|
|
args := strings.Fields(cmd)
|
|
|
|
c := exec.Command(args[0], args[1:]...)
|
|
|
|
return c.Run()
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// RunCommands: run a series of commands
|
2023-12-10 20:21:54 +01:00
|
|
|
func (l *UnixCmdRunner) RunCommands(commands ...string) error {
|
|
|
|
for _, cmd := range commands {
|
|
|
|
err := RunCommand(cmd)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|