-
Notifications
You must be signed in to change notification settings - Fork 5
/
action.go
156 lines (135 loc) · 3.62 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"errors"
"os/exec"
"strings"
"github.com/abhinav/tmux-fastcopy/internal/fastcopy"
"github.com/abhinav/tmux-fastcopy/internal/log"
shellwords "github.com/mattn/go-shellwords"
"go.uber.org/multierr"
)
const (
_placeholderArg = "{}"
_regexNamesEnvKey = "FASTCOPY_REGEX_NAME"
_targetPaneEnvKey = "FASTCOPY_TARGET_PANE_ID"
)
func regexNamesEnvEntry(matchers []string) string {
return _regexNamesEnvKey + "=" + strings.Join(matchers, " ")
}
type actionFactory struct {
Log *log.Logger
Environ func() []string
Getwd func() (string, error)
}
type newActionRequest struct {
// Action is a multi-word shell command.
// It should use "{}" as an argument to reference the selected text.
// If no "{}" is present, the selection will be sent to the command
// over stdin.
Action string
// Dir is the working directory to run the command in.
//
// If empty, the current working directory is used.
Dir string
// TargetPaneID is the ID of the pane to send the output to.
TargetPaneID string
}
// New builds a command handler from the provided string.
//
// The string is a multi-word shell command. It should use "{}" as an argument
// to reference the selected text. If no "{}" is present, the selection will be
// sent to the command over stdin.
func (f *actionFactory) New(req newActionRequest) (action, error) {
args, err := shellwords.Parse(req.Action)
if err != nil {
return nil, err
}
if len(args) == 0 {
return nil, errors.New("empty action")
}
dir := req.Dir
if dir == "" {
dir, err = f.Getwd()
if err != nil {
// This should never happen, but if it does, use an
// empty string and let exec.Command() figure it out.
dir = ""
}
}
cmd, args := args[0], args[1:]
for i, arg := range args {
if arg == _placeholderArg {
return &argAction{
Cmd: cmd,
BeforeArgs: args[:i],
AfterArgs: args[i+1:],
Log: f.Log,
Environ: f.Environ,
Dir: dir,
PaneID: req.TargetPaneID,
}, nil
}
}
// No "{}" use stdin.
return &stdinAction{
Cmd: cmd,
Args: args,
Log: f.Log,
Environ: f.Environ,
Dir: dir,
PaneID: req.TargetPaneID,
}, nil
}
// action specifies how to handle the user's selection.
type action interface {
Run(fastcopy.Selection) error
}
type stdinAction struct {
Cmd string
Dir string
Args []string
Log *log.Logger
PaneID string
Environ func() []string // == os.Environ
}
func (h *stdinAction) Run(sel fastcopy.Selection) (err error) {
logw := &log.Writer{
Log: h.Log.WithName(h.Cmd),
}
defer multierr.AppendInvoke(&err, multierr.Close(logw))
cmd := exec.Command(h.Cmd, h.Args...)
cmd.Stdin = strings.NewReader(sel.Text)
cmd.Stdout = logw
cmd.Stderr = logw
cmd.Dir = h.Dir
cmd.Env = append(h.Environ(),
regexNamesEnvEntry(sel.Matchers),
_targetPaneEnvKey+"="+h.PaneID)
return cmd.Run()
}
type argAction struct {
Cmd string
Dir string
BeforeArgs, AfterArgs []string
Log *log.Logger
PaneID string
Environ func() []string // == os.Environ
}
func (h *argAction) Run(sel fastcopy.Selection) (err error) {
logw := &log.Writer{
Log: h.Log.WithName(h.Cmd),
}
defer multierr.AppendInvoke(&err, multierr.Close(logw))
args := make([]string, 0, len(h.BeforeArgs)+len(h.AfterArgs)+1)
args = append(args, h.BeforeArgs...)
args = append(args, sel.Text)
args = append(args, h.AfterArgs...)
cmd := exec.Command(h.Cmd, args...)
cmd.Stdout = logw
cmd.Stderr = logw
cmd.Dir = h.Dir
cmd.Env = append(h.Environ(),
regexNamesEnvEntry(sel.Matchers),
_targetPaneEnvKey+"="+h.PaneID)
return cmd.Run()
}