func (p Profile) SetRealmBaseURL(realmBaseURL string) {
	p.SetString(keyRealmBaseURL, realmBaseURL)
}
SetRealmBaseURL sets the CLI profile Realm base url
package user
import (
	"fmt"
	"os"
	"path/filepath"
	"time"
	"github.com/10gen/realm-cli/internal/telemetry"
	"github.com/spf13/afero"
	"github.com/spf13/viper"
)
const (
	DefaultProfile = "default"
	HostingAssetCacheDir = ".asset-cache"
	envPrefix   = "realm"
	profileType = "yaml"
	extJSON = ".json"
)
const (
	FlagProfile = "profile"
	FlagProfileUsage = `Specify your profile (Default value: "default")`
	FlagAtlasBaseURL      = "atlas-url"
	FlagAtlasBaseURLUsage = "specify the base Atlas server URL"
	FlagRealmBaseURL      = "realm-url"
	FlagRealmBaseURLUsage = "specify the base Realm server URL"
	defaultAtlasBaseURL = "https://cloud.mongodb.com"
	defaultRealmBaseURL = "https://realm.mongodb.com"
)
type Profile struct {
	Flags
	Name             string
	WorkingDirectory string
	dir string
	fs  afero.Fs
}
type Flags struct {
	AtlasBaseURL  string
	RealmBaseURL  string
	TelemetryMode telemetry.Mode
}
func NewDefaultProfile() (*Profile, error) {
	return NewProfile(DefaultProfile)
}
func NewProfile(name string) (*Profile, error) {
	dir, dirErr := HomeDir()
	if dirErr != nil {
		return nil, fmt.Errorf("failed to create CLI profile: %w", dirErr)
	}
	wd, wdErr := os.Getwd()
	if wdErr != nil {
		return nil, fmt.Errorf("failed to get current working directory: %w", wdErr)
	}
	return &Profile{
		Name:             name,
		dir:              dir,
		fs:               afero.NewOsFs(),
		WorkingDirectory: wd,
	}, nil
}
func (p Profile) Clear(name string) {
	p.SetString(name, "")
}
func (p Profile) SetString(name, value string) {
	viper.Set(p.propertyKey(name), value)
}
func (p Profile) GetString(name string) string {
	return viper.GetString(p.propertyKey(name))
}
func (p Profile) propertyKey(name string) string {
	return fmt.Sprintf("%s.%s", p.Name, name)
}
func (p Profile) Load() error {
	viper.SetConfigName(p.Name)
	viper.AddConfigPath(p.dir)
	viper.SetConfigPermissions(0600)
	viper.SetConfigType(profileType)
	viper.SetEnvPrefix(envPrefix)
	viper.AutomaticEnv()
	if err := viper.ReadInConfig(); err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
			return nil  
		}
		return fmt.Errorf("failed to load CLI profile: %s", err)
	}
	return nil
}
func (p *Profile) Save() error {
	exists, existsErr := afero.DirExists(p.fs, p.dir)
	if existsErr != nil {
		return fmt.Errorf("failed to save CLI profile: %s", existsErr)
	}
	if !exists {
		if err := p.fs.MkdirAll(p.dir, 0700); err != nil {
			return fmt.Errorf("failed to save CLI profile: %s", err)
		}
	}
	if err := viper.WriteConfigAs(p.Path()); err != nil {
		return fmt.Errorf("failed to save CLI profile: %s", err)
	}
	return nil
}
func (p *Profile) ResolveFlags() error {
	if p.Flags.TelemetryMode == telemetry.ModeEmpty {
		p.Flags.TelemetryMode = p.TelemetryMode()
	}
	p.SetString(keyTelemetryMode, string(p.Flags.TelemetryMode))
	if p.Flags.RealmBaseURL == "" {
		realmBaseURL := p.RealmBaseURL()
		if realmBaseURL == "" {
			realmBaseURL = defaultRealmBaseURL
		}
		p.Flags.RealmBaseURL = realmBaseURL
	}
	p.SetRealmBaseURL(p.Flags.RealmBaseURL)
	if p.Flags.AtlasBaseURL == "" {
		atlasBaseURL := p.AtlasBaseURL()
		if atlasBaseURL == "" {
			atlasBaseURL = defaultAtlasBaseURL
		}
		p.Flags.AtlasBaseURL = atlasBaseURL
	}
	p.SetAtlasBaseURL(p.Flags.AtlasBaseURL)
	return p.Save()
}
func (p Profile) Dir() string {
	return p.dir
}
func (p Profile) Path() string {
	return fmt.Sprintf("%s/%s.%s", p.dir, p.Name, profileType)
}
const (
	keyPublicAPIKey  = "public_api_key"
	keyPrivateAPIKey = "private_api_key"
	keyAccessToken   = "access_token"
	keyRefreshToken  = "refresh_token"
	keyRealmBaseURL     = "realm_base_url"
	keyAtlasBaseURL     = "atlas_base_url"
	keyTelemetryMode    = "telemetry_mode"
	keyLastVersionCheck = "last_version_check"
)
func (p Profile) TelemetryMode() telemetry.Mode {
	return telemetry.Mode(p.GetString(keyTelemetryMode))
}
func (p Profile) Credentials() Credentials {
	return Credentials{
		p.GetString(keyPublicAPIKey),
		p.GetString(keyPrivateAPIKey),
	}
}
func (p Profile) SetCredentials(creds Credentials) {
	p.SetString(keyPublicAPIKey, creds.PublicAPIKey)
	p.SetString(keyPrivateAPIKey, creds.PrivateAPIKey)
}
func (p Profile) ClearCredentials() {
	p.Clear(keyPublicAPIKey)
	p.Clear(keyPrivateAPIKey)
}
func (p Profile) Session() Session {
	return Session{
		p.GetString(keyAccessToken),
		p.GetString(keyRefreshToken),
	}
}
func (p Profile) SetSession(session Session) {
	p.SetString(keyAccessToken, session.AccessToken)
	p.SetString(keyRefreshToken, session.RefreshToken)
}
func (p Profile) ClearSession() {
	p.Clear(keyAccessToken)
	p.Clear(keyRefreshToken)
}
func (p Profile) RealmBaseURL() string {
	return p.GetString(keyRealmBaseURL)
}