Fix save with sudo auto-detection and sudo/doas message

This commit is contained in:
Zachary Yedidia
2021-08-20 13:55:59 -04:00
parent 2eeeb0f2e4
commit dceddcfd83
2 changed files with 14 additions and 8 deletions

View File

@@ -1,6 +1,9 @@
package action package action
import ( import (
"errors"
"fmt"
"io/fs"
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
@@ -807,7 +810,7 @@ func (h *BufPane) SaveAs() bool {
func (h *BufPane) saveBufToFile(filename string, action string, callback func()) bool { func (h *BufPane) saveBufToFile(filename string, action string, callback func()) bool {
err := h.Buf.SaveAs(filename) err := h.Buf.SaveAs(filename)
if err != nil { if err != nil {
if strings.HasSuffix(err.Error(), "permission denied") { if errors.Is(err, fs.ErrPermission) {
saveWithSudo := func() { saveWithSudo := func() {
err = h.Buf.SaveAsWithSudo(filename) err = h.Buf.SaveAsWithSudo(filename)
if err != nil { if err != nil {
@@ -824,12 +827,15 @@ func (h *BufPane) saveBufToFile(filename string, action string, callback func())
if h.Buf.Settings["autosu"].(bool) { if h.Buf.Settings["autosu"].(bool) {
saveWithSudo() saveWithSudo()
} else { } else {
InfoBar.YNPrompt("Permission denied. Do you want to save this file using sudo? (y,n)", func(yes, canceled bool) { InfoBar.YNPrompt(
if yes && !canceled { fmt.Sprintf("Permission denied. Do you want to save this file using %s? (y,n)", config.GlobalSettings["sucmd"].(string)),
saveWithSudo() func(yes, canceled bool) {
h.completeAction(action) if yes && !canceled {
} saveWithSudo()
}) h.completeAction(action)
}
},
)
return false return false
} }
} else { } else {

View File

@@ -256,7 +256,7 @@ func NewBufferFromFileAtLoc(path string, btype BufType, cursorLoc Loc) (*Buffer,
} }
if readonly && prompt != nil { if readonly && prompt != nil {
prompt.Message("Warning: file is readonly - sudo will be attempted when saving") prompt.Message(fmt.Sprintf("Warning: file is readonly - %s will be attempted when saving", config.GlobalSettings["sucmd"].(string)))
// buf.SetOptionNative("readonly", true) // buf.SetOptionNative("readonly", true)
} }