Skip to content

Commit

Permalink
chore: skip loading project ref if flag is set
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Dec 7, 2024
1 parent bfa2c42 commit 8b60145
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 34 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ var (
}
}
}
// TODO: always load config.toml
if err := flags.ParseDatabaseConfig(cmd.Flags(), fsys); err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/projects/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ func Run(ctx context.Context, fsys afero.Fs) error {
return errors.New("Unexpected error retrieving projects: " + string(resp.Body))
}

projectRef, err := flags.LoadProjectRef(fsys)
if err != nil && err != utils.ErrNotLinked {
if err := flags.LoadProjectRef(fsys); err != nil && err != utils.ErrNotLinked {
fmt.Fprintln(os.Stderr, err)
}

var projects []linkedProject
for _, project := range *resp.JSON200 {
projects = append(projects, linkedProject{
V1ProjectWithDatabaseResponse: project,
Linked: project.Id == projectRef,
Linked: project.Id == flags.ProjectRef,
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

func Run(ctx context.Context, fsys afero.Fs) error {
if _, err := flags.LoadProjectRef(fsys); err != nil && !errors.Is(err, utils.ErrNotLinked) {
if err := flags.LoadProjectRef(fsys); err != nil && !errors.Is(err, utils.ErrNotLinked) {
fmt.Fprintln(os.Stderr, err)
}
if err := utils.Config.Load("", utils.NewRootFS(fsys)); err != nil && !errors.Is(err, os.ErrNotExist) {
Expand Down
2 changes: 1 addition & 1 deletion internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
func Run(ctx context.Context, fsys afero.Fs, excludedContainers []string, ignoreHealthCheck bool) error {
// Sanity checks.
{
_, _ = flags.LoadProjectRef(fsys)
_ = flags.LoadProjectRef(fsys)
if err := utils.LoadConfigFS(fsys); err != nil {
return err
}
Expand Down
16 changes: 6 additions & 10 deletions internal/utils/flags/db_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ func ParseDatabaseConfig(flagSet *pflag.FlagSet, fsys afero.Fs) error {
// Update connection config
switch connType {
case direct:
if err := utils.Config.Load("", utils.NewRootFS(fsys)); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
if err := utils.Config.Load("", utils.NewRootFS(fsys)); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
if flag := flagSet.Lookup("db-url"); flag != nil {
config, err := pgconn.ParseConfig(flag.Value.String())
Expand All @@ -76,25 +74,23 @@ func ParseDatabaseConfig(flagSet *pflag.FlagSet, fsys afero.Fs) error {
if err := utils.LoadConfigFS(fsys); err != nil {
return err
}
projectRef, err := LoadProjectRef(fsys)
if err != nil {
if err := LoadProjectRef(fsys); err != nil {
return err
}
DbConfig = NewDbConfigWithPassword(projectRef)
DbConfig = NewDbConfigWithPassword(ProjectRef)
case proxy:
token, err := utils.LoadAccessTokenFS(fsys)
if err != nil {
return err
}
projectRef, err := LoadProjectRef(fsys)
if err != nil {
if err := LoadProjectRef(fsys); err != nil {
return err
}
DbConfig.Host = utils.GetSupabaseAPIHost()
DbConfig.Port = 443
DbConfig.User = "postgres"
DbConfig.Password = token
DbConfig.Database = projectRef
DbConfig.Database = ProjectRef
}
return nil
}
Expand Down
35 changes: 16 additions & 19 deletions internal/utils/flags/project_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ import (
var ProjectRef string

func ParseProjectRef(ctx context.Context, fsys afero.Fs) error {
// Flag takes highest precedence
if len(ProjectRef) > 0 {
return utils.AssertProjectRefIsValid(ProjectRef)
}
// Followed by linked ref file
if _, err := LoadProjectRef(fsys); !errors.Is(err, utils.ErrNotLinked) {
if err := LoadProjectRef(fsys); !errors.Is(err, utils.ErrNotLinked) {
return err
}
// Prompt as the last resort
Expand Down Expand Up @@ -55,20 +50,22 @@ func PromptProjectRef(ctx context.Context, title string) error {
return nil
}

func LoadProjectRef(fsys afero.Fs) (string, error) {
func LoadProjectRef(fsys afero.Fs) error {
// Flag takes highest precedence
if len(ProjectRef) > 0 {
return utils.AssertProjectRefIsValid(ProjectRef)
}
// 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))
if ProjectRef = viper.GetString("PROJECT_ID"); len(ProjectRef) > 0 {
return utils.AssertProjectRefIsValid(ProjectRef)
}
if err := utils.AssertProjectRefIsValid(ProjectRef); err != nil {
return "", err
// Load from local file last
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)
}
return ProjectRef, nil
ProjectRef = string(bytes.TrimSpace(projectRefBytes))
return utils.AssertProjectRefIsValid(ProjectRef)
}

0 comments on commit 8b60145

Please sign in to comment.