feat(cmd/joj3): log real time teapot stderr
This commit is contained in:
		
							parent
							
								
									9a57b701fd
								
							
						
					
					
						commit
						52296109cc
					
				|  | @ -1,6 +1,7 @@ | ||||||
| package teapot | package teapot | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"bufio" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"log/slog" | 	"log/slog" | ||||||
| 	"os" | 	"os" | ||||||
|  | @ -35,14 +36,35 @@ func Run(conf *conf.Conf) error { | ||||||
| 	re := regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`) | 	re := regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`) | ||||||
| 	execCommand := func(name string, cmdArgs []string) error { | 	execCommand := func(name string, cmdArgs []string) error { | ||||||
| 		cmd := exec.Command(name, cmdArgs...) // #nosec G204
 | 		cmd := exec.Command(name, cmdArgs...) // #nosec G204
 | ||||||
| 		outputBytes, err := cmd.CombinedOutput() | 		stderr, err := cmd.StderrPipe() | ||||||
| 		output := re.ReplaceAllString(string(outputBytes), "") | 		if err != nil { | ||||||
| 		for _, line := range strings.Split(output, "\n") { | 			slog.Error("stderr pipe", "error", err) | ||||||
| 			if line == "" { | 			return err | ||||||
|  | 		} | ||||||
|  | 		if err := cmd.Start(); err != nil { | ||||||
|  | 			slog.Error("cmd start", "error", err) | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 		var wg sync.WaitGroup | ||||||
|  | 		wg.Add(1) | ||||||
|  | 		scanner := bufio.NewScanner(stderr) | ||||||
|  | 		go func() { | ||||||
|  | 			for scanner.Scan() { | ||||||
|  | 				text := re.ReplaceAllString(scanner.Text(), "") | ||||||
|  | 				if text == "" { | ||||||
| 					continue | 					continue | ||||||
| 				} | 				} | ||||||
| 			slog.Info(fmt.Sprintf("%s %s", name, cmdArgs[0]), "output", line) | 				slog.Info(fmt.Sprintf("%s %s", name, cmdArgs[0]), "stderr", text) | ||||||
| 			} | 			} | ||||||
|  | 			wg.Done() | ||||||
|  | 			if scanner.Err() != nil { | ||||||
|  | 				slog.Error("stderr scanner", "error", scanner.Err()) | ||||||
|  | 			} | ||||||
|  | 		}() | ||||||
|  | 		if err = cmd.Start(); err != nil { | ||||||
|  | 			return err | ||||||
|  | 		} | ||||||
|  | 		wg.Wait() | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	var wg sync.WaitGroup | 	var wg sync.WaitGroup | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user