go(utils): add command util for printing output with stdout pipe

This commit is contained in:
oscar 2022-08-21 21:22:41 +12:00
parent 8028a50cc3
commit 4a5b954ad6
2 changed files with 60 additions and 0 deletions

15
go/commands/yarn.go Normal file
View File

@ -0,0 +1,15 @@
package commands
import (
"ocl/portainer-devtool/utils"
)
// RunClient starts the portainer client
func RunPortainerClient(workdir string) error {
err := utils.RunCommandWithStdoutPipe(workdir, "yarn")
if err != nil {
return err
}
return utils.RunCommandWithStdoutPipe(workdir, "yarn", "start:client")
}

45
go/utils/command.go Normal file
View File

@ -0,0 +1,45 @@
package utils
import (
"bufio"
"fmt"
"os/exec"
)
func RunCommandWithStdoutPipe(workdir, progName string, args ...string) error {
cmd := exec.Command(progName, args...)
cmd.Dir = workdir
out, err := cmd.StdoutPipe()
if err != nil {
return err
}
scanner := bufio.NewScanner(out)
go func() {
counter := 0
for scanner.Scan() {
if counter > 10 {
// Swallow the output
if counter%50 == 0 {
fmt.Printf("output %d lines in total.\n", counter)
}
counter++
continue
}
PrintOutput("", scanner.Bytes())
counter++
}
}()
err = cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
return nil
}