Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(codegen): Allow plugins to access environment variables
Currently untested, but environment variable access must be configured
in sqlc.yaml.
  • Loading branch information
kyleconroy committed Aug 29, 2023
commit 686da14eaf125675be2a9fc354522faea95229bd
2 changes: 2 additions & 0 deletions internal/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,13 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re
case plug.Process != nil:
handler = &process.Runner{
Cmd: plug.Process.Cmd,
Env: plug.Env,
}
case plug.WASM != nil:
handler = &wasm.Runner{
URL: plug.WASM.URL,
SHA256: plug.WASM.SHA256,
Env: plug.Env,
}
default:
return "", nil, fmt.Errorf("unsupported plugin type")
Expand Down
3 changes: 2 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ type Cloud struct {
}

type Plugin struct {
Name string `json:"name" yaml:"name"`
Name string `json:"name" yaml:"name"`
Env []string `json:"env" yaml:"env"`
Process *struct {
Cmd string `json:"cmd" yaml:"cmd"`
} `json:"process" yaml:"process"`
Expand Down
5 changes: 5 additions & 0 deletions internal/ext/process/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"os"
"os/exec"

"google.golang.org/protobuf/proto"
Expand All @@ -14,6 +15,7 @@ import (

type Runner struct {
Cmd string
Env []string
}

// TODO: Update the gen func signature to take a ctx
Expand All @@ -34,6 +36,9 @@ func (r Runner) Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plug
cmd.Env = []string{
fmt.Sprintf("SQLC_VERSION=%s", req.SqlcVersion),
}
for _, key := range r.Env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", key, os.Getenv(key)))
}

out, err := cmd.Output()
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions internal/ext/wasm/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func cacheDir() (string, error) {
type Runner struct {
URL string
SHA256 string
Env []string
}

var flight singleflight.Group
Expand Down Expand Up @@ -255,6 +256,14 @@ func (r *Runner) Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plu
wasiConfig.SetStdoutFile(stdoutPath)
wasiConfig.SetStderrFile(stderrPath)

keys := []string{"SQLC_VERSION"}
vals := []string{req.SqlcVersion}
for _, key := range r.Env {
keys = append(keys, key)
vals = append(vals, os.Getenv(key))
}
wasiConfig.SetEnv(keys, vals)

store := wasmtime.NewStore(engine)
store.SetWasi(wasiConfig)

Expand Down