Cleanup path logics with pathExists and binaryExists

This commit is contained in:
Filippo Valsorda
2019-06-01 14:55:58 +01:00
parent b1564cfb0d
commit 245b2732c8
5 changed files with 33 additions and 44 deletions

View File

@@ -46,24 +46,11 @@ func init() {
SystemTrustFilename = "/usr/share/pki/trust/anchors/%s.pem"
SystemTrustCommand = []string{"update-ca-certificates"}
}
if SystemTrustCommand != nil {
_, err := exec.LookPath(SystemTrustCommand[0])
if err != nil {
SystemTrustCommand = nil
}
if SystemTrustCommand != nil && !binaryExists(SystemTrustCommand[0]) {
SystemTrustCommand = nil
}
}
func pathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func binaryExists(name string) bool {
_, err := exec.LookPath(name)
return err == nil
}
func (m *mkcert) systemTrustFilename() string {
return fmt.Sprintf(SystemTrustFilename, strings.Replace(m.caUniqueName(), " ", "_", -1))
}
@@ -115,7 +102,7 @@ func (m *mkcert) uninstallPlatform() bool {
}
func CommandWithSudo(cmd ...string) *exec.Cmd {
if _, err := exec.LookPath("sudo"); err != nil {
if !binaryExists("sudo") {
return exec.Command(cmd[0], cmd[1:]...)
}
return exec.Command("sudo", append([]string{"--"}, cmd...)...)