diff --git a/cmd/exec.go b/cmd/exec.go new file mode 100644 index 0000000..9698f2e --- /dev/null +++ b/cmd/exec.go @@ -0,0 +1,35 @@ +package cmd + +import ( + "bufio" + "fmt" + "github.com/goplus/igop" + "io" + "os" +) + +func IgopExec(options CmdOptions, args []string) (int, error) { + var content []byte + + if options.ScriptIsSet { + content = []byte(options.Script) + } else { + stat, err := os.Stdin.Stat() + if err != nil || (stat.Mode()&os.ModeCharDevice) != 0 { + return -1, fmt.Errorf("must input a valid file or content, \"igop exec < 1.txt\"") + } + content, err = io.ReadAll(bufio.NewReader(os.Stdin)) + if err != nil { + return -2, err + } + } + + var mode = igop.EnablePrintAny + if options.Debug { + mode |= igop.EnableTracing | igop.EnableDumpImports | igop.EnableDumpInstr + } + + ctx := igop.NewContext(mode) + + return ctx.RunFile("main.gop", content, args) +} diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go deleted file mode 100644 index 3374cc7..0000000 --- a/cmd/exec/exec.go +++ /dev/null @@ -1,67 +0,0 @@ -package exec - -import ( - "bufio" - "fmt" - "github.com/goplus/igop" - "github.com/spf13/cobra" - "io" - "os" - - _ "gopkg.in/go-mixed/gos.v1/mod" -) - -type execCmdOptions struct { - debug bool - script string - scriptIsSet bool -} - -func ExecCmd() *cobra.Command { - var options = execCmdOptions{} - - execCmd := &cobra.Command{ - Use: "exec [-s | --script ] --