refactor: clean code

This commit is contained in:
Fu Diwei
2024-11-19 22:43:15 +08:00
parent a6c93ef9b8
commit 82807fcc1b
10 changed files with 127 additions and 111 deletions

View File

@@ -83,13 +83,13 @@ func (d *LocalDeployer) Deploy(ctx context.Context, certPem string, privkeyPem s
switch d.config.OutputFormat {
case "", OUTPUT_FORMAT_PEM:
if err := fs.WriteFileString(d.config.OutputCertPath, certPem); err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to save certificate file")
}
d.logger.Appendt("certificate file saved")
if err := fs.WriteFileString(d.config.OutputKeyPath, privkeyPem); err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to save private key file")
}
d.logger.Appendt("private key file saved")
@@ -97,13 +97,13 @@ func (d *LocalDeployer) Deploy(ctx context.Context, certPem string, privkeyPem s
case OUTPUT_FORMAT_PFX:
pfxData, err := x509.TransformCertificateFromPEMToPFX(certPem, privkeyPem, d.config.PfxPassword)
if err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to transform certificate to PFX")
}
d.logger.Appendt("certificate transformed to PFX")
if err := fs.WriteFile(d.config.OutputCertPath, pfxData); err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to save certificate file")
}
d.logger.Appendt("certificate file saved")
@@ -111,13 +111,13 @@ func (d *LocalDeployer) Deploy(ctx context.Context, certPem string, privkeyPem s
case OUTPUT_FORMAT_JKS:
jksData, err := x509.TransformCertificateFromPEMToJKS(certPem, privkeyPem, d.config.JksAlias, d.config.JksKeypass, d.config.JksStorepass)
if err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to transform certificate to JKS")
}
d.logger.Appendt("certificate transformed to JKS")
if err := fs.WriteFile(d.config.OutputCertPath, jksData); err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to save certificate file")
}
d.logger.Appendt("certificate file uploaded")

View File

@@ -91,7 +91,7 @@ func (d *SshDeployer) Deploy(ctx context.Context, certPem string, privkeyPem str
d.config.SshKeyPassphrase,
)
if err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to create ssh client")
}
defer client.Close()
@@ -111,13 +111,13 @@ func (d *SshDeployer) Deploy(ctx context.Context, certPem string, privkeyPem str
switch d.config.OutputFormat {
case "", OUTPUT_FORMAT_PEM:
if err := writeSftpFileString(client, d.config.OutputCertPath, certPem); err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to upload certificate file")
}
d.logger.Appendt("certificate file uploaded")
if err := writeSftpFileString(client, d.config.OutputKeyPath, privkeyPem); err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to upload private key file")
}
d.logger.Appendt("private key file uploaded")
@@ -125,13 +125,13 @@ func (d *SshDeployer) Deploy(ctx context.Context, certPem string, privkeyPem str
case OUTPUT_FORMAT_PFX:
pfxData, err := x509.TransformCertificateFromPEMToPFX(certPem, privkeyPem, d.config.PfxPassword)
if err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to transform certificate to PFX")
}
d.logger.Appendt("certificate transformed to PFX")
if err := writeSftpFile(client, d.config.OutputCertPath, pfxData); err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to upload certificate file")
}
d.logger.Appendt("certificate file uploaded")
@@ -139,13 +139,13 @@ func (d *SshDeployer) Deploy(ctx context.Context, certPem string, privkeyPem str
case OUTPUT_FORMAT_JKS:
jksData, err := x509.TransformCertificateFromPEMToJKS(certPem, privkeyPem, d.config.JksAlias, d.config.JksKeypass, d.config.JksStorepass)
if err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to transform certificate to JKS")
}
d.logger.Appendt("certificate transformed to JKS")
if err := writeSftpFile(client, d.config.OutputCertPath, jksData); err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to upload certificate file")
}
d.logger.Appendt("certificate file uploaded")
@@ -205,7 +205,7 @@ func createSshClient(host string, port int32, username string, password string,
func execSshCommand(sshCli *ssh.Client, command string) (string, string, error) {
session, err := sshCli.NewSession()
if err != nil {
return "", "", xerrors.Wrap(err, "failed to create ssh session")
return "", "", err
}
defer session.Close()
@@ -215,7 +215,7 @@ func execSshCommand(sshCli *ssh.Client, command string) (string, string, error)
session.Stderr = &stderrBuf
err = session.Run(command)
if err != nil {
return "", "", xerrors.Wrap(err, "failed to execute ssh command")
return "", "", err
}
return stdoutBuf.String(), stderrBuf.String(), nil

View File

@@ -58,7 +58,7 @@ type webhookData struct {
func (d *WebhookDeployer) Deploy(ctx context.Context, certPem string, privkeyPem string) (*deployer.DeployResult, error) {
certX509, err := x509.ParseCertificateFromPEM(certPem)
if err != nil {
return nil, err
return nil, xerrors.Wrap(err, "failed to parse x509")
}
data := &webhookData{