Skip to content

Commit

Permalink
Added exec redirect to temp file
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Dec 1, 2024
1 parent c1b8cc0 commit a241c49
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
11 changes: 6 additions & 5 deletions internal/app/action/result.go.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML">
<div class="divider text-lg text-secondary">Output File</div>

{{ range . }}
<h2>
<a class="link link-primary" href="{{ .url }}"> Download {{ .name }}</a>
</h2>
{{ end }}
<h2 class="text-center">
{{ range . }}
Download <a class="link link-primary" href="{{ .url }}">{{ .name }}</a>
<br />
{{ end }}
</h2>
</div>
{{ end }}

Expand Down
31 changes: 25 additions & 6 deletions plugins/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"fmt"
"io"
"os"
"os/exec"

"github.com/claceio/clace/internal/app"
Expand All @@ -34,12 +35,13 @@ func NewExecPlugin(_ *types.PluginContext) (any, error) {

func (e *ExecPlugin) Run(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var (
path starlark.String
cmdArgs *starlark.List
env *starlark.List
processPartial starlark.Bool
path starlark.String
cmdArgs *starlark.List
env *starlark.List
processPartial, stdoutToFile starlark.Bool
)
if err := starlark.UnpackArgs("run", args, kwargs, "path", &path, "args?", &cmdArgs, "env?", &env, "process_partial?", &processPartial); err != nil {
if err := starlark.UnpackArgs("run", args, kwargs, "path", &path, "args?", &cmdArgs, "env?", &env,
"process_partial?", &processPartial, "stdout_file", &stdoutToFile); err != nil {
return nil, err
}
if cmdArgs == nil {
Expand All @@ -53,6 +55,7 @@ func (e *ExecPlugin) Run(thread *starlark.Thread, _ *starlark.Builtin, args star
argsList := make([]string, 0, cmdArgs.Len())
envList := make([]string, 0, env.Len())
processPartialBool := bool(processPartial)
stdoutToFileBool := bool(stdoutToFile)

for i := 0; i < cmdArgs.Len(); i++ {
value, ok := cmdArgs.Index(i).(starlark.String)
Expand Down Expand Up @@ -84,7 +87,19 @@ func (e *ExecPlugin) Run(thread *starlark.Thread, _ *starlark.Builtin, args star
}

var buf bytes.Buffer
_, err = io.CopyN(&buf, stdout, MAX_BYTES_STDOUT)
var tempFile *os.File

if stdoutToFileBool {
tempFile, err = os.CreateTemp("", "clace-exec-stdout-*")
if err != nil {
return nil, fmt.Errorf("error creating temporary file: %w", err)
}
defer tempFile.Close()
_, err = io.Copy(tempFile, stdout)
} else {
_, err = io.CopyN(&buf, stdout, MAX_BYTES_STDOUT)
}

if err != nil && err != io.EOF {
return nil, err
}
Expand All @@ -97,6 +112,10 @@ func (e *ExecPlugin) Run(thread *starlark.Thread, _ *starlark.Builtin, args star
return nil, runErr
}

if stdoutToFileBool {
return app.NewResponse(starlark.String(tempFile.Name())), nil
}

lines := starlark.NewList([]starlark.Value{})
scanner := bufio.NewScanner(bytes.NewReader(buf.Bytes()))
for scanner.Scan() {
Expand Down

0 comments on commit a241c49

Please sign in to comment.