forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.go
80 lines (66 loc) · 2.18 KB
/
editor.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
package editor
import (
"bytes"
"context"
"io"
"io/ioutil"
"os"
"os/exec"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/out"
"github.com/gopasspw/gopass/pkg/tempfile"
"github.com/fatih/color"
shellquote "github.com/kballard/go-shellquote"
"github.com/pkg/errors"
)
var (
// Stdin is exported for tests
Stdin io.Reader = os.Stdin
// Stdout is exported for tests
Stdout io.Writer = os.Stdout
// Stderr is exported for tests
Stderr io.Writer = os.Stderr
)
// Invoke will start the given editor and return the content
func Invoke(ctx context.Context, editor string, content []byte) ([]byte, error) {
if !ctxutil.IsTerminal(ctx) {
return nil, errors.New("need terminal")
}
tmpfile, err := tempfile.New(ctx, "gopass-edit")
if err != nil {
return []byte{}, errors.Errorf("failed to create tmpfile %s: %s", editor, err)
}
defer func() {
if err := tmpfile.Remove(ctx); err != nil {
color.Red("Failed to remove tempfile at %s: %s", tmpfile.Name(), err)
}
}()
if _, err := tmpfile.Write(content); err != nil {
return []byte{}, errors.Errorf("failed to write tmpfile to start with %s %v: %s", editor, tmpfile.Name(), err)
}
if err := tmpfile.Close(); err != nil {
return []byte{}, errors.Errorf("failed to close tmpfile to start with %s %v: %s", editor, tmpfile.Name(), err)
}
cmdArgs, err := shellquote.Split(editor)
if err != nil {
return []byte{}, errors.Errorf("failed to parse EDITOR command `%s`", editor)
}
editor = cmdArgs[0]
args := append(cmdArgs[1:], tmpfile.Name())
cmd := exec.Command(editor, args...)
cmd.Stdin = Stdin
cmd.Stdout = Stdout
cmd.Stderr = Stderr
if err := cmd.Run(); err != nil {
out.Debug(ctx, "editor - cmd: %s %+v - error: %+v", cmd.Path, cmd.Args, err)
return []byte{}, errors.Errorf("failed to run %s with %s file: %s", editor, tmpfile.Name(), err)
}
nContent, err := ioutil.ReadFile(tmpfile.Name())
if err != nil {
return []byte{}, errors.Errorf("failed to read from tmpfile: %v", err)
}
// enforce unix line endings in the password store
nContent = bytes.Replace(nContent, []byte("\r\n"), []byte("\n"), -1)
nContent = bytes.Replace(nContent, []byte("\r"), []byte("\n"), -1)
return nContent, nil
}