Skip to content

Commit

Permalink
feat(editor): allow to pass a context (#204)
Browse files Browse the repository at this point in the history
* feat(editor): allow to pass a context

Signed-off-by: Carlos Alexandro Becker <[email protected]>

* fix: rename funcs

---------

Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 authored Oct 7, 2024
1 parent 8f76464 commit 7cc13b2
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion editor/editor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package editor

import (
"context"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -53,7 +54,20 @@ func EndOfLine() Option {

// Cmd returns a *exec.Cmd editing the given path with $EDITOR or nano if no
// $EDITOR is set.
// Deprecated: use Command or CommandContext instead.
func Cmd(app, path string, options ...Option) (*exec.Cmd, error) {
return CommandContext(context.Background(), app, path, options...)
}

// Command returns a *exec.Cmd editing the given path with $EDITOR or nano if
// no $EDITOR is set.
func Command(app, path string, options ...Option) (*exec.Cmd, error) {
return CommandContext(context.Background(), app, path, options...)
}

// CmdContext returns a *exec.Cmd editing the given path with $EDITOR or nano
// if no $EDITOR is set.
func CommandContext(ctx context.Context, app, path string, options ...Option) (*exec.Cmd, error) {
if os.Getenv("SNAP_REVISION") != "" {
return nil, fmt.Errorf("Did you install with Snap? %[1]s is sandboxed and unable to open an editor. Please install %[1]s with Go or another package manager to enable editing.", app)
}
Expand All @@ -73,7 +87,7 @@ func Cmd(app, path string, options ...Option) (*exec.Cmd, error) {
args = append(args, path)
}

return exec.Command(editor, args...), nil
return exec.CommandContext(ctx, editor, args...), nil
}

func getEditor() (string, []string) {
Expand Down

0 comments on commit 7cc13b2

Please sign in to comment.