this repo has no description smallweb.run
smallweb
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

add a way to pass stdin to a smallweb run command

pomdtr c9d800f6 6bf8205e

+28 -13
+1 -1
cmd/crons.go
··· 155 155 } 156 156 wk := worker.NewWorker(a) 157 157 158 - command, err := wk.Command(context.Background(), job.Strings("args")...) 158 + command, err := wk.Command(context.Background(), job.Strings("args"), nil) 159 159 if err != nil { 160 160 fmt.Println(err) 161 161 continue
+13 -1
cmd/run.go
··· 2 2 3 3 import ( 4 4 "fmt" 5 + "io" 6 + "os" 5 7 8 + "github.com/mattn/go-isatty" 6 9 "github.com/pomdtr/smallweb/app" 7 10 "github.com/pomdtr/smallweb/worker" 8 11 "github.com/spf13/cobra" ··· 35 38 } 36 39 37 40 wk := worker.NewWorker(a) 38 - command, err := wk.Command(cmd.Context(), args[1:]...) 41 + var input []byte 42 + if !isatty.IsTerminal(os.Stdin.Fd()) { 43 + i, err := io.ReadAll(os.Stdin) 44 + if err != nil { 45 + return fmt.Errorf("failed to read from stdin: %w", err) 46 + } 47 + 48 + input = i 49 + } 50 + command, err := wk.Command(cmd.Context(), args[1:], input) 39 51 if err != nil { 40 52 return fmt.Errorf("failed to create command: %w", err) 41 53 }
+3 -3
cmd/up.go
··· 205 205 206 206 if flags.smtpAddr != "" { 207 207 fmt.Fprintf(cmd.ErrOrStderr(), "Starting smtp server on %s...\n", flags.smtpAddr) 208 - go smtpd.ListenAndServe(flags.smtpAddr, func(remoteAddr net.Addr, from string, to []string, msg []byte) error { 208 + go smtpd.ListenAndServe(flags.smtpAddr, func(remoteAddr net.Addr, from string, to []string, data []byte) error { 209 209 for _, recipient := range to { 210 210 parts := strings.Split(recipient, "@") 211 211 if len(parts) != 2 { ··· 223 223 } 224 224 225 225 worker := worker.NewWorker(a) 226 - if err := worker.SendEmail(context.Background(), msg); err != nil { 226 + if err := worker.SendEmail(context.Background(), data); err != nil { 227 227 return fmt.Errorf("failed to send email: %v", err) 228 228 } 229 229 } ··· 305 305 } 306 306 307 307 wk := worker.NewWorker(a) 308 - cmd, err := wk.Command(sess.Context(), sess.Command()...) 308 + cmd, err := wk.Command(sess.Context(), sess.Command(), nil) 309 309 if err != nil { 310 310 fmt.Fprintf(sess, "failed to get command: %v\n", err) 311 311 sess.Exit(1)
+3 -3
example/email/main.ts
··· 2 2 import PostalMime from "npm:postal-mime@2.4.3" 3 3 4 4 export default { 5 - async email(data: ReadableStream) { 5 + async email(msg: ReadableStream) { 6 6 await ensureDir("./data") 7 7 8 - const msg = await PostalMime.parse(data); 9 - await Deno.writeTextFile(`./data/email.json`, JSON.stringify(msg, null, 2)); 8 + const email = await PostalMime.parse(msg); 9 + await Deno.writeTextFile(`./data/email.json`, JSON.stringify(email, null, 2)); 10 10 } 11 11 }
+1 -1
go.mod
··· 31 31 github.com/getsops/sops/v3 v3.9.4 32 32 github.com/knadh/koanf/providers/posflag v0.1.0 33 33 github.com/leaanthony/gosod v1.0.4 34 + github.com/mhale/smtpd v0.8.3 34 35 github.com/pkg/sftp v1.13.9 35 36 github.com/robfig/cron/v3 v3.0.1 36 37 golang.org/x/crypto v0.36.0 ··· 138 139 github.com/mattn/go-colorable v0.1.14 // indirect 139 140 github.com/mattn/go-localereader v0.0.1 // indirect 140 141 github.com/mattn/go-runewidth v0.0.16 // indirect 141 - github.com/mhale/smtpd v0.8.3 // indirect 142 142 github.com/mholt/acmez/v3 v3.1.1 // indirect 143 143 github.com/miekg/dns v1.1.63 // indirect 144 144 github.com/mitchellh/copystructure v1.2.0 // indirect
+3 -1
worker/sandbox.ts
··· 160 160 Deno.exit(1); 161 161 } 162 162 163 - await handler.run(payload.args) 163 + const data = decodeBase64(payload.input) 164 + const blob = new Blob([data]); 165 + await handler.run(payload.args, blob.stream()); 164 166 } else if (payload.command === "email") { 165 167 const mod = await import(payload.entrypoint); 166 168 if (!mod.default || typeof mod.default !== "object") {
+4 -3
worker/worker.go
··· 461 461 return "", fmt.Errorf("deno executable not found") 462 462 } 463 463 464 - func (me *Worker) Command(ctx context.Context, args ...string) (*exec.Cmd, error) { 464 + func (me *Worker) Command(ctx context.Context, args []string, input []byte) (*exec.Cmd, error) { 465 465 if args == nil { 466 466 args = []string{} 467 467 } ··· 481 481 "command": "run", 482 482 "entrypoint": me.App.Entrypoint(), 483 483 "args": args, 484 + "input": base64.StdEncoding.EncodeToString(input), 484 485 }); err != nil { 485 486 return nil, fmt.Errorf("could not encode input: %w", err) 486 487 } ··· 496 497 return command, nil 497 498 } 498 499 499 - func (me *Worker) SendEmail(ctx context.Context, data []byte) error { 500 + func (me *Worker) SendEmail(ctx context.Context, msg []byte) error { 500 501 deno, err := DenoExecutable() 501 502 if err != nil { 502 503 return fmt.Errorf("could not find deno executable") ··· 511 512 if err := encoder.Encode(map[string]any{ 512 513 "command": "email", 513 514 "entrypoint": me.App.Entrypoint(), 514 - "msg": base64.StdEncoding.EncodeToString(data), 515 + "msg": base64.StdEncoding.EncodeToString(msg), 515 516 }); err != nil { 516 517 return fmt.Errorf("could not encode input: %w", err) 517 518 }