Skip to content

Commit

Permalink
feat(functions): allow passing env file to serve
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Mar 27, 2022
1 parent a1b8c6a commit cc1942b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
8 changes: 7 additions & 1 deletion cmd/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ var (
Short: "Serve a Function locally.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return serve.Run(args[0])
envFilePath, err := cmd.Flags().GetString("env-file")
if err != nil {
return err
}

return serve.Run(envFilePath, args[0])
},
}
)

func init() {
functionsServeCmd.Flags().String("env-file", "", "Path to an env file to be populated to the Function environment")
functionsCmd.AddCommand(functionsDeleteCmd)
functionsCmd.AddCommand(functionsDeployCmd)
functionsCmd.AddCommand(functionsNewCmd)
Expand Down
58 changes: 53 additions & 5 deletions internal/functions/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ package serve

import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"strconv"
"strings"
"syscall"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/pkg/stdcopy"
"github.com/joho/godotenv"
"github.com/supabase/cli/internal/utils"
)

var ctx = context.Background()

func Run(slug string) error {
func Run(envFilePath string, slug string) error {
// 1. Sanity checks.
{
if err := utils.AssertSupabaseCliIsSetUp(); err != nil {
Expand Down Expand Up @@ -100,13 +104,57 @@ func Run(slug string) error {

{
fmt.Println("Serving " + utils.Bold("supabase/functions/"+slug))
out, err := utils.DockerExec(ctx, utils.DenoRelayId, []string{
"deno", "run", "--allow-all", "--watch", "/home/deno/functions/" + slug + "/index.ts",
})

env := []string{
"SUPABASE_URL=http://localhost:" + strconv.FormatUint(uint64(utils.Config.Api.Port), 10),
"SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs",
"SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSJ9.vI9obAHOGyVVKa3pD--kJlyxp-Z2zV9UUMAhKpNLAcU",
"SUPABASE_DB_URL=postgresql://postgres:postgres@localhost:" + strconv.FormatUint(uint64(utils.Config.Db.Port), 10) + "/postgres",
}

if envFilePath == "" {
// skip
} else {
envMap, err := godotenv.Read(envFilePath)
if errors.Is(err, os.ErrNotExist) {
// skip
} else if err != nil {
return err
}
for name, value := range envMap {
if strings.HasPrefix(name, "SUPABASE_") {
return errors.New("Invalid secret name: " + name + ". Secret names cannot start with SUPABASE_.")
}
env = append(env, name+"="+value)
}
}

exec, err := utils.Docker.ContainerExecCreate(
ctx,
utils.DenoRelayId,
types.ExecConfig{
Env: env,
Cmd: []string{
"deno", "run", "--allow-all", "--watch", "/home/deno/functions/" + slug + "/index.ts",
},
AttachStderr: true,
AttachStdout: true,
},
)
if err != nil {
return err
}

resp, err := utils.Docker.ContainerExecAttach(ctx, exec.ID, types.ExecStartCheck{})
if err != nil {
return err
}
if _, err := stdcopy.StdCopy(os.Stdout, os.Stderr, out); err != nil {

if err := utils.Docker.ContainerExecStart(ctx, exec.ID, types.ExecStartCheck{}); err != nil {
return err
}

if _, err := stdcopy.StdCopy(os.Stdout, os.Stderr, resp.Reader); err != nil {
return err
}
}
Expand Down

0 comments on commit cc1942b

Please sign in to comment.