Skip to content

Commit 80aaaac

Browse files
author
Jonas Falck
committed
Fix retry logic since exec.Cmd cannot be reused
1 parent 366dde9 commit 80aaaac

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

cmd_apply.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ func (cmd CmdApply) Usage() string {
2626
}
2727

2828
func ApplyRule(outputs Outputs, rule Rule) error {
29-
var cmds []*exec.Cmd
29+
var cmds commands
3030
var err error
3131

3232
switch {
3333
case rule.ConfigureSingle != "" || len(rule.ConfigureRow) > 0 || len(rule.ConfigureColumn) > 0:
3434
cmds, err = BuildCommandOutputRow(rule, outputs)
3535
case rule.ConfigureCommand != "":
36-
cmds = []*exec.Cmd{exec.Command("sh", "-c", rule.ConfigureCommand)}
36+
cmds = commands{command{"sh", "-c", rule.ConfigureCommand}}
3737
default:
3838
return fmt.Errorf("no output configuration for rule %v", rule.Name)
3939
}
@@ -45,7 +45,7 @@ func ApplyRule(outputs Outputs, rule Rule) error {
4545
foundError := false
4646
for _, cmd := range cmds {
4747
for i := 0; i < 4; i++ {
48-
err = RunCommand(cmd)
48+
err = RunCommand(cmd.Cmd())
4949
if err == nil {
5050
break
5151
}

randr.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,25 @@ func DetectOutputs() (Outputs, error) {
496496
return RandrParse(bytes.NewReader(output))
497497
}
498498

499+
type (
500+
commands []command
501+
command []string
502+
)
503+
504+
func (c command) Cmd() *exec.Cmd {
505+
return exec.Command(c[0], c[1:]...) // #nosec
506+
}
507+
508+
func newCommand(cmd string, args ...string) command {
509+
c := command{cmd}
510+
return append(c, args...)
511+
}
512+
499513
// BuildCommandOutputRow return a sequence of calls to `xrandr` to configure
500514
// all named outputs in a row, left to right, given the currently active
501515
// Outputs and a list of output names, optionally followed by "@" and the
502516
// desired mode, e.g. LVDS1@1377x768.
503-
func BuildCommandOutputRow(rule Rule, current Outputs) ([]*exec.Cmd, error) {
517+
func BuildCommandOutputRow(rule Rule, current Outputs) (commands, error) {
504518
var outputs []string
505519
var row bool
506520

@@ -606,18 +620,17 @@ func BuildCommandOutputRow(rule Rule, current Outputs) ([]*exec.Cmd, error) {
606620
for _, enableArgs := range enableOutputArgs {
607621
args = append(args, enableArgs...)
608622
}
609-
cmd := exec.Command(command, args...)
610-
return []*exec.Cmd{cmd}, nil
623+
return commands{newCommand(command, args...)}, nil
611624
}
612625

613626
V("splitting the configuration into several calls to xrandr\n")
614627

615628
// otherwise return several calls to xrandr
616-
cmds := []*exec.Cmd{}
629+
cmds := commands{}
617630

618631
// disable an output
619632
if len(disableOutputArgs) > 0 {
620-
cmds = append(cmds, exec.Command(command, disableOutputArgs[0]...))
633+
cmds = append(cmds, newCommand(command, disableOutputArgs[0]...))
621634
disableOutputArgs = disableOutputArgs[1:]
622635
}
623636

@@ -633,7 +646,7 @@ func BuildCommandOutputRow(rule Rule, current Outputs) ([]*exec.Cmd, error) {
633646
enableOutputArgs = enableOutputArgs[1:]
634647
}
635648

636-
cmds = append(cmds, exec.Command(command, args...))
649+
cmds = append(cmds, newCommand(command, args...))
637650
}
638651

639652
return cmds, nil

rule.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ type Rule struct {
2626
}
2727

2828
func (r Rule) OutputsDiff(old Rule) Outputs {
29-
outputs := []string{r.ConfigureSingle}
29+
outputs := []string{}
30+
if r.ConfigureSingle != "" {
31+
outputs = append(outputs, r.ConfigureSingle)
32+
}
3033
outputs = append(outputs, r.ConfigureRow...)
3134
outputs = append(outputs, r.ConfigureColumn...)
3235
for k, v := range outputs {
3336
outputs[k] = strings.SplitN(v, "@", 2)[0]
3437
}
3538

36-
outputsOld := []string{old.ConfigureSingle}
39+
outputsOld := []string{}
40+
if old.ConfigureSingle != "" {
41+
outputsOld = append(outputsOld, old.ConfigureSingle)
42+
}
3743
outputsOld = append(outputsOld, old.ConfigureRow...)
3844
outputsOld = append(outputsOld, old.ConfigureColumn...)
3945
for k, v := range outputsOld {

0 commit comments

Comments
 (0)