From cf95a7bac960f97f296349d2a22217cc7b3fde27 Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 21 Jun 2023 10:49:46 +0300 Subject: [PATCH] Teached the goscript interpreter to get output of commands. --- check.anko | 2 ++ src/tool/script/main.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/check.anko b/check.anko index 42168fd..6598ad0 100644 --- a/check.anko +++ b/check.anko @@ -2,6 +2,8 @@ e = 5 v = 53 println(e + v) +println("cock", Cmd("ls").Stdout()) + if v < 55 { println("it fucking works") } diff --git a/src/tool/script/main.go b/src/tool/script/main.go index 7614c66..579435b 100644 --- a/src/tool/script/main.go +++ b/src/tool/script/main.go @@ -18,8 +18,11 @@ import ( "github.com/surdeus/goscript/vm" "github.com/surdeus/gomtool/src/mtool" "os/exec" + //"bytes" ) +type Output []byte + type Command struct { input io.Reader *exec.Cmd @@ -36,6 +39,10 @@ var ( //flag *mtool.Flags ) +func (o Output) String() string { + return string(o) +} + func Run(flagSet *mtool.Flags) { 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 } +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 { input := cmd.input output := cmd.output