Skip to content

Commit

Permalink
fix: allows env var to override linked project id (#2480)
Browse files Browse the repository at this point in the history
* fix: allows env var to override linked project id

* chore: only unlink the temp project
  • Loading branch information
sweatybridge authored Nov 8, 2024
1 parent e26aee5 commit 14fa86b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
11 changes: 5 additions & 6 deletions internal/unlink/unlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ import (
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/credentials"
"github.com/supabase/cli/internal/utils/flags"
"github.com/zalando/go-keyring"
)

func Run(ctx context.Context, fsys afero.Fs) error {
projectRef, err := flags.LoadProjectRef(fsys)
if err != nil {
return err
}
if err := Unlink(projectRef, fsys); err != nil {
if projectRef, err := afero.ReadFile(fsys, utils.ProjectRefPath); errors.Is(err, os.ErrNotExist) {
return errors.New(utils.ErrNotLinked)
} else if err != nil {
return errors.Errorf("failed to load project ref: %w", err)
} else if err := Unlink(string(projectRef), fsys); err != nil {
return err
}
fmt.Fprintln(os.Stdout, "Finished "+utils.Aqua("supabase unlink")+".")
Expand Down
19 changes: 10 additions & 9 deletions internal/utils/flags/project_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ var ProjectRef string

func ParseProjectRef(ctx context.Context, fsys afero.Fs) error {
// Flag takes highest precedence
if len(ProjectRef) == 0 {
ProjectRef = viper.GetString("PROJECT_ID")
}
if len(ProjectRef) > 0 {
return utils.AssertProjectRefIsValid(ProjectRef)
}
Expand Down Expand Up @@ -59,13 +56,17 @@ func PromptProjectRef(ctx context.Context, title string) error {
}

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)
// Env var takes precedence over ref file
ProjectRef = viper.GetString("PROJECT_ID")
if len(ProjectRef) == 0 {
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))
}
ProjectRef = string(bytes.TrimSpace(projectRefBytes))
if err := utils.AssertProjectRefIsValid(ProjectRef); err != nil {
return "", err
}
Expand Down

0 comments on commit 14fa86b

Please sign in to comment.