Reworked command building.
Typical shell-supported command sets: - `cmd` - `cmd --arg` - `cmd --arg=value` - `ENV_VAR="value" cmd` - `ENV_VAR="value" cmd --arg` - `ENV_VAR="value" cmd --arg=value` The listed commands above are now better managed. Initially, the command types `cmd --arg=value` or `ENV_VAR="value" cmd --arg=value` were not correctly parsed. After finding a solution to this problem, I conducted further tests and discovered cases that were not currently handled. These cases are as follows: - `cmd "arg with space"` - `ENV_VAR="value with space" cmd`
This commit is contained in:
54
tools.go
54
tools.go
@@ -567,49 +567,51 @@ func launch(command string, terminal bool) {
|
|||||||
|
|
||||||
elements := strings.Split(command, " ")
|
elements := strings.Split(command, " ")
|
||||||
|
|
||||||
|
envVarsNum := 0
|
||||||
|
if strings.Contains(elements[0], "=") {
|
||||||
|
for _, element := range elements {
|
||||||
|
if strings.Contains(element, "=") {
|
||||||
|
envVarsNum++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// find prepended env variables, if any
|
// find prepended env variables, if any
|
||||||
envVarsNum := strings.Count(command, "=")
|
|
||||||
var envVars []string
|
var envVars []string
|
||||||
|
|
||||||
cmdIdx := -1
|
|
||||||
|
|
||||||
if envVarsNum > 0 {
|
|
||||||
for idx, item := range elements {
|
|
||||||
if strings.Contains(item, "=") {
|
|
||||||
envVars = append(envVars, item)
|
|
||||||
} else if !strings.HasPrefix(item, "-") && cmdIdx == -1 {
|
|
||||||
cmdIdx = idx
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if cmdIdx == -1 {
|
|
||||||
cmdIdx = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
if themeToPrepend != "" {
|
if themeToPrepend != "" {
|
||||||
envVars = append(envVars, fmt.Sprintf("GTK_THEME=%s", themeToPrepend))
|
envVars = append(envVars, fmt.Sprintf("GTK_THEME=%s", themeToPrepend))
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command(elements[cmdIdx], elements[1+cmdIdx:]...)
|
cmdIdx := envVarsNum
|
||||||
|
firstArgIdx := envVarsNum + 1
|
||||||
|
|
||||||
|
if envVarsNum > 0 {
|
||||||
|
for idx, item := range elements {
|
||||||
|
if envVarsNum > idx && strings.Contains(item, "=") {
|
||||||
|
envVars = append(envVars, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(elements[cmdIdx], elements[firstArgIdx:]...)
|
||||||
|
|
||||||
var prefixCommand string
|
var prefixCommand string
|
||||||
var args []string
|
var args []string
|
||||||
if terminal {
|
if terminal {
|
||||||
prefixCommand = *term
|
prefixCommand = *term
|
||||||
if *term != "foot" {
|
if *term != "foot" {
|
||||||
args = []string{"-e", elements[cmdIdx]}
|
args = []string{"-e", strings.Join(elements, " ")}
|
||||||
} else {
|
} else {
|
||||||
args = []string{elements[cmdIdx]}
|
args = elements[cmdIdx:]
|
||||||
}
|
}
|
||||||
cmd = exec.Command(prefixCommand, args...)
|
cmd = exec.Command(prefixCommand, args...)
|
||||||
} else if *wm == "sway" {
|
} else if *wm == "sway" {
|
||||||
prefixCommand = "swaymsg"
|
cmd = exec.Command("swaymsg", "exec", strings.Join(elements, " "))
|
||||||
args = []string{"exec", elements[cmdIdx]}
|
|
||||||
cmd = exec.Command(prefixCommand, args...)
|
|
||||||
} else if *wm == "hyprland" {
|
} else if *wm == "hyprland" {
|
||||||
prefixCommand = "hyprctl"
|
cmd = exec.Command("hyprctl", "dispatch", "exec", strings.Join(elements, " "))
|
||||||
args = []string{"dispatch", "exec", elements[cmdIdx]}
|
|
||||||
cmd = exec.Command(prefixCommand, args...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// set env variables
|
// set env variables
|
||||||
@@ -618,7 +620,7 @@ func launch(command string, terminal bool) {
|
|||||||
cmd.Env = append(cmd.Env, envVars...)
|
cmd.Env = append(cmd.Env, envVars...)
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := fmt.Sprintf("env vars: %s; command: '%s'; args: %s\n", envVars, cmd.Args[cmdIdx], cmd.Args[1+cmdIdx:])
|
msg := fmt.Sprintf("env vars: %s; command: '%s'; args: %s\n", envVars, cmd.Args[0], cmd.Args[1:])
|
||||||
log.Info(msg)
|
log.Info(msg)
|
||||||
|
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||||
|
|||||||
Reference in New Issue
Block a user