mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-23 01:41:31 +00:00
RPC refactor to separate core from core_client and the rest of RPC.
Other random changes.
This commit is contained in:
61
process/process.go
Normal file
61
process/process.go
Normal file
@ -0,0 +1,61 @@
|
||||
package process
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
func makeFile(prefix string) *os.File {
|
||||
now := time.Now()
|
||||
filename := fmt.Sprintf("%v_%v.out", prefix, now.Format("2006_01_02_15_04_05_MST"))
|
||||
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
type Process struct {
|
||||
Cmd *exec.Cmd
|
||||
Output *os.File
|
||||
}
|
||||
|
||||
const (
|
||||
ProcessModeStd = iota
|
||||
ProcessModeDaemon
|
||||
)
|
||||
|
||||
func CreateProcess(mode int, name string, args ...string) *Process {
|
||||
out := makeFile(name)
|
||||
cmd := exec.Command(name, args...)
|
||||
switch mode {
|
||||
case ProcessModeStd:
|
||||
cmd.Stdout = io.MultiWriter(os.Stdout, out)
|
||||
cmd.Stderr = io.MultiWriter(os.Stderr, out)
|
||||
cmd.Stdin = nil
|
||||
case ProcessModeDaemon:
|
||||
cmd.Stdout = out
|
||||
cmd.Stderr = out
|
||||
cmd.Stdin = nil
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
fmt.Printf("Failed to run command. %v\n", err)
|
||||
return nil
|
||||
} else {
|
||||
fmt.Printf("Success!")
|
||||
}
|
||||
return &Process{
|
||||
Cmd: cmd,
|
||||
Output: out,
|
||||
}
|
||||
}
|
||||
|
||||
func Watch(proc *Process) {
|
||||
exitErr := proc.Cmd.Wait()
|
||||
if exitErr != nil {
|
||||
fmt.Println("%v", exitErr)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user