-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.go
More file actions
211 lines (156 loc) · 4 KB
/
plugin.go
File metadata and controls
211 lines (156 loc) · 4 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"bytes"
"bufio"
"fmt"
"os"
"log"
"os/exec"
"regexp"
tea "github.com/charmbracelet/bubbletea"
)
func (m *model) writeState() string {
ct := m.CurrentTab
tmpdir := os.Getenv("TMPDIR")
t, err := os.CreateTemp(tmpdir, "BFM-STATE-")
if err != nil {
log.Fatal(err)
}
log.Printf("Created TMP File: %s", t.Name())
fwriteln(t, ct.absdir)
if len(m.selectedFiles) != 0 {
for _, sf := range(m.selectedFiles) {
fwriteln(t, fmt.Sprintf("%s/%s", sf.directory, sf.file.Name()))
}
} else {
if len(ct.filteredFiles) > 0 {
hovered := ct.filteredFiles[ct.cursor]
fwriteln(t, fmt.Sprintf("%s/%s", ct.absdir, hovered.Name()))
} else { // No files in current directory
fwriteln(t, "")
}
}
dst := m.CurrentTab.absdir
var paths []string
var errors []string
for _, sf := range(m.selectedFiles) {
if (sf.directory == m.CurrentTab.absdir) {
errors = append(errors, fmt.Sprintf("%s is already in %s", sf.file.Name(), dst))
} else {
src := fmt.Sprintf("%s/%s", sf.directory, sf.file.Name())
log.Printf("Moving %s to %s", src, dst)
paths = append(paths, src)
}
}
err = t.Close()
if err != nil {
log.Fatal(err)
}
return t.Name()
}
func (m *model) createCmd() string {
tmpdir := os.Getenv("TMPDIR")
t, err := os.CreateTemp(tmpdir, "BFM-CMD-")
if err != nil {
log.Fatal(err)
}
log.Printf("Created TMP File: %s", t.Name())
err = t.Close()
if err != nil {
log.Fatal(err)
}
return t.Name()
}
func (m *model) RunPlugin(pluginpath string, args ...string) tea.Cmd {
log.Printf("Running Plugin %s", pluginpath)
statepath := m.writeState()
cmdpath := m.createCmd()
args = append([]string{cmdpath}, args...) // $2
args = append([]string{statepath}, args...) // $1
var stdout bytes.Buffer
var stderr bytes.Buffer
c := exec.Command(pluginpath, args...) //nolint:gosec
// Assign stdout and stderr for the subprocess to the buffers
c.Stdout = &stdout
c.Stderr = &stderr
return tea.ExecProcess(c, func(err error) tea.Msg {
tea_cmds := m.runPluginCommands(cmdpath)
os.Remove(statepath)
os.Remove(cmdpath)
return runPluginFinishedMsg{
pluginpath,
tea_cmds,
err,
stdout,
stderr,
}
})
}
func (m *model) RunInteractivePlugin(pluginpath string, args ...string) tea.Cmd {
log.Printf("Running Interactive Plugin %s", pluginpath)
statepath := m.writeState()
cmdpath := m.createCmd()
args = append([]string{cmdpath}, args...) // $2
args = append([]string{statepath}, args...) // $1
c := exec.Command(pluginpath, args...) //nolint:gosec
return tea.ExecProcess(c, func(err error) tea.Msg {
tea_cmds := m.runPluginCommands(cmdpath)
os.Remove(statepath)
os.Remove(cmdpath)
var empty bytes.Buffer
return runPluginFinishedMsg{
pluginpath,
tea_cmds,
err,
empty,
empty,
}
})
}
func (m *model) toTeaCmd(cmd string) tea.Cmd {
log.Printf("Processing %s", cmd)
cdr := regexp.MustCompile("^cd (.*)")
captures := cdr.FindStringSubmatch(cmd)
if captures != nil {
return cd(captures[1])
}
selectr := regexp.MustCompile("^select (.*)")
captures = selectr.FindStringSubmatch(cmd)
if captures != nil {
return selectFile(captures[1])
}
showr := regexp.MustCompile("^error (.*)")
captures = showr.FindStringSubmatch(cmd)
if captures != nil {
return userError(captures[1])
}
if cmd == "refresh" {
return refresh()
}
if cmd == "deselect all" {
return deselectAll()
}
return nil
}
// Called by Update to runs commands in f written by plugin
func (m *model) runPluginCommands(f string) tea.Cmd {
file, err := os.Open(f)
if err != nil {
m.appendError("Error opening cmd file "+f+":"+err.Error())
return nil
}
defer file.Close()
teaCmds := []tea.Cmd{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
err = scanner.Err();
if err != nil {
m.appendError("Error reading cmd file "+f+":"+err.Error())
}
plugincmd := scanner.Text()
log.Printf("Running command: %s", plugincmd)
teaCmd := m.toTeaCmd(plugincmd)
teaCmds = append(teaCmds, teaCmd)
}
return tea.Sequence(teaCmds...)
}