Skip to content

Commit

Permalink
feat: run multiple init programs
Browse files Browse the repository at this point in the history
- We log their outputs and status codes
- Windows is supported as well, leveraging cmd.exe /c

Co-authored-by: pancho horrillo <pancho.horrillo@bbva.com>
  • Loading branch information
nilp0inter and panchoh committed Dec 24, 2020
1 parent 989bf2e commit 0c16b54
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 18 deletions.
11 changes: 11 additions & 0 deletions internal/cmd/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build !windows

package cmd

import (
"os/exec"
)

func BuildCmd(path string) *exec.Cmd {
return exec.Command(path)
}
9 changes: 9 additions & 0 deletions internal/cmd/runner_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cmd

import (
"os/exec"
)

func BuildCmd(path string) *exec.Cmd {
return exec.Command("cmd.exe", "/c", path)
}
63 changes: 45 additions & 18 deletions internal/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package cmd

import (
"bufio"
"errors"
"io"
"os"
"os/exec"
"sync"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -57,23 +59,8 @@ var ServerCmd = &cobra.Command{

server.StartServer(sConf)

if len(args) > 0 {
powfile := args[0]
_, err := os.Stat(powfile)
if os.IsNotExist(err) {
logger.L.Fatalf("%s does not exist", powfile)
}
logger.L.Printf("Running powfile: %q\n", powfile)
kapowCMD := exec.Command("bash", powfile)
kapowCMD.Stdout = os.Stdout
kapowCMD.Stderr = os.Stderr
kapowCMD.Env = os.Environ()

err = kapowCMD.Run()
if err != nil {
logger.L.Fatal(err)
}
logger.L.Printf("Done running powfile: %q\n", powfile)
for _, path := range args {
go Run(path)
}

select {}
Expand Down Expand Up @@ -112,3 +99,43 @@ func validateServerCommandArguments(cmd *cobra.Command, args []string) error {

return nil
}

func Run(path string) {
logger.L.Printf("Running init program %+q", path)
cmd := BuildCmd(path)
cmd.Env = os.Environ()

var wg sync.WaitGroup
if stdout, err := cmd.StdoutPipe(); err == nil {
wg.Add(1)
go logPipe(path, "stdout", stdout, &wg)
}
if stderr, err := cmd.StderrPipe(); err == nil {
wg.Add(1)
go logPipe(path, "stderr", stderr, &wg)
}
err := cmd.Start()
if err != nil {
logger.L.Fatalf("Unable to run init program %+q: %s", path, err)
}

wg.Wait()
err = cmd.Wait()
if err != nil {
logger.L.Printf("Init program exited with error: %s", err)
} else {
logger.L.Printf("Init program %+q finished OK", path)
}
}

func logPipe(path, name string, pipe io.ReadCloser, wg *sync.WaitGroup) {
defer wg.Done()
in := bufio.NewScanner(pipe)

for in.Scan() {
logger.L.Printf("%+q (%s): %s", path, name, in.Text())
}
if err := in.Err(); err != nil {
logger.L.Printf("Error reading from %+q’s %s: %s", path, name, err)
}
}
19 changes: 19 additions & 0 deletions test/runwindowsrun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"os"
"os/exec"
)

func main() {
cmd := exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", os.Args[1])
err := cmd.Start()
if err != nil {
fmt.Println(err)
}
err = cmd.Wait()
if err != nil {
fmt.Println(err)
}
}

0 comments on commit 0c16b54

Please sign in to comment.