From f62657edde00a4993186bfe0a98475a1b29f084b Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 5 Jul 2025 23:43:12 +0200 Subject: [PATCH] Added run command --- src/cli/Exec_test.go | 2 +- src/cli/run.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/cli/Exec_test.go b/src/cli/Exec_test.go index d21cd6d..eb78aab 100644 --- a/src/cli/Exec_test.go +++ b/src/cli/Exec_test.go @@ -11,6 +11,7 @@ func TestExec(t *testing.T) { assert.Equal(t, cli.Exec(nil), 2) assert.Equal(t, cli.Exec([]string{"_"}), 2) assert.Equal(t, cli.Exec([]string{"build"}), 1) + assert.Equal(t, cli.Exec([]string{"run"}), 1) assert.Equal(t, cli.Exec([]string{"build", "--invalid-parameter"}), 2) assert.Equal(t, cli.Exec([]string{"build", "../../examples/hello", "--invalid-parameter"}), 2) assert.Equal(t, cli.Exec([]string{"build", "../../examples/hello", "--dry"}), 0) @@ -21,5 +22,4 @@ func TestExec(t *testing.T) { assert.Equal(t, cli.Exec([]string{"build", "../../examples/hello", "--dry", "--arch", "x86"}), 0) assert.Equal(t, cli.Exec([]string{"build", "../../examples/hello/hello.q", "--dry"}), 0) assert.Equal(t, cli.Exec([]string{"help"}), 0) - assert.Equal(t, cli.Exec([]string{"run"}), 0) } \ No newline at end of file diff --git a/src/cli/run.go b/src/cli/run.go index 25d8377..e97ddff 100644 --- a/src/cli/run.go +++ b/src/cli/run.go @@ -1,6 +1,44 @@ package cli +import ( + "fmt" + "os" + "os/exec" + + "git.urbach.dev/cli/q/src/compiler" + "git.urbach.dev/cli/q/src/linker" +) + // run builds and runs the executable. func run(args []string) int { + b, err := newBuildFromArgs(args) + + if err != nil { + return exit(err) + } + + result, err := compiler.Compile(b) + + if err != nil { + return exit(err) + } + + err = linker.WriteFile(b.Executable(), b, result) + + if err != nil { + return exit(err) + } + + cmd := exec.Command(b.Executable()) + cmd.Stdout = os.Stdout + cmd.Stdin = os.Stdin + cmd.Stderr = os.Stderr + err = cmd.Run() + + if err != nil { + fmt.Fprintln(os.Stderr, err) + return exit(err) + } + return 0 } \ No newline at end of file