add support for editors that need arguments

This commit is contained in:
Lilian Jónsdóttir 2024-03-23 13:57:15 -07:00
parent e96bac3d9d
commit f4df6f35eb
4 changed files with 45 additions and 20 deletions

View file

@ -66,6 +66,14 @@ var (
return nil return nil
}, },
}, },
&cli.StringSliceFlag{
Name: "editor-args",
Usage: "`arg`uments to send to the editor",
Action: func(ctx *cli.Context, args []string) error {
cfg.EditorArgs = args
return nil
},
},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "force", Name: "force",
Usage: "Re-encrypt the file even if no changes have been made.", Usage: "Re-encrypt the file even if no changes have been made.",
@ -134,6 +142,9 @@ func before(ctx *cli.Context) error {
} }
} }
// setup editor with loaded config options
edt = editor.New(cfg.Editor, cfg.EditorArgs, cfg.Prefix, cfg.Suffix, cfg.RandomLength)
return nil return nil
} }
@ -165,7 +176,8 @@ func action(ctx *cli.Context) error {
} }
logger.Debug("decrypted " + input_file + " sucessfully") logger.Debug("decrypted " + input_file + " sucessfully")
edited, err := editor.EditTempFile(cfg.Editor, string(decrypted), cfg.Prefix, cfg.Suffix, cfg.RandomLength) // open decrypted data in the editor
edited, err := edt.EditTempFile(string(decrypted))
if err != nil { if err != nil {
return err return err
} }

View file

@ -16,6 +16,7 @@ var (
cfg config.Config cfg config.Config
configFile string configFile string
edt editor.Editor
input_file, output_file string input_file, output_file string
force_overwrite bool force_overwrite bool
) )

View file

@ -2,7 +2,9 @@ package config
type Config struct { type Config struct {
IdentityFile string `json:"identityfile" yaml:"identityfile" toml:"identityfile"` IdentityFile string `json:"identityfile" yaml:"identityfile" toml:"identityfile"`
RecipientFile string `json:"recipientfile" yaml:"recipientfile" toml:"recipientfile"`
Editor string `json:"editor" yaml:"editor" toml:"editor"` Editor string `json:"editor" yaml:"editor" toml:"editor"`
EditorArgs []string `json:"editorargs" yaml:"editorargs" toml:"editorargs"`
Prefix string `json:"randomfileprefix" yaml:"randomfileprefix" toml:"randomfileprefix"` Prefix string `json:"randomfileprefix" yaml:"randomfileprefix" toml:"randomfileprefix"`
Suffix string `json:"randomfilesuffix" yaml:"randomfilesuffix" toml:"randomfilesuffix"` Suffix string `json:"randomfilesuffix" yaml:"randomfilesuffix" toml:"randomfilesuffix"`
RandomLength int `json:"randomfilenamelength" yaml:"randomfilenamelength" toml:"randomfilenamelength"` RandomLength int `json:"randomfilenamelength" yaml:"randomfilenamelength" toml:"randomfilenamelength"`

View file

@ -1,7 +1,6 @@
package editor package editor
import ( import (
"errors"
"io/fs" "io/fs"
"os" "os"
"os/exec" "os/exec"
@ -9,14 +8,17 @@ import (
"git.burning.moe/celediel/agedit/pkg/tmpfile" "git.burning.moe/celediel/agedit/pkg/tmpfile"
) )
// EditFile opens the specified file in the configured editor type Editor struct {
func EditFile(editor, filename string) error { Command string
if editor == "" { Args []string
return errors.New("editor not set") generator tmpfile.Generator
} }
// TODO: handle editors that require arguments // EditFile opens the specified file in the configured editor
cmd := exec.Command(editor, filename) func (e *Editor) EditFile(filename string) error {
args := append(e.Args, filename)
cmd := exec.Command(e.Command, args...)
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -30,7 +32,7 @@ func EditFile(editor, filename string) error {
// EditTempFile creates a temporary file with a random name, opens it in the // EditTempFile creates a temporary file with a random name, opens it in the
// editor, and returns the byte slice of its contents. // editor, and returns the byte slice of its contents.
func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([]byte, error) { func (e *Editor) EditTempFile(start string) ([]byte, error) {
var ( var (
filename string filename string
bytes []byte bytes []byte
@ -38,10 +40,7 @@ func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([]
file *os.File file *os.File
) )
// generator := tmpfile.NewGenerator("agedit_", ".txt", 13) filename = e.generator.GenerateFullPath()
generator := tmpfile.NewGenerator(prefix, suffix, filename_length)
filename = generator.GenerateFullPath()
if file, err = os.Create(filename); err != nil { if file, err = os.Create(filename); err != nil {
return nil, err return nil, err
} }
@ -50,7 +49,7 @@ func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([]
return nil, err return nil, err
} }
if err = EditFile(editor, filename); err != nil { if err = e.EditFile(filename); err != nil {
return nil, err return nil, err
} }
@ -68,3 +67,14 @@ func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([]
return bytes, nil return bytes, nil
} }
// New returns an Editor configured to open files with `command` + `args`.
// The prefix and suffix will be added to the randomly generated
// filename of `length` characters.
func New(command string, args []string, prefix, suffix string, length int) Editor {
return Editor{
Command: command,
Args: args,
generator: tmpfile.NewGenerator(prefix, suffix, length),
}
}