Skip to content

Commit 061f3c5

Browse files
committed
fix arg order return nil on context cancel
1 parent 2753aa1 commit 061f3c5

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

orbit/pkg/execuser/execuser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func RunWithOutput(path string, opts ...Option) (output []byte, exitCode int, er
5252
}
5353

5454
// RunWithWait runs an application as the current login user and waits for it to finish
55-
// or to be canceled by the context.
55+
// or to be canceled by the context. Canceling the context will not return an error.
5656
// It assumes the caller is running with high privileges (root on UNIX).
5757
func RunWithWait(ctx context.Context, path string, opts ...Option) error {
5858
var o eopts

orbit/pkg/execuser/execuser_linux.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ func runWithOutput(path string, opts eopts) (output []byte, exitCode int, err er
5151
return nil, 0, fmt.Errorf("get args: %w", err)
5252
}
5353

54+
args = append(args, path)
55+
5456
if len(opts.args) > 0 {
5557
for _, arg := range opts.args {
5658
args = append(args, arg[0], arg[1])
5759
}
5860
}
5961

60-
args = append(args, path)
61-
6262
cmd := exec.Command("sudo", args...)
6363
log.Printf("cmd=%s", cmd.String())
6464

@@ -81,15 +81,24 @@ func runWithWait(ctx context.Context, path string, opts eopts) error {
8181

8282
args = append(args, path)
8383

84+
if len(opts.args) > 0 {
85+
for _, arg := range opts.args {
86+
args = append(args, arg[0], arg[1])
87+
}
88+
}
89+
8490
cmd := exec.CommandContext(ctx, "sudo", args...)
8591
log.Printf("cmd=%s", cmd.String())
8692

8793
if err := cmd.Start(); err != nil {
88-
return fmt.Errorf("cmd %q: %w", path, err)
94+
return fmt.Errorf("cmd start %q: %w", path, err)
8995
}
9096

9197
if err := cmd.Wait(); err != nil {
92-
return fmt.Errorf("cmd %q: %w", path, err)
98+
if errors.Is(ctx.Err(), context.Canceled) {
99+
return nil
100+
}
101+
return fmt.Errorf("cmd wait %q: %w", path, err)
93102
}
94103

95104
return nil

0 commit comments

Comments
 (0)