Skip to content

Commit a89f885

Browse files
authored
Merge pull request #1611 from felixfontein/editor
Use SOPS_EDITOR before EDITOR
2 parents fbc550f + 5b7c45f commit a89f885

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ encryption/decryption transparently and open the cleartext file in an editor
106106
please wait while an encryption key is being generated and stored in a secure fashion
107107
file written to mynewtestfile.yaml
108108
109-
Editing will happen in whatever ``$EDITOR`` is set to, or, if it's not set, in vim.
109+
Editing will happen in whatever ``$SOPS_EDITOR`` or ``$EDITOR`` is set to, or, if it's
110+
not set, in vim, nano, or vi.
110111
Keep in mind that SOPS will wait for the editor to exit, and then try to reencrypt
111112
the file. Some GUI editors (atom, sublime) spawn a child process and then exit
112113
immediately. They usually have an option to wait for the main editor window to be

cmd/sops/edit.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,12 @@ func hashFile(filePath string) ([]byte, error) {
245245
}
246246

247247
func runEditor(path string) error {
248-
editor := os.Getenv("EDITOR")
248+
envVar := "SOPS_EDITOR"
249+
editor := os.Getenv(envVar)
250+
if editor == "" {
251+
envVar = "EDITOR"
252+
editor = os.Getenv(envVar)
253+
}
249254
var cmd *exec.Cmd
250255
if editor == "" {
251256
editor, err := lookupAnyEditor("vim", "nano", "vi")
@@ -256,7 +261,7 @@ func runEditor(path string) error {
256261
} else {
257262
parts, err := shlex.Split(editor)
258263
if err != nil {
259-
return fmt.Errorf("invalid $EDITOR: %s", editor)
264+
return fmt.Errorf("invalid $%s: %s", envVar, editor)
260265
}
261266
parts = append(parts, path)
262267
cmd = exec.Command(parts[0], parts[1:]...)
@@ -275,5 +280,5 @@ func lookupAnyEditor(editorNames ...string) (editorPath string, err error) {
275280
return editorPath, nil
276281
}
277282
}
278-
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, ", "))
283+
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, ", "))
279284
}

cmd/sops/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ func main() {
137137
138138
To use a different GPG binary than the one in your PATH, set SOPS_GPG_EXEC.
139139
140-
To select a different editor than the default (vim), set EDITOR.
140+
To select a different editor than the default (vim), set SOPS_EDITOR or
141+
EDITOR.
141142
142143
Note that flags must always be provided before the filename to operate on.
143144
Otherwise, they will be ignored.

0 commit comments

Comments
 (0)