go(utils): add command util for printing output with stdout pipe
This commit is contained in:
parent
8028a50cc3
commit
4a5b954ad6
15
go/commands/yarn.go
Normal file
15
go/commands/yarn.go
Normal 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
45
go/utils/command.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user