-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor runner; support Go snippets
- Loading branch information
Showing
11 changed files
with
212 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Examples | ||
|
||
## Shell | ||
|
||
This is a basic snippet with shell command: | ||
|
||
```sh | ||
$ echo "Hello, rdme!" | ||
``` | ||
|
||
It can contain multiple lines too: | ||
|
||
```sh | ||
$ echo "1" | ||
$ echo "2" | ||
$ echo "3" | ||
``` | ||
|
||
Also, the dollar sign is not needed: | ||
|
||
```sh | ||
echo "Hello, rdme! Again!" | ||
``` | ||
|
||
## Go | ||
|
||
It can also execute a snippet of Go code: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Hello from Go, rdme!") | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"io" | ||
) | ||
|
||
type Executable interface { | ||
Run(context.Context) error | ||
} | ||
|
||
type Base struct { | ||
Dir string | ||
Stdin io.Reader | ||
Stdout io.Writer | ||
Stderr io.Writer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type Go struct { | ||
Base | ||
Source string | ||
} | ||
|
||
func (g Go) Run(ctx context.Context) error { | ||
executable, err := exec.LookPath("go") | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to find %q executable", "go") | ||
} | ||
|
||
tmpDir, err := os.MkdirTemp("", "rdme-*") | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to create a temp dir") | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
mainFile := filepath.Join(tmpDir, "main.go") | ||
|
||
err = os.WriteFile(mainFile, []byte(g.Source), 0o600) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to write source to file") | ||
} | ||
|
||
c := exec.CommandContext(ctx, executable, "run", mainFile) | ||
c.Dir = g.Dir | ||
c.Stderr = g.Stderr | ||
c.Stdout = g.Stdout | ||
c.Stdin = g.Stdin | ||
|
||
return errors.Wrapf(c.Run(), "failed to run command %q", "go run main.go") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type Shell struct { | ||
Base | ||
Cmds []string | ||
} | ||
|
||
func (s Shell) Run(ctx context.Context) error { | ||
sh, ok := os.LookupEnv("SHELL") | ||
if !ok { | ||
sh = "/bin/sh" | ||
} | ||
|
||
for _, cmd := range s.Cmds { | ||
if err := execSingle(ctx, sh, s.Dir, cmd, s.Stdin, s.Stdout, s.Stderr); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func execSingle(ctx context.Context, sh, dir, cmd string, stdin io.Reader, stdout, stderr io.Writer) error { | ||
c := exec.CommandContext(ctx, sh, []string{"-c", cmd}...) | ||
c.Dir = dir | ||
c.Stderr = stderr | ||
c.Stdout = stdout | ||
c.Stdin = stdin | ||
|
||
return errors.Wrapf(c.Run(), "failed to run command %q", cmd) | ||
} |