From 536694ca0a51dea2bfaee47d84eb582c2db2db7e Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Mon, 6 Oct 2025 15:55:18 +0800 Subject: [PATCH] fix: remember profile after login --- cmd/login.go | 8 ++++++++ cmd/root.go | 1 + internal/utils/misc.go | 1 + internal/utils/profile.go | 17 +++++++++++++++-- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/cmd/login.go b/cmd/login.go index ca9aea666..d1d4086fd 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -6,6 +6,7 @@ import ( "github.com/go-errors/errors" "github.com/spf13/afero" "github.com/spf13/cobra" + "github.com/spf13/viper" "github.com/supabase/cli/internal/login" "github.com/supabase/cli/internal/utils" "golang.org/x/term" @@ -38,6 +39,13 @@ var ( } return login.Run(cmd.Context(), os.Stdout, params) }, + PostRunE: func(cmd *cobra.Command, args []string) error { + if prof := viper.GetString("PROFILE"); viper.IsSet("PROFILE") { + // Failure to save should block subsequent commands on CI + return utils.WriteFile(utils.ProfilePath, []byte(prof), afero.NewOsFs()) + } + return nil + }, } ) diff --git a/cmd/root.go b/cmd/root.go index db1cfabbc..8329cd86c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -120,6 +120,7 @@ var ( if viper.GetBool("DEBUG") { http.DefaultTransport = debug.NewTransport() fmt.Fprintln(os.Stderr, cmd.Root().Short) + fmt.Fprintf(os.Stderr, "Using profile: %s (%s)\n", utils.CurrentProfile.Name, utils.CurrentProfile.ProjectHost) } cmd.SetContext(ctx) // Setup sentry last to ignore errors from parsing cli flags diff --git a/internal/utils/misc.go b/internal/utils/misc.go index 80fb13b27..84d65eaeb 100644 --- a/internal/utils/misc.go +++ b/internal/utils/misc.go @@ -74,6 +74,7 @@ var ( PoolerVersionPath = filepath.Join(TempDir, "pooler-version") RealtimeVersionPath = filepath.Join(TempDir, "realtime-version") CliVersionPath = filepath.Join(TempDir, "cli-latest") + ProfilePath = filepath.Join(TempDir, "profile") CurrBranchPath = filepath.Join(SupabaseDirPath, ".branches", "_current_branch") SchemasDir = filepath.Join(SupabaseDirPath, "schemas") MigrationsDir = filepath.Join(SupabaseDirPath, "migrations") diff --git a/internal/utils/profile.go b/internal/utils/profile.go index 53da75858..d05d493f3 100644 --- a/internal/utils/profile.go +++ b/internal/utils/profile.go @@ -43,10 +43,9 @@ var allProfiles = []Profile{{ var CurrentProfile Profile func LoadProfile(ctx context.Context, fsys afero.Fs) error { - prof := viper.GetString("PROFILE") + prof := getProfileName(fsys) for _, p := range allProfiles { if strings.EqualFold(p.Name, prof) { - fmt.Fprintln(GetDebugLogger(), "Using project host:", p.ProjectHost) CurrentProfile = p return nil } @@ -68,3 +67,17 @@ func LoadProfile(ctx context.Context, fsys afero.Fs) error { } return nil } + +func getProfileName(fsys afero.Fs) string { + debuglogger := GetDebugLogger() + if prof := viper.GetString("PROFILE"); viper.IsSet("PROFILE") { + fmt.Fprintln(debuglogger, "Loading profile from flag:", prof) + return prof + } else if content, err := afero.ReadFile(fsys, ProfilePath); err == nil { + fmt.Fprintln(debuglogger, "Loading profile from file:", ProfilePath) + return string(content) + } else { + fmt.Fprintln(debuglogger, err) + return prof + } +}