Skip to content

Commit

Permalink
增加匹配逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Dec 18, 2024
1 parent 126a68a commit 6bd1f6e
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type CommandConfig struct {
Path string // Optional execution path. // 填写可选的执行路径。
ShellType string // Optional type of shell to use, e.g., bash, zsh. // 填写可选的 shell 类型,例如 bash,zsh。
ShellFlag string // Optional shell flag, e.g., "-c". // 填写可选的 Shell 参数,例如 "-c"。
DebugMode bool // enable debug mode. // 是否启用调试模式。
DebugMode bool // enable debug mode. // 是否启用调试模式,即打印调试的日志。
MatchPipe func(line string) bool
}

// NewCommandConfig creates and returns a new CommandConfig instance.
Expand Down Expand Up @@ -98,6 +99,11 @@ func (c *CommandConfig) WithDebugMode(debugMode bool) *CommandConfig {
return c
}

func (c *CommandConfig) WithMatchPipe(matchPipe func(line string) bool) *CommandConfig {
c.MatchPipe = matchPipe
return c
}

// Exec executes a shell command with the specified name and arguments, using the CommandConfig configuration.
// Exec 使用 CommandConfig 的配置执行带有指定名称和参数的 shell 命令。
func (c *CommandConfig) Exec(name string, args ...string) ([]byte, error) {
Expand Down Expand Up @@ -194,42 +200,57 @@ func (c *CommandConfig) ExecInPipe(name string, args ...string) ([]byte, error)

wg := sync.WaitGroup{}
wg.Add(2)
stderrBuffer := printgo.NewPTX()
var errMatch = false
var stderrBuffer = printgo.NewPTX()
go func() {
defer wg.Done()
c.readPipe(stderrReader, stderrBuffer, "REASON", eroticgo.RED)
errMatch = c.readPipe(stderrReader, stderrBuffer, "REASON", eroticgo.RED)
}()
stdoutBuffer := printgo.NewPTX()
var outMatch = false
var stdoutBuffer = printgo.NewPTX()
go func() {
defer wg.Done()
c.readPipe(stdoutReader, stdoutBuffer, "OUTPUT", eroticgo.GREEN)
outMatch = c.readPipe(stdoutReader, stdoutBuffer, "OUTPUT", eroticgo.GREEN)
}()
wg.Wait()

if outMatch {
return utils.WarpMessage(done.VAE(stdoutBuffer.Bytes(), nil), c.DebugMode)
}

if errMatch { //比如 "go: upgraded github.com/xx/xx vxx => vxx" 这就不算错误,而是正确的
return utils.WarpMessage(done.VAE(stderrBuffer.Bytes(), nil), c.DebugMode)
}

if stderrBuffer.Len() > 0 {
return utils.WarpMessage(done.VAE(stdoutBuffer.Bytes(), erero.New(stderrBuffer.String())), c.DebugMode)
} else {
return utils.WarpMessage(done.VAE(stdoutBuffer.Bytes(), nil), c.DebugMode)
}
}

func (c *CommandConfig) readPipe(reader *bufio.Reader, ptx *printgo.PTX, debugMessage string, erotic eroticgo.COLOR) {
func (c *CommandConfig) readPipe(reader *bufio.Reader, ptx *printgo.PTX, debugMessage string, erotic eroticgo.COLOR) (matched bool) {
for {
streamLine, _, err := reader.ReadLine()

if c.DebugMode {
zaplog.SUG.Debugln(debugMessage, erotic.Sprint(string(streamLine)))
}

if !matched && c.MatchPipe != nil {
matched = c.MatchPipe(string(streamLine))
}

if err != nil {
if err == io.EOF {
ptx.Write(streamLine)
return
return matched
}
panic(erero.Wro(err)) //panic: 读取结果出错很罕见
} else {
ptx.Write(streamLine)
ptx.Println()
}
}
return matched

Check failure on line 255 in command.go

View workflow job for this annotation

GitHub Actions / lint

unreachable: unreachable code (govet)

Check failure on line 255 in command.go

View workflow job for this annotation

GitHub Actions / lint

unreachable: unreachable code (govet)
}

0 comments on commit 6bd1f6e

Please sign in to comment.