This commit is contained in:
Andrey Parhomenko 2023-06-10 15:47:12 +03:00
parent 507953c348
commit 82d232c948
2 changed files with 57 additions and 54 deletions

View file

@ -6,5 +6,5 @@ if v < 55 {
println("it fucking works") println("it fucking works")
} }
rcmd("goblin", "ls", "-r", "100", "src") && println("it works also") Rcmd("goblin", "ls", "-r", "100", "src") || println("it works also") && Rcmd("goblin", "cat", "check.anko")

View file

@ -20,10 +20,10 @@ import (
"os/exec" "os/exec"
) )
type Cmd struct { type Command struct {
input io.ReadCloser input io.Reader
*exec.Cmd *exec.Cmd
output io.WriteCloser output io.Writer
} }
const version = "0.1.8" const version = "0.1.8"
@ -33,78 +33,71 @@ var (
file string file string
args []string args []string
e *env.Env e *env.Env
flag *mtool.Flags //flag *mtool.Flags
) )
func Run(flagSet *mtool.Flags) { func Run(flagSet *mtool.Flags) {
var exitCode int
flag = flagSet printVersion := flagSet.Bool("v", false, "prints out the version and then exits")
flagSet.StringVar(&flagExecute, "e", "", "execute the Anko code")
flagSet.Parse()
args := flagSet.Args()
parseFlags() if *printVersion {
setupEnv()
exitCode = runNonInteractive()
//if flagExecute != "" || flag.NArg() > 0 {
//}
os.Exit(exitCode)
}
func parseFlags() {
flagVersion := flag.Bool("v", false, "prints out the version and then exits")
flag.StringVar(&flagExecute, "e", "", "execute the Anko code")
flag.Parse()
if *flagVersion {
fmt.Println(version) fmt.Println(version)
os.Exit(0) os.Exit(0)
} }
if flagExecute != "" || flag.NArg() < 1 { setupEnv()
args = flag.Args() if flagExecute != "" {
return exitCode := runNonInteractive()
os.Exit(exitCode)
} }
file = flag.Arg(0) if len(args) < 1 {
args = flag.Args()[1:] file = "#stdin"
} else {
file = args[0]
}
exitCode := runNonInteractive()
os.Exit(exitCode)
} }
func setupEnv() { func Cmd(args ...string) *Command {
e = env.NewEnv()
cmd := func(args ...string) *Cmd {
if len(args) < 1 { if len(args) < 1 {
panic("too few arguments") panic("too few arguments")
} }
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...)
return &Cmd{ return &Command{ Cmd: cmd }
Cmd: cmd,
input: os.Stdin,
output: os.Stdout,
}
} }
e.Define("args", args)
e.Define("cmd", cmd)
e.Define("rcmd", func(args ...string) bool {
rcmd := cmd(args...)
input := rcmd.input func (cmd *Command)IO(input io.Reader, output io.Writer) *Command {
output := rcmd.output cmd.input = input
cmd.output = output
return cmd
}
stdin, err := rcmd.StdinPipe() func (cmd *Command) Run() error {
input := cmd.input
output := cmd.output
stdin, err := cmd.StdinPipe()
if err != nil { if err != nil {
panic(err) return err
} }
stdout, err := rcmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
panic(err) return err
} }
err = rcmd.Start() err = cmd.Start()
if err != nil { if err != nil {
panic(err) return err
} }
go func(){ go func(){
_, err = io.Copy(stdin, input) _, err = io.Copy(stdin, input)
@ -112,6 +105,7 @@ func setupEnv() {
panic(err) panic(err)
} }
}() }()
go func() { go func() {
_, err = io.Copy(output, stdout) _, err = io.Copy(output, stdout)
if err != nil { if err != nil {
@ -119,13 +113,22 @@ func setupEnv() {
} }
}() }()
err = rcmd.Wait() err = cmd.Wait()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return false return err
} }
return true return nil
}
func setupEnv() {
e = env.NewEnv()
e.Define("Args", args)
e.Define("Cmd", Cmd)
e.Define("Rcmd", func(args ...string) bool {
cmd := Cmd(args...).IO(os.Stdin, os.Stdout)
return cmd.Run() == nil
}) })
core.Import(e) core.Import(e)
} }