Skip to content

Commit 6e9af16

Browse files
authored
fix(DEVWF-755): remember profile after login (#4253)
fix: remember profile after login
1 parent e43b5c6 commit 6e9af16

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

cmd/login.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/go-errors/errors"
77
"github.com/spf13/afero"
88
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
910
"github.com/supabase/cli/internal/login"
1011
"github.com/supabase/cli/internal/utils"
1112
"golang.org/x/term"
@@ -38,6 +39,13 @@ var (
3839
}
3940
return login.Run(cmd.Context(), os.Stdout, params)
4041
},
42+
PostRunE: func(cmd *cobra.Command, args []string) error {
43+
if prof := viper.GetString("PROFILE"); viper.IsSet("PROFILE") {
44+
// Failure to save should block subsequent commands on CI
45+
return utils.WriteFile(utils.ProfilePath, []byte(prof), afero.NewOsFs())
46+
}
47+
return nil
48+
},
4149
}
4250
)
4351

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ var (
120120
if viper.GetBool("DEBUG") {
121121
http.DefaultTransport = debug.NewTransport()
122122
fmt.Fprintln(os.Stderr, cmd.Root().Short)
123+
fmt.Fprintf(os.Stderr, "Using profile: %s (%s)\n", utils.CurrentProfile.Name, utils.CurrentProfile.ProjectHost)
123124
}
124125
cmd.SetContext(ctx)
125126
// Setup sentry last to ignore errors from parsing cli flags

internal/utils/misc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var (
7474
PoolerVersionPath = filepath.Join(TempDir, "pooler-version")
7575
RealtimeVersionPath = filepath.Join(TempDir, "realtime-version")
7676
CliVersionPath = filepath.Join(TempDir, "cli-latest")
77+
ProfilePath = filepath.Join(TempDir, "profile")
7778
CurrBranchPath = filepath.Join(SupabaseDirPath, ".branches", "_current_branch")
7879
SchemasDir = filepath.Join(SupabaseDirPath, "schemas")
7980
MigrationsDir = filepath.Join(SupabaseDirPath, "migrations")

internal/utils/profile.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ var allProfiles = []Profile{{
4343
var CurrentProfile Profile
4444

4545
func LoadProfile(ctx context.Context, fsys afero.Fs) error {
46-
prof := viper.GetString("PROFILE")
46+
prof := getProfileName(fsys)
4747
for _, p := range allProfiles {
4848
if strings.EqualFold(p.Name, prof) {
49-
fmt.Fprintln(GetDebugLogger(), "Using project host:", p.ProjectHost)
5049
CurrentProfile = p
5150
return nil
5251
}
@@ -68,3 +67,17 @@ func LoadProfile(ctx context.Context, fsys afero.Fs) error {
6867
}
6968
return nil
7069
}
70+
71+
func getProfileName(fsys afero.Fs) string {
72+
debuglogger := GetDebugLogger()
73+
if prof := viper.GetString("PROFILE"); viper.IsSet("PROFILE") {
74+
fmt.Fprintln(debuglogger, "Loading profile from flag:", prof)
75+
return prof
76+
} else if content, err := afero.ReadFile(fsys, ProfilePath); err == nil {
77+
fmt.Fprintln(debuglogger, "Loading profile from file:", ProfilePath)
78+
return string(content)
79+
} else {
80+
fmt.Fprintln(debuglogger, err)
81+
return prof
82+
}
83+
}

0 commit comments

Comments
 (0)