From 710003cafb57e75b6549bf66bc0280cef638c600 Mon Sep 17 00:00:00 2001 From: pupilcc Date: Wed, 10 Jan 2024 18:43:38 +0800 Subject: [PATCH] feat: issue output realtime --- middleware/acme.go | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/middleware/acme.go b/middleware/acme.go index 278fd99..37a5d0e 100644 --- a/middleware/acme.go +++ b/middleware/acme.go @@ -60,11 +60,24 @@ func Issue(name string) error { alias := os.Getenv("ACME_ALIAS") cmd := exec.Command(filepath.Join(usr.HomeDir, ".acme.sh/acme.sh"), "--issue", "--dns", dns, "-d", name, "--challenge-alias", alias, "--keylength", "2048") - output, err := cmd.CombinedOutput() + stdout, err := cmd.StdoutPipe() if err != nil { - logger.Error("Running command failed", zap.String("error:", err.Error())) + logger.Error("cmd.StdoutPipe() running command failed", zap.String("error:", err.Error())) + } + if err = cmd.Start(); err != nil { + logger.Error("cmd.Start() running command failed", zap.String("error:", err.Error())) + } + for { + tmp := make([]byte, 1024) + _, err := stdout.Read(tmp) + fmt.Print(string(tmp)) + if err != nil { + break + } + } + if err := cmd.Wait(); err != nil { + logger.Error("cmd.Wait() running command failed", zap.String("error:", err.Error())) } - fmt.Printf(string(output)) return err } @@ -73,10 +86,23 @@ func Install(name string, id string) error { cmd := exec.Command(filepath.Join(usr.HomeDir, ".acme.sh/acme.sh"), "--install-cert", "-d", name, "--key-file", filepath.Join(service.CertPath, id+".key"), "--fullchain-file", filepath.Join(service.CertPath, id+".crt")) logger.Info("command", zap.String("Running command:", strings.Join(cmd.Args, " "))) - output, err := cmd.CombinedOutput() + stdout, err := cmd.StdoutPipe() if err != nil { - logger.Error("Running command failed", zap.String("error:", err.Error())) + logger.Error("cmd.StdoutPipe() running command failed", zap.String("error:", err.Error())) + } + if err = cmd.Start(); err != nil { + logger.Error("cmd.Start() running command failed", zap.String("error:", err.Error())) + } + for { + tmp := make([]byte, 1024) + _, err := stdout.Read(tmp) + fmt.Print(string(tmp)) + if err != nil { + break + } + } + if err := cmd.Wait(); err != nil { + logger.Error("cmd.Wait() running command failed", zap.String("error:", err.Error())) } - fmt.Printf(string(output)) return err }