1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-03-31 01:24:31 +00:00

added so if shell are defined via flag that will be used, and not the default shell of the OS.

This commit is contained in:
postmannen 2024-12-21 23:45:34 +01:00
parent c89983c489
commit 77ca78195d
2 changed files with 31 additions and 37 deletions

View file

@ -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()

View file

@ -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
}