Skip to content

Commit

Permalink
Use SOPS_EDITOR before EDITOR.
Browse files Browse the repository at this point in the history
Signed-off-by: Felix Fontein <[email protected]>
  • Loading branch information
felixfontein committed Sep 11, 2024
1 parent 50ad3e1 commit 53118e4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ encryption/decryption transparently and open the cleartext file in an editor
please wait while an encryption key is being generated and stored in a secure fashion
file written to mynewtestfile.yaml
Editing will happen in whatever ``$EDITOR`` is set to, or, if it's not set, in vim.
Editing will happen in whatever ``$SOPS_EDITOR`` or ``$EDITOR`` is set to, or, if it's
not set, in vim, nano, or vi.
Keep in mind that SOPS will wait for the editor to exit, and then try to reencrypt
the file. Some GUI editors (atom, sublime) spawn a child process and then exit
immediately. They usually have an option to wait for the main editor window to be
Expand Down
11 changes: 8 additions & 3 deletions cmd/sops/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,12 @@ func hashFile(filePath string) ([]byte, error) {
}

func runEditor(path string) error {
editor := os.Getenv("EDITOR")
envVar := "SOPS_EDITOR"
editor := os.Getenv(envVar)
if editor == "" {
envVar = "EDITOR"
editor = os.Getenv(envVar)
}
var cmd *exec.Cmd
if editor == "" {
editor, err := lookupAnyEditor("vim", "nano", "vi")
Expand All @@ -256,7 +261,7 @@ func runEditor(path string) error {
} else {
parts, err := shlex.Split(editor)
if err != nil {
return fmt.Errorf("invalid $EDITOR: %s", editor)
return fmt.Errorf("invalid $%s: %s", envVar, editor)
}
parts = append(parts, path)
cmd = exec.Command(parts[0], parts[1:]...)
Expand All @@ -275,5 +280,5 @@ func lookupAnyEditor(editorNames ...string) (editorPath string, err error) {
return editorPath, nil
}
}
return "", fmt.Errorf("no editor available: sops attempts to use the editor defined in the EDITOR environment variable, and if that's not set defaults to any of %s, but none of them could be found", strings.Join(editorNames, ", "))
return "", fmt.Errorf("no editor available: sops attempts to use the editor defined in the SOPS_EDITOR or EDITOR environment variables, and if that's not set defaults to any of %s, but none of them could be found", strings.Join(editorNames, ", "))
}
3 changes: 2 additions & 1 deletion cmd/sops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ func main() {
To use a different GPG binary than the one in your PATH, set SOPS_GPG_EXEC.
To select a different editor than the default (vim), set EDITOR.
To select a different editor than the default (vim), set SOPS_EDITOR or
EDITOR.
Note that flags must always be provided before the filename to operate on.
Otherwise, they will be ignored.
Expand Down

0 comments on commit 53118e4

Please sign in to comment.