refactor: clean code

This commit is contained in:
Fu Diwei 2024-10-31 11:37:16 +08:00
parent 3c3d4e9109
commit 83264a6946
21 changed files with 31 additions and 51 deletions

View File

@ -59,7 +59,7 @@ func (d *AliyunALBDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *AliyunALBDeployer) GetInfo() []string { func (d *AliyunALBDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -46,7 +46,7 @@ func (d *AliyunCDNDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *AliyunCDNDeployer) GetInfo() []string { func (d *AliyunCDNDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -59,7 +59,7 @@ func (d *AliyunCLBDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *AliyunCLBDeployer) GetInfo() []string { func (d *AliyunCLBDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -47,7 +47,7 @@ func (d *AliyunDCDNDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *AliyunDCDNDeployer) GetInfo() []string { func (d *AliyunDCDNDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -59,7 +59,7 @@ func (d *AliyunNLBDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *AliyunNLBDeployer) GetInfo() []string { func (d *AliyunNLBDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -45,7 +45,7 @@ func (d *AliyunOSSDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *AliyunOSSDeployer) GetInfo() []string { func (d *AliyunOSSDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -52,7 +52,7 @@ type DeployerOption struct {
type Deployer interface { type Deployer interface {
Deploy(ctx context.Context) error Deploy(ctx context.Context) error
GetInfo() []string GetInfos() []string
GetID() string GetID() string
} }
@ -156,19 +156,6 @@ func toStr(tag string, data any) string {
return tag + "" + string(byts) return tag + "" + string(byts)
} }
func getDeployString(conf domain.DeployConfig, key string) string {
if _, ok := conf.Config[key]; !ok {
return ""
}
val, ok := conf.Config[key].(string)
if !ok {
return ""
}
return val
}
func getDeployVariables(conf domain.DeployConfig) map[string]string { func getDeployVariables(conf domain.DeployConfig) map[string]string {
rs := make(map[string]string) rs := make(map[string]string)
data, ok := conf.Config["variables"] data, ok := conf.Config["variables"]

View File

@ -62,7 +62,7 @@ func (d *HuaweiCloudCDNDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *HuaweiCloudCDNDeployer) GetInfo() []string { func (d *HuaweiCloudCDNDeployer) GetInfos() []string {
return d.infos return d.infos
} }
@ -74,7 +74,7 @@ func (d *HuaweiCloudCDNDeployer) Deploy(ctx context.Context) error {
} }
showDomainFullConfigResp, err := d.sdkClient.ShowDomainFullConfig(showDomainFullConfigReq) showDomainFullConfigResp, err := d.sdkClient.ShowDomainFullConfig(showDomainFullConfigReq)
if err != nil { if err != nil {
return err return xerrors.Wrap(err, "failed to execute sdk request 'cdn.ShowDomainFullConfig'")
} }
d.infos = append(d.infos, toStr("已查询到加速域名配置", showDomainFullConfigResp)) d.infos = append(d.infos, toStr("已查询到加速域名配置", showDomainFullConfigResp))
@ -111,7 +111,7 @@ func (d *HuaweiCloudCDNDeployer) Deploy(ctx context.Context) error {
} }
updateDomainMultiCertificatesResp, err := d.sdkClient.UploadDomainMultiCertificatesEx(updateDomainMultiCertificatesReq) updateDomainMultiCertificatesResp, err := d.sdkClient.UploadDomainMultiCertificatesEx(updateDomainMultiCertificatesReq)
if err != nil { if err != nil {
return err return xerrors.Wrap(err, "failed to execute sdk request 'cdn.UploadDomainMultiCertificatesEx'")
} }
d.infos = append(d.infos, toStr("已更新加速域名配置", updateDomainMultiCertificatesResp)) d.infos = append(d.infos, toStr("已更新加速域名配置", updateDomainMultiCertificatesResp))

View File

@ -67,7 +67,7 @@ func (d *HuaweiCloudELBDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *HuaweiCloudELBDeployer) GetInfo() []string { func (d *HuaweiCloudELBDeployer) GetInfos() []string {
return d.infos return d.infos
} }
@ -173,7 +173,7 @@ func (u *HuaweiCloudELBDeployer) getSdkProjectId(accessKeyId, secretAccessKey, r
if err != nil { if err != nil {
return "", err return "", err
} else if response.Projects == nil || len(*response.Projects) == 0 { } else if response.Projects == nil || len(*response.Projects) == 0 {
return "", fmt.Errorf("no project found") return "", errors.New("no project found")
} }
return (*response.Projects)[0].Id, nil return (*response.Projects)[0].Id, nil
@ -198,7 +198,7 @@ func (d *HuaweiCloudELBDeployer) deployToCertificate(ctx context.Context) error
} }
updateCertificateResp, err := d.sdkClient.UpdateCertificate(updateCertificateReq) updateCertificateResp, err := d.sdkClient.UpdateCertificate(updateCertificateReq)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute sdk request 'elb.UpdateCertificate': %w", err) return xerrors.Wrap(err, "failed to execute sdk request 'elb.UpdateCertificate'")
} }
d.infos = append(d.infos, toStr("已更新 ELB 证书", updateCertificateResp)) d.infos = append(d.infos, toStr("已更新 ELB 证书", updateCertificateResp))
@ -221,7 +221,7 @@ func (d *HuaweiCloudELBDeployer) deployToLoadbalancer(ctx context.Context) error
} }
showLoadBalancerResp, err := d.sdkClient.ShowLoadBalancer(showLoadBalancerReq) showLoadBalancerResp, err := d.sdkClient.ShowLoadBalancer(showLoadBalancerReq)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute sdk request 'elb.ShowLoadBalancer': %w", err) return xerrors.Wrap(err, "failed to execute sdk request 'elb.ShowLoadBalancer'")
} }
d.infos = append(d.infos, toStr("已查询到 ELB 负载均衡器", showLoadBalancerResp)) d.infos = append(d.infos, toStr("已查询到 ELB 负载均衡器", showLoadBalancerResp))
@ -239,7 +239,7 @@ func (d *HuaweiCloudELBDeployer) deployToLoadbalancer(ctx context.Context) error
} }
listListenersResp, err := d.sdkClient.ListListeners(listListenersReq) listListenersResp, err := d.sdkClient.ListListeners(listListenersReq)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute sdk request 'elb.ListListeners': %w", err) return xerrors.Wrap(err, "failed to execute sdk request 'elb.ListListeners'")
} }
if listListenersResp.Listeners != nil { if listListenersResp.Listeners != nil {
@ -309,7 +309,7 @@ func (d *HuaweiCloudELBDeployer) updateListenerCertificate(ctx context.Context,
} }
showListenerResp, err := d.sdkClient.ShowListener(showListenerReq) showListenerResp, err := d.sdkClient.ShowListener(showListenerReq)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute sdk request 'elb.ShowListener': %w", err) return xerrors.Wrap(err, "failed to execute sdk request 'elb.ShowListener'")
} }
d.infos = append(d.infos, toStr("已查询到 ELB 监听器", showListenerResp)) d.infos = append(d.infos, toStr("已查询到 ELB 监听器", showListenerResp))
@ -335,7 +335,7 @@ func (d *HuaweiCloudELBDeployer) updateListenerCertificate(ctx context.Context,
} }
listOldCertificateResp, err := d.sdkClient.ListCertificates(listOldCertificateReq) listOldCertificateResp, err := d.sdkClient.ListCertificates(listOldCertificateReq)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute sdk request 'elb.ListCertificates': %w", err) return xerrors.Wrap(err, "failed to execute sdk request 'elb.ListCertificates'")
} }
showNewCertificateReq := &hcElbModel.ShowCertificateRequest{ showNewCertificateReq := &hcElbModel.ShowCertificateRequest{
@ -343,7 +343,7 @@ func (d *HuaweiCloudELBDeployer) updateListenerCertificate(ctx context.Context,
} }
showNewCertificateResp, err := d.sdkClient.ShowCertificate(showNewCertificateReq) showNewCertificateResp, err := d.sdkClient.ShowCertificate(showNewCertificateReq)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute sdk request 'elb.ShowCertificate': %w", err) return xerrors.Wrap(err, "failed to execute sdk request 'elb.ShowCertificate'")
} }
for _, certificate := range *listOldCertificateResp.Certificates { for _, certificate := range *listOldCertificateResp.Certificates {
@ -376,7 +376,7 @@ func (d *HuaweiCloudELBDeployer) updateListenerCertificate(ctx context.Context,
} }
updateListenerResp, err := d.sdkClient.UpdateListener(updateListenerReq) updateListenerResp, err := d.sdkClient.UpdateListener(updateListenerReq)
if err != nil { if err != nil {
return fmt.Errorf("failed to execute sdk request 'elb.UpdateListener': %w", err) return xerrors.Wrap(err, "failed to execute sdk request 'elb.UpdateListener'")
} }
d.infos = append(d.infos, toStr("已更新 ELB 监听器", updateListenerResp)) d.infos = append(d.infos, toStr("已更新 ELB 监听器", updateListenerResp))

View File

@ -47,7 +47,7 @@ func (d *K8sSecretDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *K8sSecretDeployer) GetInfo() []string { func (d *K8sSecretDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -41,7 +41,7 @@ func (d *LocalDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *LocalDeployer) GetInfo() []string { func (d *LocalDeployer) GetInfos() []string {
return []string{} return []string{}
} }

View File

@ -55,7 +55,7 @@ func (d *QiniuCDNDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *QiniuCDNDeployer) GetInfo() []string { func (d *QiniuCDNDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -32,7 +32,7 @@ func (d *SSHDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *SSHDeployer) GetInfo() []string { func (d *SSHDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -64,7 +64,7 @@ func (d *TencentCDNDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *TencentCDNDeployer) GetInfo() []string { func (d *TencentCDNDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -62,7 +62,7 @@ func (d *TencentCLBDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *TencentCLBDeployer) GetInfo() []string { func (d *TencentCLBDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -57,7 +57,7 @@ func (d *TencentCOSDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *TencentCOSDeployer) GetInfo() []string { func (d *TencentCOSDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -63,7 +63,7 @@ func (d *TencentECDNDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *TencentECDNDeployer) GetInfo() []string { func (d *TencentECDNDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -63,7 +63,7 @@ func (d *TencentTEODeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *TencentTEODeployer) GetInfo() []string { func (d *TencentTEODeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -29,7 +29,7 @@ func (d *WebhookDeployer) GetID() string {
return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id) return fmt.Sprintf("%s-%s", d.option.AccessRecord.GetString("name"), d.option.AccessRecord.Id)
} }
func (d *WebhookDeployer) GetInfo() []string { func (d *WebhookDeployer) GetInfos() []string {
return d.infos return d.infos
} }

View File

@ -105,11 +105,11 @@ func deploy(ctx context.Context, record *models.Record) error {
if err = deployer.Deploy(ctx); err != nil { if err = deployer.Deploy(ctx); err != nil {
app.GetApp().Logger().Error("部署失败", "err", err) app.GetApp().Logger().Error("部署失败", "err", err)
history.record(deployPhase, "部署失败", &RecordInfo{Err: err, Info: deployer.GetInfo()}) history.record(deployPhase, "部署失败", &RecordInfo{Err: err, Info: deployer.GetInfos()})
return err return err
} }
history.record(deployPhase, fmt.Sprintf("[%s]-部署成功", deployer.GetID()), &RecordInfo{ history.record(deployPhase, fmt.Sprintf("[%s]-部署成功", deployer.GetID()), &RecordInfo{
Info: deployer.GetInfo(), Info: deployer.GetInfos(),
}, false) }, false)
} }

View File

@ -37,13 +37,6 @@ func NewTencentCloudSSLUploader(config *TencentCloudSSLUploaderConfig) (Uploader
} }
func (u *TencentCloudSSLUploader) Upload(ctx context.Context, certPem string, privkeyPem string) (res *UploadResult, err error) { func (u *TencentCloudSSLUploader) Upload(ctx context.Context, certPem string, privkeyPem string) (res *UploadResult, err error) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Recovered from panic: %+v", r)
fmt.Println()
}
}()
// 生成新证书名(需符合腾讯云命名规则) // 生成新证书名(需符合腾讯云命名规则)
var certId, certName string var certId, certName string
certName = fmt.Sprintf("certimate-%d", time.Now().UnixMilli()) certName = fmt.Sprintf("certimate-%d", time.Now().UnixMilli())