Skip to content

Commit

Permalink
feat: add enabled flag for functions under config.toml (#2688)
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete authored Sep 18, 2024
2 parents 9c1b27a + 3cc5a17 commit 7ec843d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
25 changes: 19 additions & 6 deletions internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

func Run(ctx context.Context, slugs []string, projectRef string, noVerifyJWT *bool, importMapPath string, fsys afero.Fs) error {
// Load function config and project id
var skippedFunctions []string
if err := utils.LoadConfigFS(fsys); err != nil {
return err
} else if len(slugs) > 0 {
Expand All @@ -24,9 +25,12 @@ func Run(ctx context.Context, slugs []string, projectRef string, noVerifyJWT *bo
return err
}
}
} else if slugs, err = GetFunctionSlugs(fsys); err != nil {
} else if slugs, skippedFunctions, err = GetFunctionSlugs(fsys); err != nil {
return err
}
if len(skippedFunctions) > 0 {
fmt.Fprintf(utils.GetDebugLogger(), "Skipped deploying the following functions: %s\n", strings.Join(skippedFunctions, ", "))
}
// TODO: require all functions to be deployed from config for v2
if len(slugs) == 0 {
return errors.Errorf("No Functions specified or found in %s", utils.Bold(utils.FunctionsDir))
Expand All @@ -45,20 +49,23 @@ func Run(ctx context.Context, slugs []string, projectRef string, noVerifyJWT *bo
return nil
}

func GetFunctionSlugs(fsys afero.Fs) ([]string, error) {
func GetFunctionSlugs(fsys afero.Fs) (slugs []string, disabledSlugs []string, err error) {
pattern := filepath.Join(utils.FunctionsDir, "*", "index.ts")
paths, err := afero.Glob(fsys, pattern)
if err != nil {
return nil, errors.Errorf("failed to glob function slugs: %w", err)
return nil, nil, errors.Errorf("failed to glob function slugs: %w", err)
}
var slugs []string
for _, path := range paths {
slug := filepath.Base(filepath.Dir(path))
if utils.FuncSlugPattern.MatchString(slug) {
slugs = append(slugs, slug)
if isFunctionEnabled(slug) {
slugs = append(slugs, slug)
} else {
disabledSlugs = append(disabledSlugs, slug)
}
}
}
return slugs, nil
return slugs, disabledSlugs, nil
}

func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool, fsys afero.Fs) (config.FunctionConfig, error) {
Expand Down Expand Up @@ -93,3 +100,9 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool,
}
return functionConfig, nil
}

func isFunctionEnabled(slug string) bool {
functionConfig := utils.Config.Functions[slug]
// If the function config Enabled is not defined, or defined and set to true
return functionConfig.Enabled == nil || *functionConfig.Enabled
}
52 changes: 52 additions & 0 deletions internal/functions/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,58 @@ import_map = "./import_map.json"
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("skip disabled functions from config", func(t *testing.T) {
t.Cleanup(func() { clear(utils.Config.Functions) })
// Setup in-memory fs
fsys := afero.NewMemMapFs()
require.NoError(t, utils.WriteConfig(fsys, false))
f, err := fsys.OpenFile(utils.ConfigPath, os.O_APPEND|os.O_WRONLY, 0600)
require.NoError(t, err)
_, err = f.WriteString(`
[functions.disabled-func]
enabled = false
import_map = "./import_map.json"
`)
require.NoError(t, err)
require.NoError(t, f.Close())
importMapPath, err := filepath.Abs(filepath.Join(utils.SupabaseDirPath, "import_map.json"))
require.NoError(t, err)
require.NoError(t, afero.WriteFile(fsys, importMapPath, []byte("{}"), 0644))
// Setup function entrypoints
require.NoError(t, afero.WriteFile(fsys, filepath.Join(utils.FunctionsDir, "enabled-func", "index.ts"), []byte{}, 0644))
require.NoError(t, afero.WriteFile(fsys, filepath.Join(utils.FunctionsDir, "disabled-func", "index.ts"), []byte{}, 0644))
// Setup valid project ref
project := apitest.RandomProjectRef()
// Setup valid access token
token := apitest.RandomAccessToken(t)
t.Setenv("SUPABASE_ACCESS_TOKEN", string(token))
// Setup valid deno path
_, err = fsys.Create(utils.DenoPathOverride)
require.NoError(t, err)
// Setup mock api
defer gock.OffAll()
gock.New(utils.DefaultApiHost).
Get("/v1/projects/" + project + "/functions").
Reply(http.StatusOK).
JSON([]api.FunctionResponse{})
gock.New(utils.DefaultApiHost).
Post("/v1/projects/"+project+"/functions").
MatchParam("slug", "enabled-func").
Reply(http.StatusCreated).
JSON(api.FunctionResponse{Id: "1"})
require.NoError(t, apitest.MockDocker(utils.Docker))
apitest.MockDockerStart(utils.Docker, imageUrl, containerId)
require.NoError(t, apitest.MockDockerLogs(utils.Docker, containerId, "bundled"))
// Setup output file
outputDir := filepath.Join(utils.TempDir, ".output_enabled-func")
require.NoError(t, afero.WriteFile(fsys, filepath.Join(outputDir, "output.eszip"), []byte(""), 0644))
// Run test
err = Run(context.Background(), nil, project, nil, "", fsys)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("throws error on malformed slug", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
Expand Down
5 changes: 4 additions & 1 deletion internal/functions/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,13 @@ func parseEnvFile(envFilePath string, fsys afero.Fs) ([]string, error) {
}

func populatePerFunctionConfigs(cwd, importMapPath string, noVerifyJWT *bool, fsys afero.Fs) ([]string, string, error) {
slugs, err := deploy.GetFunctionSlugs(fsys)
slugs, skippedFunctions, err := deploy.GetFunctionSlugs(fsys)
if err != nil {
return nil, "", err
}
if len(skippedFunctions) > 0 {
fmt.Fprintf(utils.GetDebugLogger(), "Skipped serving the following functions: %s\n", strings.Join(skippedFunctions, ", "))
}
functionsConfig, err := deploy.GetFunctionConfig(slugs, importMapPath, noVerifyJWT, fsys)
if err != nil {
return nil, "", err
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ type (
FunctionConfig map[string]function

function struct {
Enabled *bool `toml:"enabled"`
VerifyJWT *bool `toml:"verify_jwt" json:"verifyJWT"`
ImportMap string `toml:"import_map" json:"importMapPath,omitempty"`
Entrypoint string `json:"-"`
Expand Down

0 comments on commit 7ec843d

Please sign in to comment.