-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): seed buckets locally from config file (#2460)
* feat(storage): create buckets locally from config file * feat: add command for seeding buckets * chore: add example config for buckets
- Loading branch information
1 parent
08780a5
commit 576f675
Showing
11 changed files
with
205 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"os/signal" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/supabase/cli/internal/seed/buckets" | ||
"github.com/supabase/cli/internal/utils" | ||
) | ||
|
||
var ( | ||
seedCmd = &cobra.Command{ | ||
GroupID: groupLocalDev, | ||
Use: "seed", | ||
Short: "Seed a Supabase project from " + utils.ConfigPath, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt) | ||
cmd.SetContext(ctx) | ||
return cmd.Root().PersistentPreRunE(cmd, args) | ||
}, | ||
} | ||
|
||
bucketsCmd = &cobra.Command{ | ||
Use: "buckets", | ||
Short: "Seed buckets declared in [storage.buckets]", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return buckets.Run(cmd.Context()) | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
seedFlags := seedCmd.PersistentFlags() | ||
seedFlags.Bool("linked", false, "Seeds the linked project.") | ||
seedFlags.Bool("local", true, "Seeds the local database.") | ||
seedCmd.MarkFlagsMutuallyExclusive("local", "linked") | ||
seedCmd.AddCommand(bucketsCmd) | ||
rootCmd.AddCommand(seedCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package buckets | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/supabase/cli/internal/storage/client" | ||
"github.com/supabase/cli/internal/utils" | ||
"github.com/supabase/cli/internal/utils/flags" | ||
"github.com/supabase/cli/pkg/storage" | ||
) | ||
|
||
func Run(ctx context.Context) error { | ||
api, err := client.NewStorageAPI(ctx, flags.ProjectRef) | ||
if err != nil { | ||
return err | ||
} | ||
buckets, err := api.ListBuckets(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
var exists []string | ||
console := utils.NewConsole() | ||
for _, b := range buckets { | ||
props := NewBucketProps(b.Name) | ||
if props == nil { | ||
continue | ||
} | ||
exists = append(exists, b.Name) | ||
label := fmt.Sprintf("Bucket %s already exists. Do you want to overwrite its properties?", utils.Bold(b.Id)) | ||
if shouldOverwrite, err := console.PromptYesNo(ctx, label, true); err != nil { | ||
return err | ||
} else if !shouldOverwrite { | ||
continue | ||
} | ||
body := storage.UpdateBucketRequest{ | ||
Id: b.Id, | ||
BucketProps: props, | ||
} | ||
if _, err := api.UpdateBucket(ctx, body); err != nil { | ||
return err | ||
} | ||
} | ||
for name := range utils.Config.Storage.Buckets { | ||
if utils.SliceContains(exists, name) { | ||
continue | ||
} | ||
msg := "Creating storage bucket:" | ||
if len(flags.ProjectRef) == 0 { | ||
msg = "Creating local storage bucket:" | ||
} | ||
fmt.Fprintln(os.Stderr, msg, name) | ||
body := storage.CreateBucketRequest{ | ||
Name: name, | ||
BucketProps: NewBucketProps(name), | ||
} | ||
if _, err := api.CreateBucket(ctx, body); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func NewBucketProps(name string) *storage.BucketProps { | ||
config, ok := utils.Config.Storage.Buckets[name] | ||
if !ok { | ||
return nil | ||
} | ||
props := storage.BucketProps{ | ||
Public: config.Public, | ||
AllowedMimeTypes: config.AllowedMimeTypes, | ||
} | ||
if config.FileSizeLimit > 0 { | ||
props.FileSizeLimit = int(config.FileSizeLimit) | ||
} else { | ||
props.FileSizeLimit = int(utils.Config.Storage.FileSizeLimit) | ||
} | ||
return &props | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters