mirror of
https://github.com/FiloSottile/mkcert.git
synced 2025-10-14 00:41:40 +08:00
Cleanup path logics with pathExists and binaryExists
This commit is contained in:
4
cert.go
4
cert.go
@@ -245,7 +245,7 @@ func (m *mkcert) makeCertFromCSR() {
|
|||||||
|
|
||||||
// loadCA will load or create the CA at CAROOT.
|
// loadCA will load or create the CA at CAROOT.
|
||||||
func (m *mkcert) loadCA() {
|
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()
|
m.newCA()
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Using the local CA at \"%s\" ✨\n", m.CAROOT)
|
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)
|
m.caCert, err = x509.ParseCertificate(certDERBlock.Bytes)
|
||||||
fatalIfErr(err, "failed to parse the CA certificate")
|
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
|
return // keyless mode, where only -install works
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
main.go
11
main.go
@@ -14,6 +14,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"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)
|
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
|
||||||
|
}
|
||||||
|
@@ -39,19 +39,16 @@ func init() {
|
|||||||
hasJava = true
|
hasJava = true
|
||||||
javaHome = v
|
javaHome = v
|
||||||
|
|
||||||
_, err := os.Stat(filepath.Join(v, keytoolPath))
|
if pathExists(filepath.Join(v, keytoolPath)) {
|
||||||
if err == nil {
|
|
||||||
hasKeytool = true
|
hasKeytool = true
|
||||||
keytoolPath = filepath.Join(v, keytoolPath)
|
keytoolPath = filepath.Join(v, keytoolPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = os.Stat(filepath.Join(v, "lib", "security", "cacerts"))
|
if pathExists(filepath.Join(v, "lib", "security", "cacerts")) {
|
||||||
if err == nil {
|
|
||||||
cacertsPath = filepath.Join(v, "lib", "security", "cacerts")
|
cacertsPath = filepath.Join(v, "lib", "security", "cacerts")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = os.Stat(filepath.Join(v, "jre", "lib", "security", "cacerts"))
|
if pathExists(filepath.Join(v, "jre", "lib", "security", "cacerts")) {
|
||||||
if err == nil {
|
|
||||||
cacertsPath = filepath.Join(v, "jre", "lib", "security", "cacerts")
|
cacertsPath = filepath.Join(v, "jre", "lib", "security", "cacerts")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -46,24 +46,11 @@ func init() {
|
|||||||
SystemTrustFilename = "/usr/share/pki/trust/anchors/%s.pem"
|
SystemTrustFilename = "/usr/share/pki/trust/anchors/%s.pem"
|
||||||
SystemTrustCommand = []string{"update-ca-certificates"}
|
SystemTrustCommand = []string{"update-ca-certificates"}
|
||||||
}
|
}
|
||||||
if SystemTrustCommand != nil {
|
if SystemTrustCommand != nil && !binaryExists(SystemTrustCommand[0]) {
|
||||||
_, err := exec.LookPath(SystemTrustCommand[0])
|
SystemTrustCommand = nil
|
||||||
if err != nil {
|
|
||||||
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 {
|
func (m *mkcert) systemTrustFilename() string {
|
||||||
return fmt.Sprintf(SystemTrustFilename, strings.Replace(m.caUniqueName(), " ", "_", -1))
|
return fmt.Sprintf(SystemTrustFilename, strings.Replace(m.caUniqueName(), " ", "_", -1))
|
||||||
}
|
}
|
||||||
@@ -115,7 +102,7 @@ func (m *mkcert) uninstallPlatform() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CommandWithSudo(cmd ...string) *exec.Cmd {
|
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(cmd[0], cmd[1:]...)
|
||||||
}
|
}
|
||||||
return exec.Command("sudo", append([]string{"--"}, cmd...)...)
|
return exec.Command("sudo", append([]string{"--"}, cmd...)...)
|
||||||
|
@@ -23,29 +23,25 @@ func init() {
|
|||||||
"/Applications/Firefox Nightly.app",
|
"/Applications/Firefox Nightly.app",
|
||||||
"C:\\Program Files\\Mozilla Firefox",
|
"C:\\Program Files\\Mozilla Firefox",
|
||||||
} {
|
} {
|
||||||
_, err := os.Stat(path)
|
hasNSS = hasNSS || pathExists(path)
|
||||||
hasNSS = hasNSS || err == nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "darwin":
|
case "darwin":
|
||||||
var err error
|
if hasCertutil = binaryExists("certutil"); hasCertutil {
|
||||||
certutilPath, err = exec.LookPath("certutil")
|
certutilPath, _ = exec.LookPath("certutil")
|
||||||
if err != nil {
|
} else {
|
||||||
var out []byte
|
out, err := exec.Command("brew", "--prefix", "nss").Output()
|
||||||
out, err = exec.Command("brew", "--prefix", "nss").Output()
|
if err == nil {
|
||||||
if err != nil {
|
certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
|
||||||
return
|
hasCertutil = pathExists(certutilPath)
|
||||||
}
|
}
|
||||||
certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
|
|
||||||
_, err = os.Stat(certutilPath)
|
|
||||||
}
|
}
|
||||||
hasCertutil = err == nil
|
|
||||||
|
|
||||||
case "linux":
|
case "linux":
|
||||||
var err error
|
if hasCertutil = binaryExists("certutil"); hasCertutil {
|
||||||
certutilPath, err = exec.LookPath("certutil")
|
certutilPath, _ = exec.LookPath("certutil")
|
||||||
hasCertutil = err == nil
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +92,7 @@ func (m *mkcert) uninstallNSS() {
|
|||||||
|
|
||||||
func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
|
func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
|
||||||
profiles, _ := filepath.Glob(FirefoxProfile)
|
profiles, _ := filepath.Glob(FirefoxProfile)
|
||||||
if _, err := os.Stat(nssDB); err == nil {
|
if pathExists(nssDB) {
|
||||||
profiles = append(profiles, nssDB)
|
profiles = append(profiles, nssDB)
|
||||||
}
|
}
|
||||||
if len(profiles) == 0 {
|
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() {
|
if stat, err := os.Stat(profile); err != nil || !stat.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(filepath.Join(profile, "cert9.db")); err == nil {
|
if pathExists(filepath.Join(profile, "cert9.db")) {
|
||||||
f("sql:" + profile)
|
f("sql:" + profile)
|
||||||
found++
|
found++
|
||||||
continue
|
} else if pathExists(filepath.Join(profile, "cert8.db")) {
|
||||||
}
|
|
||||||
if _, err := os.Stat(filepath.Join(profile, "cert8.db")); err == nil {
|
|
||||||
f("dbm:" + profile)
|
f("dbm:" + profile)
|
||||||
found++
|
found++
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user