magic/cmd/acme_sh/acme_sh.go
2026-03-28 19:25:12 +08:00

42 lines
1.4 KiB
Go

package acme_sh
import (
"context"
"fmt"
"os/exec"
"strings"
"gitea.micah.wiki/pandora/magic/config"
"gitea.micah.wiki/pandora/magic/pkg/env"
)
func Run(_ context.Context, cmdPath string, conf *config.Config, current *config.AcmeSh, isNew bool) error {
var currentCmd string
domains := strings.Split(current.Domain, ",")
domainValue := ""
for _, domain := range domains {
if strings.Contains(domain, "*") {
domainValue = fmt.Sprintf("%s -d \"%s\"", domainValue, domain)
} else {
domainValue = fmt.Sprintf("%s -d %s", domainValue, domain)
}
}
if isNew {
currentCmd = fmt.Sprintf("%s --issue %s --dns %s --key-file %s/key.pem --fullchain-file %s/cert.pem --yes-I-know-dns-manual-mode-enough-go-ahead-please --server letsencrypt",
cmdPath, domainValue, current.DNSType, current.KeyPath, current.KeyPath)
} else {
currentCmd = fmt.Sprintf("%s --renew %s --dns %s --key-file %s/key.pem --fullchain-file %s/cert.pem --yes-I-know-dns-manual-mode-enough-go-ahead-please --force --server letsencrypt",
cmdPath, domainValue, current.DNSType, current.KeyPath, current.KeyPath)
}
cmd := exec.Command(env.GetShellName(), "-c", fmt.Sprintf("source %s; %s", conf.ShellConfig, currentCmd))
output, err := cmd.Output()
// 如果返回是没有错误,则直接成功。
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("acme.sh 创建成功\n%s", string(output)))
return nil
}