diff --git a/cmd/agedit/cli.go b/cmd/agedit/cli.go index f824913..607d5e5 100644 --- a/cmd/agedit/cli.go +++ b/cmd/agedit/cli.go @@ -66,6 +66,14 @@ var ( 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{ Name: "force", 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 } @@ -165,7 +176,8 @@ func action(ctx *cli.Context) error { } 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 { return err } diff --git a/cmd/agedit/main.go b/cmd/agedit/main.go index 6d47e79..6e6ef3b 100644 --- a/cmd/agedit/main.go +++ b/cmd/agedit/main.go @@ -16,6 +16,7 @@ var ( cfg config.Config configFile string + edt editor.Editor input_file, output_file string force_overwrite bool ) diff --git a/internal/config/config.go b/internal/config/config.go index 5b95c48..f87497c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,11 +1,13 @@ package config type Config struct { - IdentityFile string `json:"identityfile" yaml:"identityfile" toml:"identityfile"` - Editor string `json:"editor" yaml:"editor" toml:"editor"` - Prefix string `json:"randomfileprefix" yaml:"randomfileprefix" toml:"randomfileprefix"` - Suffix string `json:"randomfilesuffix" yaml:"randomfilesuffix" toml:"randomfilesuffix"` - RandomLength int `json:"randomfilenamelength" yaml:"randomfilenamelength" toml:"randomfilenamelength"` + IdentityFile string `json:"identityfile" yaml:"identityfile" toml:"identityfile"` + RecipientFile string `json:"recipientfile" yaml:"recipientfile" toml:"recipientfile"` + Editor string `json:"editor" yaml:"editor" toml:"editor"` + EditorArgs []string `json:"editorargs" yaml:"editorargs" toml:"editorargs"` + Prefix string `json:"randomfileprefix" yaml:"randomfileprefix" toml:"randomfileprefix"` + Suffix string `json:"randomfilesuffix" yaml:"randomfilesuffix" toml:"randomfilesuffix"` + RandomLength int `json:"randomfilenamelength" yaml:"randomfilenamelength" toml:"randomfilenamelength"` } var Defaults = Config{ diff --git a/pkg/editor/editor.go b/pkg/editor/editor.go index 51b9530..25edd19 100644 --- a/pkg/editor/editor.go +++ b/pkg/editor/editor.go @@ -1,7 +1,6 @@ package editor import ( - "errors" "io/fs" "os" "os/exec" @@ -9,14 +8,17 @@ import ( "git.burning.moe/celediel/agedit/pkg/tmpfile" ) -// EditFile opens the specified file in the configured editor -func EditFile(editor, filename string) error { - if editor == "" { - return errors.New("editor not set") - } +type Editor struct { + Command string + Args []string + generator tmpfile.Generator +} - // TODO: handle editors that require arguments - cmd := exec.Command(editor, filename) +// EditFile opens the specified file in the configured editor +func (e *Editor) EditFile(filename string) error { + args := append(e.Args, filename) + + cmd := exec.Command(e.Command, args...) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout 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 // 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 ( filename string bytes []byte @@ -38,10 +40,7 @@ func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([] file *os.File ) - // generator := tmpfile.NewGenerator("agedit_", ".txt", 13) - generator := tmpfile.NewGenerator(prefix, suffix, filename_length) - - filename = generator.GenerateFullPath() + filename = e.generator.GenerateFullPath() if file, err = os.Create(filename); err != nil { return nil, err } @@ -50,7 +49,7 @@ func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([] return nil, err } - if err = EditFile(editor, filename); err != nil { + if err = e.EditFile(filename); err != nil { return nil, err } @@ -68,3 +67,14 @@ func EditTempFile(editor, start, prefix, suffix string, filename_length int) ([] 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), + } +}