Browse Source

Cleanup path logics with pathExists and binaryExists

Filippo Valsorda 5 years ago
parent
commit
245b2732c8
5 changed files with 33 additions and 44 deletions
  1. 2 2
      cert.go
  2. 11 0
      main.go
  3. 3 6
      truststore_java.go
  4. 3 16
      truststore_linux.go
  5. 14 20
      truststore_nss.go

+ 2 - 2
cert.go

@@ -245,7 +245,7 @@ func (m *mkcert) makeCertFromCSR() {
 
 // loadCA will load or create the CA at CAROOT.
 func (m *mkcert) loadCA() {
-	if _, err := os.Stat(filepath.Join(m.CAROOT, rootName)); os.IsNotExist(err) {
+	if !pathExists(filepath.Join(m.CAROOT, rootName)) {
 		m.newCA()
 	} else {
 		log.Printf("Using the local CA at \"%s\" ✨\n", m.CAROOT)
@@ -260,7 +260,7 @@ func (m *mkcert) loadCA() {
 	m.caCert, err = x509.ParseCertificate(certDERBlock.Bytes)
 	fatalIfErr(err, "failed to parse the CA certificate")
 
-	if _, err := os.Stat(filepath.Join(m.CAROOT, rootKeyName)); os.IsNotExist(err) {
+	if !pathExists(filepath.Join(m.CAROOT, rootKeyName)) {
 		return // keyless mode, where only -install works
 	}
 

+ 11 - 0
main.go

@@ -14,6 +14,7 @@ import (
 	"net"
 	"net/mail"
 	"os"
+	"os/exec"
 	"path/filepath"
 	"regexp"
 	"runtime"
@@ -330,3 +331,13 @@ func fatalIfCmdErr(err error, cmd string, out []byte) {
 		log.Fatalf("ERROR: failed to execute \"%s\": %s\n\n%s\n", cmd, err, out)
 	}
 }
+
+func pathExists(path string) bool {
+	_, err := os.Stat(path)
+	return err == nil
+}
+
+func binaryExists(name string) bool {
+	_, err := exec.LookPath(name)
+	return err == nil
+}

+ 3 - 6
truststore_java.go

@@ -39,19 +39,16 @@ func init() {
 		hasJava = true
 		javaHome = v
 
-		_, err := os.Stat(filepath.Join(v, keytoolPath))
-		if err == nil {
+		if pathExists(filepath.Join(v, keytoolPath)) {
 			hasKeytool = true
 			keytoolPath = filepath.Join(v, keytoolPath)
 		}
 
-		_, err = os.Stat(filepath.Join(v, "lib", "security", "cacerts"))
-		if err == nil {
+		if pathExists(filepath.Join(v, "lib", "security", "cacerts")) {
 			cacertsPath = filepath.Join(v, "lib", "security", "cacerts")
 		}
 
-		_, err = os.Stat(filepath.Join(v, "jre", "lib", "security", "cacerts"))
-		if err == nil {
+		if pathExists(filepath.Join(v, "jre", "lib", "security", "cacerts")) {
 			cacertsPath = filepath.Join(v, "jre", "lib", "security", "cacerts")
 		}
 	}

+ 3 - 16
truststore_linux.go

@@ -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...)...)

+ 14 - 20
truststore_nss.go

@@ -23,29 +23,25 @@ func init() {
 		"/Applications/Firefox Nightly.app",
 		"C:\\Program Files\\Mozilla Firefox",
 	} {
-		_, err := os.Stat(path)
-		hasNSS = hasNSS || err == nil
+		hasNSS = hasNSS || pathExists(path)
 	}
 
 	switch runtime.GOOS {
 	case "darwin":
-		var err error
-		certutilPath, err = exec.LookPath("certutil")
-		if err != nil {
-			var out []byte
-			out, err = exec.Command("brew", "--prefix", "nss").Output()
-			if err != nil {
-				return
+		if hasCertutil = binaryExists("certutil"); hasCertutil {
+			certutilPath, _ = exec.LookPath("certutil")
+		} else {
+			out, err := exec.Command("brew", "--prefix", "nss").Output()
+			if err == nil {
+				certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
+				hasCertutil = pathExists(certutilPath)
 			}
-			certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
-			_, err = os.Stat(certutilPath)
 		}
-		hasCertutil = err == nil
 
 	case "linux":
-		var err error
-		certutilPath, err = exec.LookPath("certutil")
-		hasCertutil = err == nil
+		if hasCertutil = binaryExists("certutil"); hasCertutil {
+			certutilPath, _ = exec.LookPath("certutil")
+		}
 	}
 }
 
@@ -96,7 +92,7 @@ func (m *mkcert) uninstallNSS() {
 
 func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
 	profiles, _ := filepath.Glob(FirefoxProfile)
-	if _, err := os.Stat(nssDB); err == nil {
+	if pathExists(nssDB) {
 		profiles = append(profiles, nssDB)
 	}
 	if len(profiles) == 0 {
@@ -106,12 +102,10 @@ func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
 		if stat, err := os.Stat(profile); err != nil || !stat.IsDir() {
 			continue
 		}
-		if _, err := os.Stat(filepath.Join(profile, "cert9.db")); err == nil {
+		if pathExists(filepath.Join(profile, "cert9.db")) {
 			f("sql:" + profile)
 			found++
-			continue
-		}
-		if _, err := os.Stat(filepath.Join(profile, "cert8.db")); err == nil {
+		} else if pathExists(filepath.Join(profile, "cert8.db")) {
 			f("dbm:" + profile)
 			found++
 		}