-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically detect mkcert certificates
- Loading branch information
Bradley Kemp
committed
Jun 18, 2019
1 parent
0dbaa0d
commit 17c6b08
Showing
3 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package detectcert | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var ( | ||
keySuffix = "-key.pem" | ||
certSuffix = ".pem" | ||
) | ||
|
||
// Detect finds files in the current directory that look like | ||
// mkcert-generated key-certificate pairs | ||
func Detect() (cert string, key string, err error) { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
return "", "", err | ||
} | ||
files, err := ioutil.ReadDir(wd) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
for _, file := range files { | ||
if file.IsDir() { | ||
continue | ||
} | ||
|
||
if strings.HasSuffix(file.Name(), keySuffix) { | ||
keyName := file.Name() | ||
certName := strings.TrimSuffix(file.Name(), keySuffix) + certSuffix | ||
_, err := os.Stat(certName) | ||
if err == nil { | ||
// found a key and a cert that match the mkcert pattern | ||
return certName, keyName, nil | ||
} | ||
} | ||
} | ||
|
||
return "", "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters