From bcf0420a4ebbbcb112b9bb515571d18f9f07dc10 Mon Sep 17 00:00:00 2001 From: Bobbie Soedirgo Date: Thu, 12 Jan 2023 21:55:41 +0800 Subject: [PATCH 1/3] feat: import_map function config --- internal/functions/deploy/deploy.go | 25 +++++++++++++++++-------- internal/functions/serve/serve.go | 8 ++++++++ internal/utils/config.go | 4 +++- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/internal/functions/deploy/deploy.go b/internal/functions/deploy/deploy.go index 1783862e23..28487fb43a 100644 --- a/internal/functions/deploy/deploy.go +++ b/internal/functions/deploy/deploy.go @@ -18,7 +18,10 @@ import ( "github.com/supabase/cli/pkg/api" ) -const EszipContentType = "application/vnd.denoland.eszip" +const ( + eszipContentType = "application/vnd.denoland.eszip" + supabaseDirPath = "supabase" +) func Run(ctx context.Context, slug string, projectRefArg string, noVerifyJWT *bool, useLegacyBundle bool, importMapPath string, fsys afero.Fs) error { // 1. Sanity checks. @@ -34,17 +37,23 @@ func Run(ctx context.Context, slug string, projectRefArg string, noVerifyJWT *bo } else if !utils.ProjectRefPattern.MatchString(projectRef) { return errors.New("Invalid project ref format. Must be like `abcdefghijklmnopqrst`.") } + // Load function config if any for fallbacks for some flags, but continue on error. + _ = utils.LoadConfigFS(fsys) // Ensure noVerifyJWT is not nil. if noVerifyJWT == nil { x := false - // Load function config if any, but continue on error. - if err := utils.LoadConfigFS(fsys); err == nil { - if functionConfig, ok := utils.Config.Functions[slug]; ok && !*functionConfig.VerifyJWT { - x = true - } + if functionConfig, ok := utils.Config.Functions[slug]; ok && !*functionConfig.VerifyJWT { + x = true } noVerifyJWT = &x } + if functionConfig, ok := utils.Config.Functions[slug]; ok && importMapPath == "" && functionConfig.ImportMap != "" { + if filepath.IsAbs(functionConfig.ImportMap) { + importMapPath = functionConfig.ImportMap + } else { + importMapPath = filepath.Join(supabaseDirPath, functionConfig.ImportMap) + } + } if err := utils.ValidateFunctionSlug(slug); err != nil { return err } @@ -146,7 +155,7 @@ func deployFunction(ctx context.Context, projectRef, slug string, functionBody i Name: &slug, VerifyJwt: &verifyJWT, ImportMap: &importMap, - }, EszipContentType, functionBody) + }, eszipContentType, functionBody) } if err != nil { return err @@ -167,7 +176,7 @@ func deployFunction(ctx context.Context, projectRef, slug string, functionBody i resp, err = utils.GetSupabase().UpdateFunctionWithBodyWithResponse(ctx, projectRef, slug, &api.UpdateFunctionParams{ VerifyJwt: &verifyJWT, ImportMap: &importMap, - }, EszipContentType, functionBody) + }, eszipContentType, functionBody) } if err != nil { return err diff --git a/internal/functions/serve/serve.go b/internal/functions/serve/serve.go index b2c2438aaf..78451af0dc 100644 --- a/internal/functions/serve/serve.go +++ b/internal/functions/serve/serve.go @@ -20,6 +20,7 @@ import ( const ( relayFuncDir = "/home/deno/functions" customDockerImportMapPath = "/home/deno/import_map.json" + supabaseDirPath = "supabase" ) func ParseEnvFile(envFilePath string) ([]string, error) { @@ -60,6 +61,13 @@ func Run(ctx context.Context, slug string, envFilePath string, noVerifyJWT *bool return fmt.Errorf("Failed to read env file: %w", err) } } + if functionConfig, ok := utils.Config.Functions[slug]; ok && importMapPath == "" && functionConfig.ImportMap != "" { + if filepath.IsAbs(functionConfig.ImportMap) { + importMapPath = functionConfig.ImportMap + } else { + importMapPath = filepath.Join(supabaseDirPath, functionConfig.ImportMap) + } + } if importMapPath != "" { if _, err := fsys.Stat(importMapPath); err != nil { return fmt.Errorf("Failed to read import map: %w", err) diff --git a/internal/utils/config.go b/internal/utils/config.go index eea9415e8b..a93de349b7 100644 --- a/internal/utils/config.go +++ b/internal/utils/config.go @@ -146,7 +146,8 @@ type ( } function struct { - VerifyJWT *bool `toml:"verify_jwt"` + VerifyJWT *bool `toml:"verify_jwt"` + ImportMap string `toml:"import_map"` } // TODO @@ -341,6 +342,7 @@ func LoadConfigFS(fsys afero.Fs) error { Config.Functions[name] = function{ VerifyJWT: verifyJWT, + ImportMap: functionConfig.ImportMap, } } From fe7246db9509d97f1d84638f8c8881e05ed8453b Mon Sep 17 00:00:00 2001 From: Bobbie Soedirgo Date: Fri, 13 Jan 2023 12:38:46 +0800 Subject: [PATCH 2/3] refactor: move supabaseDirPath to misc.go --- internal/functions/deploy/deploy.go | 7 ++----- internal/functions/serve/serve.go | 3 +-- internal/utils/misc.go | 19 ++++++++++--------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/internal/functions/deploy/deploy.go b/internal/functions/deploy/deploy.go index 28487fb43a..3a79d923e4 100644 --- a/internal/functions/deploy/deploy.go +++ b/internal/functions/deploy/deploy.go @@ -18,10 +18,7 @@ import ( "github.com/supabase/cli/pkg/api" ) -const ( - eszipContentType = "application/vnd.denoland.eszip" - supabaseDirPath = "supabase" -) +const eszipContentType = "application/vnd.denoland.eszip" func Run(ctx context.Context, slug string, projectRefArg string, noVerifyJWT *bool, useLegacyBundle bool, importMapPath string, fsys afero.Fs) error { // 1. Sanity checks. @@ -51,7 +48,7 @@ func Run(ctx context.Context, slug string, projectRefArg string, noVerifyJWT *bo if filepath.IsAbs(functionConfig.ImportMap) { importMapPath = functionConfig.ImportMap } else { - importMapPath = filepath.Join(supabaseDirPath, functionConfig.ImportMap) + importMapPath = filepath.Join(utils.SupabaseDirPath, functionConfig.ImportMap) } } if err := utils.ValidateFunctionSlug(slug); err != nil { diff --git a/internal/functions/serve/serve.go b/internal/functions/serve/serve.go index 78451af0dc..e9652f63fc 100644 --- a/internal/functions/serve/serve.go +++ b/internal/functions/serve/serve.go @@ -20,7 +20,6 @@ import ( const ( relayFuncDir = "/home/deno/functions" customDockerImportMapPath = "/home/deno/import_map.json" - supabaseDirPath = "supabase" ) func ParseEnvFile(envFilePath string) ([]string, error) { @@ -65,7 +64,7 @@ func Run(ctx context.Context, slug string, envFilePath string, noVerifyJWT *bool if filepath.IsAbs(functionConfig.ImportMap) { importMapPath = functionConfig.ImportMap } else { - importMapPath = filepath.Join(supabaseDirPath, functionConfig.ImportMap) + importMapPath = filepath.Join(utils.SupabaseDirPath, functionConfig.ImportMap) } } if importMapPath != "" { diff --git a/internal/utils/misc.go b/internal/utils/misc.go index 0e0c9e4afe..6d10e7e2be 100644 --- a/internal/utils/misc.go +++ b/internal/utils/misc.go @@ -82,15 +82,6 @@ DO 'BEGIN WHILE ( ServiceRoleKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU" JWTSecret = "super-secret-jwt-token-with-at-least-32-characters-long" AccessTokenKey = "access-token" - - ConfigPath = "supabase/config.toml" - ProjectRefPath = "supabase/.temp/project-ref" - RemoteDbPath = "supabase/.temp/remote-db-url" - CurrBranchPath = "supabase/.branches/_current_branch" - MigrationsDir = "supabase/migrations" - FunctionsDir = "supabase/functions" - DbTestsDir = "supabase/tests" - SeedDataPath = "supabase/seed.sql" ) var ( @@ -138,6 +129,16 @@ var ( "supabase_functions", "supabase_migrations", }, SystemSchemas...) + + SupabaseDirPath = "supabase" + ConfigPath = filepath.Join("supabase", "config.toml") + ProjectRefPath = filepath.Join("supabase", ".temp", "project-ref") + RemoteDbPath = filepath.Join("supabase", ".temp", "remote-db-url") + CurrBranchPath = filepath.Join("supabase", ".branches", "_current_branch") + MigrationsDir = filepath.Join("supabase", "migrations") + FunctionsDir = filepath.Join("supabase", "functions") + DbTestsDir = filepath.Join("supabase", "tests") + SeedDataPath = filepath.Join("supabase", "seed.sql") ) // Used by unit tests From dbab6ddc7bdb4937985be758da8da502ba12ee6c Mon Sep 17 00:00:00 2001 From: Han Qiao Date: Fri, 13 Jan 2023 12:50:58 +0800 Subject: [PATCH 3/3] chore: refactor path prefix --- internal/utils/misc.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/utils/misc.go b/internal/utils/misc.go index 6d10e7e2be..f79d131d66 100644 --- a/internal/utils/misc.go +++ b/internal/utils/misc.go @@ -131,14 +131,14 @@ var ( }, SystemSchemas...) SupabaseDirPath = "supabase" - ConfigPath = filepath.Join("supabase", "config.toml") - ProjectRefPath = filepath.Join("supabase", ".temp", "project-ref") - RemoteDbPath = filepath.Join("supabase", ".temp", "remote-db-url") - CurrBranchPath = filepath.Join("supabase", ".branches", "_current_branch") - MigrationsDir = filepath.Join("supabase", "migrations") - FunctionsDir = filepath.Join("supabase", "functions") - DbTestsDir = filepath.Join("supabase", "tests") - SeedDataPath = filepath.Join("supabase", "seed.sql") + ConfigPath = filepath.Join(SupabaseDirPath, "config.toml") + ProjectRefPath = filepath.Join(SupabaseDirPath, ".temp", "project-ref") + RemoteDbPath = filepath.Join(SupabaseDirPath, ".temp", "remote-db-url") + CurrBranchPath = filepath.Join(SupabaseDirPath, ".branches", "_current_branch") + MigrationsDir = filepath.Join(SupabaseDirPath, "migrations") + FunctionsDir = filepath.Join(SupabaseDirPath, "functions") + DbTestsDir = filepath.Join(SupabaseDirPath, "tests") + SeedDataPath = filepath.Join(SupabaseDirPath, "seed.sql") ) // Used by unit tests