Specify a "Sudo password:" prompt for sudo

Fixes #178
Closes #185
This commit is contained in:
Filippo Valsorda
2019-08-16 17:47:28 -04:00
parent 2d05f3b4d8
commit aa4dd61066
4 changed files with 28 additions and 20 deletions

17
main.go
View File

@@ -16,10 +16,12 @@ import (
"net/url"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"golang.org/x/net/idna"
)
@@ -345,3 +347,18 @@ func binaryExists(name string) bool {
_, err := exec.LookPath(name)
return err == nil
}
var sudoWarningOnce sync.Once
func commandWithSudo(cmd ...string) *exec.Cmd {
if u, err := user.Current(); err == nil && u.Uid == "0" {
return exec.Command(cmd[0], cmd[1:]...)
}
if !binaryExists("sudo") {
sudoWarningOnce.Do(func() {
log.Println(`Warning: "sudo" is not available, and mkcert is not running as root. The (un)install operation might fail. ⚠️`)
})
return exec.Command(cmd[0], cmd[1:]...)
}
return exec.Command("sudo", append([]string{"--prompt=Sudo password:", "--"}, cmd...)...)
}