1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
package shared
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"regexp"
"strings"
)
var (
snakeRegexp = regexp.MustCompile("\\B[A-Z]+[^_$]")
parameterizeRegexp = regexp.MustCompile("[^A-Za-z0-9_]+")
)
// SnakeCase converts the given text to snakecase/underscore syntax.
func SnakeCase(text string) string {
result := snakeRegexp.ReplaceAllStringFunc(text, func(match string) string {
return "_" + match
})
return ParameterizeString(result)
}
// ParameterizeString parameterizes the given string.
func ParameterizeString(text string) string {
result := parameterizeRegexp.ReplaceAllString(text, "_")
return strings.ToLower(result)
}
// LoadCertificatesFrom returns certificates for a given pem file
func LoadCertificatesFrom(pemFile string) (*x509.CertPool, error) {
caCert, err := ioutil.ReadFile(pemFile)
if err != nil {
return nil, err
}
certificates := x509.NewCertPool()
certificates.AppendCertsFromPEM(caCert)
return certificates, nil
}
// LoadKeyPairFrom returns a configured TLS certificate
func LoadKeyPairFrom(pemFile string, privateKeyPemFile string) (tls.Certificate, error) {
targetPrivateKeyPemFile := privateKeyPemFile
if len(targetPrivateKeyPemFile) <= 0 {
targetPrivateKeyPemFile = pemFile
}
return tls.LoadX509KeyPair(pemFile, targetPrivateKeyPemFile)
}
|