Skip to content

Commit

Permalink
chore: use global project ref variable for reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Dec 18, 2023
1 parent f15b606 commit 09fbf1f
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 75 deletions.
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func addSentryScope(scope *sentry.Scope) {
}
scope.SetContext("Services", imageToVersion)
scope.SetContext("Config", map[string]interface{}{
"INTERNAL_IMAGE_REGISTRY": viper.Get("INTERNAL_IMAGE_REGISTRY"),
"PROJECT_ID": flags.ProjectRef,
"Image Registry": utils.GetRegistry(),
"Project ID": flags.ProjectRef,
})
}
5 changes: 1 addition & 4 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/supabase/cli/internal/start"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

Expand All @@ -23,11 +22,9 @@ var (
RunE: func(cmd *cobra.Command, args []string) error {
fsys := afero.NewOsFs()
if preview {
projectRef, err := utils.LoadProjectRef(fsys)
if err != nil {
if _, err := flags.LoadProjectRef(fsys); err != nil {
return err
}
flags.ProjectRef = projectRef
}
return start.Run(cmd.Context(), fsys, excludedContainers, ignoreHealthCheck, flags.ProjectRef)
},
Expand Down
3 changes: 2 additions & 1 deletion internal/branches/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/gen/keys"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/api"
)

func Run(ctx context.Context, name, region string, fsys afero.Fs) error {
ref, err := utils.LoadProjectRef(fsys)
ref, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/branches/disable/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

func Run(ctx context.Context, fsys afero.Fs) error {
ref, err := utils.LoadProjectRef(fsys)
ref, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/branches/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

func Run(ctx context.Context, fsys afero.Fs) error {
ref, err := utils.LoadProjectRef(fsys)
ref, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/gen/types/typescript/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/jackc/pgconn"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/api"
)

Expand Down Expand Up @@ -114,7 +115,7 @@ func Run(ctx context.Context, useLocal bool, useLinked bool, projectId string, d
}

if useLinked {
projectId, err := utils.LoadProjectRef(fsys)
projectId, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
9 changes: 3 additions & 6 deletions internal/inspect/role_connections/role_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ func Run(ctx context.Context, config pgconn.Config, fsys afero.Fs, options ...fu
fmt.Printf("\nActive connections %d/%d\n\n", sum, result[0].Connection_limit)
}

ref, err := utils.LoadProjectRef(fsys)
if err != nil {
return err
if matches := utils.ProjectHostPattern.FindStringSubmatch(config.Host); len(matches) == 4 {
fmt.Println("Go to the dashboard for more here:")
fmt.Printf("https://app.supabase.com/project/%s/database/roles\n", matches[2])
}

fmt.Println("Go to the dashboard for more here:")
fmt.Printf("https://app.supabase.com/project/%s/database/roles\n", ref)

return nil
}
3 changes: 1 addition & 2 deletions internal/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/migration/repair"
"github.com/supabase/cli/internal/services"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/credentials"
"github.com/supabase/cli/internal/utils/tenant"
Expand Down Expand Up @@ -201,7 +200,7 @@ func linkDatabase(ctx context.Context, config pgconn.Config, options ...func(*pg
}

func linkDatabaseVersion(ctx context.Context, projectRef string, fsys afero.Fs) error {
version, err := services.GetDatabaseVersion(ctx, projectRef)
version, err := tenant.GetDatabaseVersion(ctx, projectRef)

Check failure on line 203 in internal/link/link.go

View workflow job for this annotation

GitHub Actions / Start

undefined: tenant.GetDatabaseVersion

Check failure on line 203 in internal/link/link.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: tenant.GetDatabaseVersion) (typecheck)

Check failure on line 203 in internal/link/link.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: tenant.GetDatabaseVersion (typecheck)

Check failure on line 203 in internal/link/link.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: tenant.GetDatabaseVersion) (typecheck)

Check failure on line 203 in internal/link/link.go

View workflow job for this annotation

GitHub Actions / Test

undefined: tenant.GetDatabaseVersion
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/projects/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

func Run(ctx context.Context, fsys afero.Fs) error {
Expand All @@ -23,7 +24,7 @@ func Run(ctx context.Context, fsys afero.Fs) error {
return errors.New("Unexpected error retrieving projects: " + string(resp.Body))
}

projectRef, err := utils.LoadProjectRef(fsys)
projectRef, err := flags.LoadProjectRef(fsys)
if err != nil && err != utils.ErrNotLinked {
fmt.Fprintln(os.Stderr, err)
}
Expand Down
27 changes: 4 additions & 23 deletions internal/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,26 @@ import (
"strings"
"sync"

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/internal/utils/tenant"
)

var (
errDatabaseVersion = errors.New("Database version not found.")
suggestLinkCommand = fmt.Sprintf("Run %s to sync your local image versions with the linked project.", utils.Aqua("supabase link"))
)
var suggestLinkCommand = fmt.Sprintf("Run %s to sync your local image versions with the linked project.", utils.Aqua("supabase link"))

func Run(ctx context.Context, fsys afero.Fs) error {
_ = utils.LoadConfigFS(fsys)
serviceImages := GetServiceImages()

linked := make(map[string]string)
if projectRef, err := utils.LoadProjectRef(fsys); err == nil {
if projectRef, err := flags.LoadProjectRef(fsys); err == nil {
var wg sync.WaitGroup
wg.Add(4)
go func() {
defer wg.Done()
if version, err := GetDatabaseVersion(ctx, projectRef); err == nil {
if version, err := tenant.GetDatabaseVersion(ctx, projectRef); err == nil {

Check failure on line 28 in internal/services/services.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: tenant.GetDatabaseVersion (typecheck)
linked[utils.Config.Db.Image] = version
}
}()
Expand Down Expand Up @@ -85,19 +82,3 @@ func GetServiceImages() []string {
utils.ImageProxyImage,
}
}

func GetDatabaseVersion(ctx context.Context, projectRef string) (string, error) {
resp, err := utils.GetSupabase().GetProjectsWithResponse(ctx)
if err != nil {
return "", errors.Errorf("failed to retrieve projects: %w", err)
}
if resp.JSON200 == nil {
return "", errors.New("Unexpected error retrieving projects: " + string(resp.Body))
}
for _, project := range *resp.JSON200 {
if project.Id == projectRef && len(project.Database.Version) > 0 {
return project.Database.Version, nil
}
}
return "", errors.New(errDatabaseVersion)
}
3 changes: 2 additions & 1 deletion internal/snippets/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
"github.com/supabase/cli/pkg/api"
)

func Run(ctx context.Context, fsys afero.Fs) error {
ref, err := utils.LoadProjectRef(fsys)
ref, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/storage/cp/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/supabase/cli/internal/storage/client"
"github.com/supabase/cli/internal/storage/ls"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

var errUnsupportedOperation = errors.New("Unsupported operation")
Expand All @@ -29,7 +30,7 @@ func Run(ctx context.Context, src, dst string, recursive bool, fsys afero.Fs) er
if err != nil {
return errors.Errorf("failed to parse dst url: %w", err)
}
projectRef, err := utils.LoadProjectRef(fsys)
projectRef, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/storage/ls/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/storage"
"github.com/supabase/cli/internal/storage/client"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

func Run(ctx context.Context, objectPath string, recursive bool, fsys afero.Fs) error {
remotePath, err := storage.ParseStorageURL(objectPath)
if err != nil {
return err
}
projectRef, err := utils.LoadProjectRef(fsys)
projectRef, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/storage/mv/mv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/supabase/cli/internal/storage"
"github.com/supabase/cli/internal/storage/client"
"github.com/supabase/cli/internal/storage/ls"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

var (
Expand All @@ -29,7 +29,7 @@ func Run(ctx context.Context, src, dst string, recursive bool, fsys afero.Fs) er
if err != nil {
return err
}
projectRef, err := utils.LoadProjectRef(fsys)
projectRef, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/storage/rm/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/supabase/cli/internal/storage/cp"
"github.com/supabase/cli/internal/storage/ls"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/flags"
)

var (
Expand Down Expand Up @@ -44,7 +45,7 @@ func Run(ctx context.Context, paths []string, recursive bool, fsys afero.Fs) err
}
groups[bucket] = append(groups[bucket], prefix)
}
projectRef, err := utils.LoadProjectRef(fsys)
projectRef, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func GetRegistryAuth() string {
registryOnce.Do(func() {
config := dockerConfig.LoadDefaultConfigFile(os.Stderr)
// Ref: https://docs.docker.com/engine/api/sdk/examples/#pull-an-image-with-authentication
auth, err := config.GetAuthConfig(getRegistry())
auth, err := config.GetAuthConfig(GetRegistry())
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to load registry credentials:", err)
return
Expand All @@ -188,7 +188,7 @@ func GetRegistryAuth() string {
// Defaults to Supabase public ECR for faster image pull
const defaultRegistry = "public.ecr.aws"

func getRegistry() string {
func GetRegistry() string {
registry := viper.GetString("INTERNAL_IMAGE_REGISTRY")
if len(registry) == 0 {
return defaultRegistry
Expand All @@ -197,7 +197,7 @@ func getRegistry() string {
}

func GetRegistryImageUrl(imageName string) string {
registry := getRegistry()
registry := GetRegistry()
if registry == "docker.io" {
return imageName
}
Expand Down
4 changes: 2 additions & 2 deletions internal/utils/flags/db_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func ParseDatabaseConfig(flagSet *pflag.FlagSet, fsys afero.Fs) error {
DbConfig.Password = utils.Config.Db.Password
DbConfig.Database = "postgres"
case linked:
projectRef, err := utils.LoadProjectRef(fsys)
projectRef, err := LoadProjectRef(fsys)
if err != nil {
return err
}
Expand All @@ -76,7 +76,7 @@ func ParseDatabaseConfig(flagSet *pflag.FlagSet, fsys afero.Fs) error {
if err != nil {
return err
}
projectRef, err := utils.LoadProjectRef(fsys)
projectRef, err := LoadProjectRef(fsys)
if err != nil {
return err
}
Expand Down
21 changes: 16 additions & 5 deletions internal/utils/flags/project_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ func ParseProjectRef(fsys afero.Fs) error {
return utils.AssertProjectRefIsValid(ProjectRef)
}
// Followed by linked ref file
if projectRefBytes, err := afero.ReadFile(fsys, utils.ProjectRefPath); err == nil {
ProjectRef = string(bytes.TrimSpace(projectRefBytes))
return utils.AssertProjectRefIsValid(ProjectRef)
} else if !errors.Is(err, os.ErrNotExist) {
return errors.Errorf("failed to load project ref: %w", err)
if _, err := LoadProjectRef(fsys); !errors.Is(err, utils.ErrNotLinked) {
return err
}
// Prompt as the last resort
if term.IsTerminal(int(os.Stdin.Fd())) {
Expand All @@ -51,3 +48,17 @@ Enter your project ref: `, utils.GetSupabaseDashboardURL())
ProjectRef = strings.TrimSpace(scanner.Text())
return utils.AssertProjectRefIsValid(ProjectRef)
}

func LoadProjectRef(fsys afero.Fs) (string, error) {
projectRefBytes, err := afero.ReadFile(fsys, utils.ProjectRefPath)
if errors.Is(err, os.ErrNotExist) {
return "", errors.New(utils.ErrNotLinked)
} else if err != nil {
return "", errors.Errorf("failed to load project ref: %w", err)
}
ProjectRef := string(bytes.TrimSpace(projectRefBytes))
if err := utils.AssertProjectRefIsValid(ProjectRef); err != nil {
return "", err
}
return ProjectRef, nil
}
17 changes: 1 addition & 16 deletions internal/utils/misc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"bytes"
"context"
_ "embed"
"os"
Expand Down Expand Up @@ -95,7 +94,7 @@ var (

ProjectRefPattern = regexp.MustCompile(`^[a-z]{20}$`)
UUIDPattern = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`)
ProjectHostPattern = regexp.MustCompile(`^(db\.)[a-z]{20}\.supabase\.(co|red)$`)
ProjectHostPattern = regexp.MustCompile(`^(db\.)([a-z]{20})\.supabase\.(co|red)$`)
MigrateFilePattern = regexp.MustCompile(`^([0-9]+)_(.*)\.sql$`)
BranchNamePattern = regexp.MustCompile(`[[:word:]-]+`)
FuncSlugPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_-]*$`)
Expand Down Expand Up @@ -289,20 +288,6 @@ func AssertProjectRefIsValid(projectRef string) error {
return nil
}

func LoadProjectRef(fsys afero.Fs) (string, error) {
projectRefBytes, err := afero.ReadFile(fsys, ProjectRefPath)
if errors.Is(err, os.ErrNotExist) {
return "", errors.New(ErrNotLinked)
} else if err != nil {
return "", errors.Errorf("failed to load project ref: %w", err)
}
projectRef := string(bytes.TrimSpace(projectRefBytes))
if !ProjectRefPattern.MatchString(projectRef) {
return "", errors.New(ErrInvalidRef)
}
return projectRef, nil
}

func ValidateFunctionSlug(slug string) error {
if !FuncSlugPattern.MatchString(slug) {
return errors.New(ErrInvalidSlug)
Expand Down

0 comments on commit 09fbf1f

Please sign in to comment.