poc of functions

This commit is contained in:
Sotig
2026-05-29 11:44:41 +03:00
parent 07cd9ebac9
commit 778380dc35
15 changed files with 924 additions and 56 deletions

View File

@@ -54,8 +54,9 @@ func execNoCheck(ctx context.Context, command string, args []string) (string, st
}
type Pipe struct {
Command string
Args []string
Command string
SuffixSymbol string
Args []string
}
func (p *Pipe) IsSanatory() bool {
@@ -70,6 +71,22 @@ func (p *Pipe) Execute(ctx context.Context) (string, string, error) {
}
}
// ExecuteCombined runs the command and returns combined stdout+stderr output.
// Useful when a tool writes useful output to either stream.
func (p *Pipe) ExecuteCombined(ctx context.Context) (string, string, error) {
if !p.IsSanatory() {
return "", "", errors.New("failed to execute cli command: " + p.Command + " " + strings.Join(p.Args, " ") + " some arguments are not allowed")
}
cmd := exec.CommandContext(ctx, p.Command, p.Args...)
cmdStr := cmd.String()
out, err := cmd.CombinedOutput()
if err != nil {
return "", cmdStr, errors.New(err.Error() + " | " + string(out))
}
return string(out), cmdStr, nil
}
func ExecuteCLIPipeLine(ctx context.Context, pipes []Pipe) (string, string, error) {
pipeLen := len(pipes)
@@ -94,7 +111,11 @@ func ExecuteCLIPipeLine(ctx context.Context, pipes []Pipe) (string, string, erro
for i, p := range pipes {
cmdArg += p.Command + " " + strings.Join(p.Args, " ")
if i != pipeLen {
cmdArg += " | "
if p.SuffixSymbol != "" {
cmdArg += " " + p.SuffixSymbol + " "
} else {
cmdArg += " | "
}
}
}