From 77ca78195d928f12428d9e851a5c9b64efc14137 Mon Sep 17 00:00:00 2001 From: postmannen <postmannen@gmail.com> Date: Sat, 21 Dec 2024 23:45:34 +0100 Subject: [PATCH] added so if shell are defined via flag that will be used, and not the default shell of the OS. --- configuration_flags.go | 4 ++- requests_cli.go | 64 ++++++++++++++++++------------------------ 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/configuration_flags.go b/configuration_flags.go index 722f01c..adea6a0 100644 --- a/configuration_flags.go +++ b/configuration_flags.go @@ -226,7 +226,9 @@ func NewConfiguration() *Configuration { log.Fatalf("error: the centralNodeName config option or flag cannot be empty, check -help\n") } - c.ShellOnNode = getShell() + if c.ShellOnNode == "" { + c.ShellOnNode = getShell() + } fmt.Printf("\n******** DETECTED SHELL: %v\n\n", c.ShellOnNode) flag.Parse() diff --git a/requests_cli.go b/requests_cli.go index 0e418a4..dad44c7 100644 --- a/requests_cli.go +++ b/requests_cli.go @@ -3,6 +3,7 @@ package ctrl import ( "bufio" "bytes" + "context" "fmt" "os/exec" "runtime" @@ -65,24 +66,7 @@ func methodCliCommand(proc process, message Message, node string) ([]byte, error } } - var cmd *exec.Cmd - - // For the Linux and Darwin operating system we allow to automatically detect - // shell interpreter, so the user don't have to type "bash", "-c" as the first - // two arguments of the methodArgs. - // We use the shell defined in the ShellOnNode variable as interpreter. Since - // it expects a "-c" directly after in the command we prefix it to the args. - if proc.configuration.ShellOnNode != "" { - switch runtime.GOOS { - case "linux", "darwin": - args := []string{"-c"} - args = append(args, message.MethodArgs...) - - cmd = exec.CommandContext(ctx, proc.configuration.ShellOnNode, args...) - default: - cmd = exec.CommandContext(ctx, message.MethodArgs[0], message.MethodArgs...) - } - } + cmd := getCmdAndArgs(ctx, proc, message) // Check for the use of env variable for CTRL_DATA, and set env if found. if foundEnvData { @@ -182,24 +166,7 @@ func methodCliCommandCont(proc process, message Message, node string) ([]byte, e go func() { defer proc.processes.wg.Done() - var cmd *exec.Cmd - - // For the Linux and Darwin operating system we allow to automatically detect - // shell interpreter, so the user don't have to type "bash", "-c" as the first - // two arguments of the methodArgs. - // We use the shell defined in the ShellOnNode variable as interpreter. Since - // it expects a "-c" directly after in the command we prefix it to the args. - if proc.configuration.ShellOnNode != "" { - switch runtime.GOOS { - case "linux", "darwin": - args := []string{"-c"} - args = append(args, message.MethodArgs...) - - cmd = exec.CommandContext(ctx, proc.configuration.ShellOnNode, args...) - default: - cmd = exec.CommandContext(ctx, message.MethodArgs[0], message.MethodArgs...) - } - } + cmd := getCmdAndArgs(ctx, proc, message) // Using cmd.StdoutPipe here so we are continuosly // able to read the out put of the command. @@ -277,3 +244,28 @@ func methodCliCommandCont(proc process, message Message, node string) ([]byte, e ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID)) return ackMsg, nil } + +// getCmdAndArgs will get the command and arguments from the message, and return +// a command to execute with the arguments. +func getCmdAndArgs(ctx context.Context, proc process, message Message) *exec.Cmd { + var cmd *exec.Cmd + + // For the Linux and Darwin operating system we allow to automatically detect + // shell interpreter, so the user don't have to type "bash", "-c" as the first + // two arguments of the methodArgs. + // We use the shell defined in the ShellOnNode variable as interpreter. Since + // it expects a "-c" directly after in the command we prefix it to the args. + if proc.configuration.ShellOnNode != "" { + switch runtime.GOOS { + case "linux", "darwin": + args := []string{"-c"} + args = append(args, message.MethodArgs...) + + cmd = exec.CommandContext(ctx, proc.configuration.ShellOnNode, args...) + default: + cmd = exec.CommandContext(ctx, message.MethodArgs[0], message.MethodArgs...) + } + } + + return cmd +}