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

48 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"
"gitea.micah.wiki/pandora/magic/pkg/loggerx"
)
// CheckAcmeSh 安装ache.sh 成功后返回安装路径,否则返回异常
func CheckAcmeSh(ctx context.Context, conf *config.Config, current *config.AcmeSh) (string, error) {
shellConf := conf.ShellConfig
shellName := env.GetShellName()
if len(shellConf) == 0 {
tempConf, err := env.GetShellConfig()
if err != nil {
return "", err
}
if len(tempConf) == 0 {
return "", fmt.Errorf("未找到sh配置文件地址")
}
shellConf = tempConf
conf.ShellConfig = tempConf
}
cmd := exec.Command(shellName, "-c", fmt.Sprintf("source %s; ls ~/.acme.sh/acme.sh", shellConf))
output, err := cmd.Output()
// 如果返回是没有错误,则直接成功。
if err == nil {
dir := strings.TrimSpace(string(output))
fmt.Println("acme.sh 路径为: " + dir)
loggerx.AcmeSh().Infof("acme.sh already installed. dir: %s", dir)
return dir, nil
}
cmd = exec.Command(shellName, "-c", fmt.Sprintf("source %s; curl https://get.acme.sh | sh -s email=%s", shellConf, current.DNSEmail))
output, err = cmd.Output()
// 如果返回是没有错误,则直接成功。
if err != nil {
return "", err
}
fmt.Println(fmt.Sprintf("acme.sh 安装成功\n%s", string(output)))
return CheckAcmeSh(ctx, conf, current)
}