Skip to content

Commit b4918f3

Browse files
fix: save profile to global path (#4697)
Co-authored-by: Andrew Valleteau <avallete@users.noreply.github.com>
1 parent ba32edc commit b4918f3

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

cmd/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var (
4242
PostRunE: func(cmd *cobra.Command, args []string) error {
4343
if prof := viper.GetString("PROFILE"); viper.IsSet("PROFILE") {
4444
// Failure to save should block subsequent commands on CI
45-
return utils.WriteFile(utils.ProfilePath, []byte(prof), afero.NewOsFs())
45+
return utils.SaveProfileName(prof, afero.NewOsFs())
4646
}
4747
return nil
4848
},

internal/utils/misc.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ var (
7575
PoolerVersionPath = filepath.Join(TempDir, "pooler-version")
7676
RealtimeVersionPath = filepath.Join(TempDir, "realtime-version")
7777
CliVersionPath = filepath.Join(TempDir, "cli-latest")
78-
ProfilePath = filepath.Join(TempDir, "profile")
7978
CurrBranchPath = filepath.Join(SupabaseDirPath, ".branches", "_current_branch")
8079
ClusterDir = filepath.Join(SupabaseDirPath, "cluster")
8180
SchemasDir = filepath.Join(SupabaseDirPath, "schemas")

internal/utils/profile.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package utils
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
"sort"
79
"strings"
810

@@ -121,15 +123,34 @@ func getProfileName(fsys afero.Fs) string {
121123
if prof := viper.GetString("PROFILE"); viper.IsSet("PROFILE") {
122124
fmt.Fprintln(debuglogger, "Loading profile from flag:", prof)
123125
return prof
124-
} else if content, err := afero.ReadFile(fsys, ProfilePath); err == nil {
125-
fmt.Fprintln(debuglogger, "Loading profile from file:", ProfilePath)
126+
} else if profilePath, err := getProfilePath(); err != nil {
127+
fmt.Fprintln(debuglogger, err)
128+
return prof
129+
} else if content, err := afero.ReadFile(fsys, profilePath); err == nil {
130+
fmt.Fprintln(debuglogger, "Loading profile from file:", profilePath)
126131
return string(content)
127132
} else {
128133
fmt.Fprintln(debuglogger, err)
129134
return prof
130135
}
131136
}
132137

138+
func getProfilePath() (string, error) {
139+
home, err := os.UserHomeDir()
140+
if err != nil {
141+
return "", errors.Errorf("failed to get $HOME directory: %w", err)
142+
}
143+
return filepath.Join(home, ".supabase", "profile"), nil
144+
}
145+
146+
func SaveProfileName(prof string, fsys afero.Fs) error {
147+
profilePath, err := getProfilePath()
148+
if err != nil {
149+
return err
150+
}
151+
return WriteFile(profilePath, []byte(prof), fsys)
152+
}
153+
133154
func AwsRegions() []string {
134155
result := make([]string, len(allProfiles[0].ProjectRegions))
135156
for i, region := range allProfiles[0].ProjectRegions {

0 commit comments

Comments
 (0)