From 0d96ea9eefe99f38c568fb13af71dbe8cc160365 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Thu, 31 Oct 2024 19:59:21 +0800 Subject: [PATCH] refactor: deprecate `internal/deployer/deployer.getDeployVariables` --- internal/deployer/deployer.go | 22 ---------------------- internal/deployer/webhook.go | 2 +- internal/domain/domains.go | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/internal/deployer/deployer.go b/internal/deployer/deployer.go index 7be7b8ed..38ee209b 100644 --- a/internal/deployer/deployer.go +++ b/internal/deployer/deployer.go @@ -156,28 +156,6 @@ func toStr(tag string, data any) string { return tag + ":" + string(byts) } -func getDeployVariables(conf domain.DeployConfig) map[string]string { - rs := make(map[string]string) - data, ok := conf.Config["variables"] - if !ok { - return rs - } - - bts, _ := json.Marshal(data) - - kvData := make([]domain.KV, 0) - - if err := json.Unmarshal(bts, &kvData); err != nil { - return rs - } - - for _, kv := range kvData { - rs[kv.Key] = kv.Value - } - - return rs -} - func convertPEMToPFX(certificate string, privateKey string, password string) ([]byte, error) { cert, err := x509.ParseCertificateFromPEM(certificate) if err != nil { diff --git a/internal/deployer/webhook.go b/internal/deployer/webhook.go index 33b73f43..35c37235 100644 --- a/internal/deployer/webhook.go +++ b/internal/deployer/webhook.go @@ -50,7 +50,7 @@ func (d *WebhookDeployer) Deploy(ctx context.Context) error { Domain: d.option.Domain, Certificate: d.option.Certificate.Certificate, PrivateKey: d.option.Certificate.PrivateKey, - Variables: getDeployVariables(d.option.DeployConfig), + Variables: d.option.DeployConfig.GetConfigAsVariables(), } body, _ := json.Marshal(data) resp, err := xhttp.Req(access.Url, http.MethodPost, bytes.NewReader(body), map[string]string{ diff --git a/internal/domain/domains.go b/internal/domain/domains.go index bed38ac2..706315a9 100644 --- a/internal/domain/domains.go +++ b/internal/domain/domains.go @@ -1,6 +1,11 @@ package domain -import "strings" +import ( + "encoding/json" + "strings" + + "github.com/usual2970/certimate/internal/domain" +) type ApplyConfig struct { Email string `json:"email"` @@ -117,6 +122,33 @@ func (dc *DeployConfig) GetConfigOrDefaultAsBool(key string, defaultValue bool) return defaultValue } +// 以变量字典形式获取配置项。 +// +// 出参: +// - 变量字典。 +func (dc *DeployConfig) GetConfigAsVariables() map[string]string { + rs := make(map[string]string) + + if dc.Config != nil { + value, ok := dc.Config["variables"] + if !ok { + return rs + } + + kvs := make([]domain.KV, 0) + bts, _ := json.Marshal(value) + if err := json.Unmarshal(bts, &kvs); err != nil { + return rs + } + + for _, kv := range kvs { + rs[kv.Key] = kv.Value + } + } + + return rs +} + // GetDomain returns the domain from the deploy config // if the domain is a wildcard domain, and wildcard is true, return the wildcard domain func (dc *DeployConfig) GetDomain(wildcard ...bool) string {