review: code with hyphen

This commit is contained in:
Fernandez Ludovic 2026-01-30 21:45:19 +01:00
commit 70805aa60b
3 changed files with 18 additions and 10 deletions

View file

@ -9,24 +9,24 @@ import (
const (
{{- range . }}
// Code{{ .Code }} is the CA code for {{ .Name }}.
// Code{{ ToVarName .Code }} is the CA code for {{ .Name }}.
// {{ .DocumentationURL }}
Code{{ .Code }} = "{{ ToLower .Code }}"
Code{{ ToVarName .Code }} = "{{ ToLower .Code }}"
{{ end }})
const (
{{- range . }}
// DirectoryURL{{ .Code }} is the directory URL for {{ .Name }}.
// DirectoryURL{{ ToVarName .Code }} is the directory URL for {{ .Name }}.
// {{ .DocumentationURL }}
DirectoryURL{{ .Code }} = "{{ .DirectoryURL }}"
DirectoryURL{{ ToVarName .Code }} = "{{ .DirectoryURL }}"
{{ end }})
// GetDirectoryURL returns the directory URL for the given CA code.
func GetDirectoryURL(code string) (string, error) {
switch strings.ToLower(code) {
{{- range . }}
case Code{{ .Code }}:
return DirectoryURL{{ .Code }}, nil
case Code{{ ToVarName .Code }}:
return DirectoryURL{{ ToVarName .Code }}, nil
{{ end }}
default:
return "", fmt.Errorf("unknown ACME server code: %q", code)
@ -36,7 +36,7 @@ func GetDirectoryURL(code string) (string, error) {
func GetAllCodes() []string {
return []string{
{{- range . }}
Code{{ .Code }},
Code{{ ToVarName .Code }},
{{- end }}
}
}

View file

@ -37,7 +37,7 @@
{
"name": "Google Trust staging",
"type": "staging",
"code": "GoogleTrustStaging",
"code": "GoogleTrust-Staging",
"directoryURL": " https://dv.acme-v02.test-api.pki.goog/directory",
"documentationURL": "https://docs.cloud.google.com/certificate-manager/docs/public-ca-tutorial#register-acme"
},
@ -51,7 +51,7 @@
{
"name": "Let's Encrypt staging",
"type": "staging",
"code": "LetsEncryptStaging",
"code": "LetsEncrypt-Staging",
"directoryURL": "https://acme-staging-v02.api.letsencrypt.org/directory",
"documentationURL": "https://letsencrypt.org/docs/staging-environment/"
},

View file

@ -13,6 +13,7 @@ import (
"slices"
"strings"
"text/template"
"unicode"
)
const (
@ -70,7 +71,8 @@ func generate() error {
err = template.Must(
template.New("ca").Funcs(map[string]any{
"ToLower": strings.ToLower,
"ToLower": strings.ToLower,
"ToVarName": toVarName,
}).Parse(srcTemplate),
).Execute(b, entries)
if err != nil {
@ -90,3 +92,9 @@ func generate() error {
return nil
}
func toVarName(s string) string {
return strings.Join(strings.FieldsFunc(s, func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsDigit(r)
}), "")
}