Skip to content

Commit 85a03ca

Browse files
committed
fix(functions): use supabase temp folder to avoid permission error
1 parent e9b9c5d commit 85a03ca

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

internal/functions/deploy/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func bundleFunction(ctx context.Context, slug, dockerEntrypointPath, importMapPa
7878
return nil, errors.Errorf("failed to get working directory: %w", err)
7979
}
8080

81-
// create temp directory to store generated eszip
81+
// Create temp directory to store generated eszip
8282
hostOutputDir := filepath.Join(utils.TempDir, fmt.Sprintf(".output_%s", slug))
8383
// BitBucket pipelines require docker bind mounts to be world writable
8484
if err := fsys.MkdirAll(hostOutputDir, 0777); err != nil {

internal/functions/download/download.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,33 +138,38 @@ func Run(ctx context.Context, slug string, projectRef string, useLegacyBundle bo
138138

139139
func downloadOne(ctx context.Context, slug string, projectRef string, fsys afero.Fs) (string, error) {
140140
fmt.Println("Downloading " + utils.Bold(slug))
141-
resp, err := utils.GetSupabase().GetFunctionBodyWithResponse(ctx, projectRef, slug)
141+
resp, err := utils.GetSupabase().GetFunctionBody(ctx, projectRef, slug)
142142
if err != nil {
143143
return "", errors.Errorf("failed to get function body: %w", err)
144144
}
145-
if resp.StatusCode() != http.StatusOK {
146-
return "", errors.New("Unexpected error downloading Function: " + string(resp.Body))
145+
defer resp.Body.Close()
146+
if resp.StatusCode != http.StatusOK {
147+
body, err := io.ReadAll(resp.Body)
148+
if err != nil {
149+
return "", errors.Errorf("Error status %d: unexpected error downloading Function", resp.StatusCode)
150+
}
151+
return "", errors.Errorf("Error status %d: %s", resp.StatusCode, string(body))
147152
}
148-
149153
// Create temp file to store downloaded eszip
150-
eszipFile, err := afero.TempFile(fsys, "", slug)
151-
if err != nil {
152-
return "", errors.Errorf("failed to create temporary file: %w", err)
154+
eszipPath := filepath.Join(utils.TempDir, fmt.Sprintf("output_%s.eszip", slug))
155+
if err := utils.MkdirIfNotExistFS(fsys, utils.TempDir); err != nil {
156+
return "", err
153157
}
154-
defer eszipFile.Close()
155-
156-
body := bytes.NewReader(resp.Body)
157-
if _, err = io.Copy(eszipFile, body); err != nil {
158+
if err := afero.WriteReader(fsys, eszipPath, resp.Body); err != nil {
158159
return "", errors.Errorf("failed to download file: %w", err)
159160
}
160-
return eszipFile.Name(), nil
161+
return eszipPath, nil
161162
}
162163

163-
func extractOne(ctx context.Context, hostEszipPath string) error {
164+
func extractOne(ctx context.Context, eszipPath string) error {
164165
hostFuncDirPath, err := filepath.Abs(utils.FunctionsDir)
165166
if err != nil {
166167
return errors.Errorf("failed to resolve absolute path: %w", err)
167168
}
169+
hostEszipPath, err := filepath.Abs(eszipPath)
170+
if err != nil {
171+
return errors.Errorf("failed to resolve eszip path: %w", err)
172+
}
168173

169174
dockerEszipPath := path.Join(dockerEszipDir, filepath.Base(hostEszipPath))
170175
binds := []string{

0 commit comments

Comments
 (0)