Teached the goscript interpreter to get output of commands.
This commit is contained in:
parent
f092240f7d
commit
cf95a7bac9
2 changed files with 36 additions and 0 deletions
|
@ -2,6 +2,8 @@ e = 5
|
||||||
v = 53
|
v = 53
|
||||||
println(e + v)
|
println(e + v)
|
||||||
|
|
||||||
|
println("cock", Cmd("ls").Stdout())
|
||||||
|
|
||||||
if v < 55 {
|
if v < 55 {
|
||||||
println("it fucking works")
|
println("it fucking works")
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,11 @@ import (
|
||||||
"github.com/surdeus/goscript/vm"
|
"github.com/surdeus/goscript/vm"
|
||||||
"github.com/surdeus/gomtool/src/mtool"
|
"github.com/surdeus/gomtool/src/mtool"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
//"bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Output []byte
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
input io.Reader
|
input io.Reader
|
||||||
*exec.Cmd
|
*exec.Cmd
|
||||||
|
@ -36,6 +39,10 @@ var (
|
||||||
//flag *mtool.Flags
|
//flag *mtool.Flags
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (o Output) String() string {
|
||||||
|
return string(o)
|
||||||
|
}
|
||||||
|
|
||||||
func Run(flagSet *mtool.Flags) {
|
func Run(flagSet *mtool.Flags) {
|
||||||
|
|
||||||
printVersion := flagSet.Bool("v", false, "prints out the version and then exits")
|
printVersion := flagSet.Bool("v", false, "prints out the version and then exits")
|
||||||
|
@ -81,6 +88,33 @@ func (cmd *Command)IO(input io.Reader, output io.Writer) *Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cmd *Command) Stdout() Output {
|
||||||
|
r, w := io.Pipe()
|
||||||
|
cmd = cmd.IO(os.Stdin, w)
|
||||||
|
|
||||||
|
ret := []byte{}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
buf := make([]byte, 512)
|
||||||
|
for {
|
||||||
|
n, err := r.Read(buf)
|
||||||
|
ret = append(ret, buf[:n]...)
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
} else if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Output(ret)
|
||||||
|
}
|
||||||
|
|
||||||
func (cmd *Command) Run() error {
|
func (cmd *Command) Run() error {
|
||||||
input := cmd.input
|
input := cmd.input
|
||||||
output := cmd.output
|
output := cmd.output
|
||||||
|
|
Loading…
Reference in a new issue