config

package
v0.0.0-...-7dd4701 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProfileFlag       = "profile"
	MongoCLIEnvPrefix = "MCLI"          // MongoCLIEnvPrefix prefix for MongoCLI ENV variables
	AtlasCLIEnvPrefix = "MONGODB_ATLAS" // AtlasCLIEnvPrefix prefix for AtlasCLI ENV variables
	DefaultProfile    = "default"       // DefaultProfile default
	CloudService      = "cloud"         // CloudService setting when using Atlas API
	CloudGovService   = "cloudgov"      // CloudGovService setting when using Atlas API for Government

	AuthTypeField = "auth_type"

	AccessTokenField   = "access_token"
	RefreshTokenField  = "refresh_token"
	ClientIDField      = "client_id"
	ClientSecretField  = "client_secret"
	OpsManagerURLField = "ops_manager_url"
	AccountURLField    = "account_url"

	TelemetryEnabledProperty = "telemetry_enabled"
	AtlasCLI                 = "atlascli"
	ContainerizedHostNameEnv = "MONGODB_ATLAS_IS_CONTAINERIZED"
	GitHubActionsHostNameEnv = "GITHUB_ACTIONS"
	AtlasActionHostNameEnv   = "ATLAS_GITHUB_ACTION"
	CLIUserTypeEnv           = "CLI_USER_TYPE" // CLIUserTypeEnv is used to separate MongoDB University users from default users
	DefaultUser              = "default"       // Users that do NOT use ATLAS CLI with MongoDB University
	UniversityUser           = "university"    // Users that uses ATLAS CLI with MongoDB University
	NativeHostName           = "native"
	DockerContainerHostName  = "container"
	GitHubActionsHostName    = "all_github_actions"
	AtlasActionHostName      = "atlascli_github_action"
	LocalDeploymentImage     = "local_deployment_image" // LocalDeploymentImage is the config key for the MongoDB Local Dev Docker image
	Version                  = "version"                // versionField is the key for the configuration version
)
View Source
const (
	MaxSupportedVersion = 2
)

MaxSupportedVersion is the maximum supported config version available in AtlasCLI.

Variables

View Source
var (
	CLIUserType = newCLIUserTypeFromEnvs()
	HostName    = getConfigHostnameFromEnvs()
)
View Source
var ErrProfileNameHasDots = errors.New("profile should not contain '.'")
View Source
var SecureProperties = []string{
	publicAPIKey,
	privateAPIKey,
	AccessTokenField,
	RefreshTokenField,
	ClientIDField,
	ClientSecretField,
}

SecureProperties defines which configuration properties require secure storage.

Functions

func APIVersion

func APIVersion() string

APIVersion get the default API version.

func AccessToken

func AccessToken() string

AccessToken get configured access token.

func AccessTokenSubject

func AccessTokenSubject() (string, error)

AccessTokenSubject will return the encoded subject in a JWT. This method won't verify the token signature, it's only safe to use to get the token claims.

func AccountURL

func AccountURL() string

AccountURL gets the configured account base url.

func AllProperties

func AllProperties() []string

func BooleanProperties

func BooleanProperties() []string

func CLIConfigHome

func CLIConfigHome() (string, error)

CLIConfigHome retrieves configHome path.

func ClientID

func ClientID() string

ClientID get configured client ID.

func ClientSecret

func ClientSecret() string

ClientSecret get configured client secret.

func Delete

func Delete() error

Delete deletes an existing configuration. The profiles are reloaded afterwards, as this edits the file directly.

func Exists

func Exists(name string) bool

Exists returns true if a profile with the give name exists.

func Get

func Get(name string) any

func GetBool

func GetBool(name string) bool

func GetInt64

func GetInt64(name string) int64

func GetLocalDeploymentImage

func GetLocalDeploymentImage() string

GetLocalDeploymentImage returns the configured MongoDB Docker image URL.

func GetString

func GetString(name string) string

func GlobalProperties

func GlobalProperties() []string

func InitProfile

func InitProfile(profile string) error

func IsAccessSet

func IsAccessSet() bool

IsAccessSet return true if Service Account or API Keys credentials have been set up.

func IsCloud

func IsCloud() bool

func IsTelemetryEnabledSet

func IsTelemetryEnabledSet() bool

IsTelemetryEnabledSet return true if telemetry_enabled has been set.

func IsTrue

func IsTrue(s string) bool

func List

func List() []string

List returns the names of available profiles.

func Name

func Name() string

func OpsManagerURL

func OpsManagerURL() string

OpsManagerURL get configured ops manager base url.

func OrgID

func OrgID() string

OrgID get configured organization ID.

func Output

func Output() string

Output get configured output format.

func Path

func Path(f string) (string, error)

func PrivateAPIKey

func PrivateAPIKey() string

PrivateAPIKey get configured private api key.

func ProfileProperties

func ProfileProperties() []string

func ProjectID

func ProjectID() string

ProjectID get configured project ID.

func PublicAPIKey

func PublicAPIKey() string

PublicAPIKey get configured public api key.

func RefreshToken

func RefreshToken() string

RefreshToken get configured refresh token.

func Rename

func Rename(newProfileName string) error

Rename replaces the Profile to a new Profile name, overwriting any Profile that existed before.

func Save

func Save() error

Save the configuration to disk.

func Service

func Service() string

Service get configured service.

func Set

func Set(name string, value any)

func SetAPIVersion

func SetAPIVersion(v string)

SetAPIVersion sets the default API version.

func SetAccessToken

func SetAccessToken(v string)

SetAccessToken set configured access token.

func SetAuthType

func SetAuthType(v AuthMechanism)

SetAuthType sets the configured auth type.

func SetClientID

func SetClientID(v string)

SetClientID set configured client ID.

func SetClientSecret

func SetClientSecret(v string)

SetClientSecret set configured client secret.

func SetDefaultProfile

func SetDefaultProfile(profile *Profile)

func SetGlobal

func SetGlobal(name string, value any)

func SetLocalDeploymentImage

func SetLocalDeploymentImage(v string)

SetLocalDeploymentImage sets the MongoDB Docker image URL.

func SetName

func SetName(name string) error

func SetOpsManagerURL

func SetOpsManagerURL(v string)

SetOpsManagerURL set configured ops manager base url.

func SetOrgID

func SetOrgID(v string)

SetOrgID sets the global organization ID.

func SetOutput

func SetOutput(v string)

SetOutput sets the global output format.

func SetPrivateAPIKey

func SetPrivateAPIKey(v string)

SetPrivateAPIKey set configured private api key.

func SetProfile

func SetProfile(profile *Profile)

Workaround to keep existing code working We cannot set the profile immediately because of a race condition which breaks all the unit tests

The goal is to get rid of this, but we will need to do this gradually, since it's a large change that affects almost every command

func SetProjectID

func SetProjectID(v string)

SetProjectID sets the global project ID.

func SetPublicAPIKey

func SetPublicAPIKey(v string)

SetPublicAPIKey set configured publicAPIKey.

func SetRefreshToken

func SetRefreshToken(v string)

SetRefreshToken set configured refresh token.

func SetService

func SetService(v string)

SetService set configured service.

func SetSilenceStorageWarning

func SetSilenceStorageWarning(v bool)

SetSilenceStorageWarning sets the global silence storage warning.

func SetSkipUpdateCheck

func SetSkipUpdateCheck(v bool)

SetSkipUpdateCheck sets the global skip update check.

func SetTelemetryEnabled

func SetTelemetryEnabled(v bool)

SetTelemetryEnabled sets the telemetry enabled value.

func SilenceStorageWarning

func SilenceStorageWarning() bool

SilenceStorageWarning get the global silence storage warning.

func SkipUpdateCheck

func SkipUpdateCheck() bool

SkipUpdateCheck get the global skip update check.

func TelemetryEnabled

func TelemetryEnabled() bool

TelemetryEnabled get the configured telemetry enabled value.

func Token

func Token() (*auth.Token, error)

Token gets configured auth.Token.

func UserAgent

func UserAgent(version string) string

func ViperConfigStoreFilename

func ViperConfigStoreFilename(configDir string) string

ViperConfigStoreFilename returns the full path to the configuration file.

func WithProfile

func WithProfile(ctx context.Context, profile *Profile) context.Context

Setting a value

Types

type AuthMechanism

type AuthMechanism string
const (
	APIKeys        AuthMechanism = "api_keys"
	UserAccount    AuthMechanism = "user_account"
	ServiceAccount AuthMechanism = "service_account"
	NoAuth         AuthMechanism = "no_auth"
)

func AuthType

func AuthType() AuthMechanism

AuthType determines the auth type, prioritizing credentials set via environment variables. It first retrieves the auth type configured in the profile. If programmatic credentials that do not match the profile's auth type are detected, AuthType infers that these credentials were set via environment variables and returns the corresponding auth type. This assumes users will not explicitly export the auth type variable.

type InMemoryStore

type InMemoryStore struct {
	// contains filtered or unexported fields
}

Temporary InMemoryStore to mimick legacy behavior Will be removed by CLOUDP-339855 when we get rid of static references in the profile

func NewInMemoryStore

func NewInMemoryStore() *InMemoryStore

NewInMemoryStore creates a new InMemoryStore instance with an initialized Viper configuration. This store is used as a temporary workaround for unit tests that expect profiles to be available before they are actually configured, preventing nil pointer references.

func (*InMemoryStore) DeleteProfile

func (*InMemoryStore) DeleteProfile(_ string) error

DeleteProfile is not implemented for InMemoryStore and will panic if called. This functionality is intended for persistent existing tests.

func (*InMemoryStore) GetGlobalValue

func (s *InMemoryStore) GetGlobalValue(propertyName string) any

GetGlobalValue retrieves a global configuration property value.

func (*InMemoryStore) GetHierarchicalValue

func (s *InMemoryStore) GetHierarchicalValue(profileName string, propertyName string) any

GetHierarchicalValue retrieves a property value with hierarchical precedence. It first checks for global properties, then falls back to profile-specific settings.

func (*InMemoryStore) GetProfileNames

func (s *InMemoryStore) GetProfileNames() []string

GetProfileNames returns a sorted list of all profile names stored in the configuration.

func (*InMemoryStore) GetProfileStringMap

func (s *InMemoryStore) GetProfileStringMap(profileName string) map[string]string

GetProfileStringMap returns all configuration properties for a profile as a string map.

func (*InMemoryStore) GetProfileValue

func (s *InMemoryStore) GetProfileValue(profileName string, propertyName string) any

GetProfileValue retrieves a property value from a specific profile's configuration.

func (*InMemoryStore) IsSecure

func (*InMemoryStore) IsSecure() bool

IsSecure returns true to indicate this store treats data as secure, even though it's stored in memory. This maintains compatibility with the Store interface.

func (*InMemoryStore) IsSetGlobal

func (s *InMemoryStore) IsSetGlobal(propertyName string) bool

IsSetGlobal checks whether a global configuration property has been set.

func (*InMemoryStore) RenameProfile

func (*InMemoryStore) RenameProfile(_, _ string) error

RenameProfile is not implemented for InMemoryStore and will panic if called. This functionality is intended for persistent existing tests.

func (*InMemoryStore) Save

func (*InMemoryStore) Save() error

Save is a no-op for InMemoryStore since data is only stored in memory and doesn't need to be persisted to disk.

func (*InMemoryStore) SetGlobalValue

func (s *InMemoryStore) SetGlobalValue(propertyName string, value any)

SetGlobalValue sets a global configuration property that applies across all profiles.

func (*InMemoryStore) SetProfileValue

func (s *InMemoryStore) SetProfileValue(profileName string, propertyName string, value any)

SetProfileValue sets a property value for a specific profile, creating or updating the profile's configuration map as needed.

type Profile

type Profile struct {
	// contains filtered or unexported fields
}

func Default

func Default() *Profile

func LoadAtlasCLIConfig

func LoadAtlasCLIConfig() (*Profile, error)

LoadAtlasCLIConfig loads configuration using the maximum supported version, ensuring plugins always use the latest AtlasCLI configuration schema.

func LoadAtlasCLIConfigWithVersion

func LoadAtlasCLIConfigWithVersion(expectedVersion int64) (*Profile, error)

LoadAtlasCLIConfigWithVersion loads configuration with validation for specified version. The expectedVersion parameter enforces compatibility by rejecting configs that are newer or older than expected.

func NewProfile

func NewProfile(name string, configStore Store) *Profile

func ProfileFromContext

func ProfileFromContext(ctx context.Context) (*Profile, bool)

Getting a value

func (*Profile) APIVersion

func (p *Profile) APIVersion() string

func (*Profile) AccessToken

func (p *Profile) AccessToken() string

func (*Profile) AccessTokenSubject

func (p *Profile) AccessTokenSubject() (string, error)

func (*Profile) AccountURL

func (p *Profile) AccountURL() string

func (*Profile) AuthType

func (p *Profile) AuthType() AuthMechanism

func (*Profile) ClientID

func (p *Profile) ClientID() string

func (*Profile) ClientSecret

func (p *Profile) ClientSecret() string

func (*Profile) Delete

func (p *Profile) Delete() error

func (*Profile) Get

func (p *Profile) Get(name string) any

func (*Profile) GetBool

func (p *Profile) GetBool(name string) bool

func (*Profile) GetBoolWithDefault

func (p *Profile) GetBoolWithDefault(name string, defaultValue bool) bool

func (*Profile) GetInt64

func (p *Profile) GetInt64(name string) int64

func (*Profile) GetLocalDeploymentImage

func (p *Profile) GetLocalDeploymentImage() string

func (*Profile) GetString

func (p *Profile) GetString(name string) string

func (*Profile) IsAccessSet

func (p *Profile) IsAccessSet() bool

func (*Profile) IsTelemetryEnabledSet

func (p *Profile) IsTelemetryEnabledSet() bool

func (*Profile) List

func (p *Profile) List() []string

func (*Profile) Name

func (p *Profile) Name() string

func (*Profile) OpsManagerURL

func (p *Profile) OpsManagerURL() string

func (*Profile) OrgID

func (p *Profile) OrgID() string

func (*Profile) Output

func (p *Profile) Output() string

func (*Profile) PrivateAPIKey

func (p *Profile) PrivateAPIKey() string

func (*Profile) ProjectID

func (p *Profile) ProjectID() string

func (*Profile) PublicAPIKey

func (p *Profile) PublicAPIKey() string

func (*Profile) RefreshToken

func (p *Profile) RefreshToken() string

func (*Profile) Rename

func (p *Profile) Rename(newProfileName string) error

func (*Profile) Save

func (p *Profile) Save() error

func (*Profile) Service

func (p *Profile) Service() string

func (*Profile) Set

func (p *Profile) Set(name string, value any)

func (*Profile) SetAPIVersion

func (p *Profile) SetAPIVersion(v string)

func (*Profile) SetAccessToken

func (p *Profile) SetAccessToken(v string)

func (*Profile) SetAuthType

func (p *Profile) SetAuthType(v AuthMechanism)

func (*Profile) SetClientID

func (p *Profile) SetClientID(v string)

func (*Profile) SetClientSecret

func (p *Profile) SetClientSecret(v string)

func (*Profile) SetGlobal

func (p *Profile) SetGlobal(name string, value any)

func (*Profile) SetLocalDeploymentImage

func (*Profile) SetLocalDeploymentImage(v string)

func (*Profile) SetName

func (p *Profile) SetName(name string) error

func (*Profile) SetOpsManagerURL

func (p *Profile) SetOpsManagerURL(v string)

func (*Profile) SetOrgID

func (p *Profile) SetOrgID(v string)

func (*Profile) SetOutput

func (p *Profile) SetOutput(v string)

func (*Profile) SetPrivateAPIKey

func (p *Profile) SetPrivateAPIKey(v string)

func (*Profile) SetProjectID

func (p *Profile) SetProjectID(v string)

func (*Profile) SetPublicAPIKey

func (p *Profile) SetPublicAPIKey(v string)

func (*Profile) SetRefreshToken

func (p *Profile) SetRefreshToken(v string)

func (*Profile) SetService

func (p *Profile) SetService(v string)

func (*Profile) SetSilenceStorageWarning

func (*Profile) SetSilenceStorageWarning(v bool)

func (*Profile) SetSkipUpdateCheck

func (*Profile) SetSkipUpdateCheck(v bool)

func (*Profile) SetTelemetryEnabled

func (*Profile) SetTelemetryEnabled(v bool)

func (*Profile) SilenceStorageWarning

func (p *Profile) SilenceStorageWarning() bool

func (*Profile) SkipUpdateCheck

func (p *Profile) SkipUpdateCheck() bool

func (*Profile) TelemetryEnabled

func (p *Profile) TelemetryEnabled() bool

func (*Profile) Token

func (p *Profile) Token() (*auth.Token, error)

type ProxyStore

type ProxyStore struct {
	// contains filtered or unexported fields
}

ProxyStore routes properties between secure and insecure stores based on property type.

func (*ProxyStore) DeleteProfile

func (p *ProxyStore) DeleteProfile(profileName string) error

DeleteProfile delegates to the insecure store for profile management.

func (*ProxyStore) GetGlobalValue

func (p *ProxyStore) GetGlobalValue(propertyName string) any

GetGlobalValue routes to secure or insecure store based on property type.

func (*ProxyStore) GetHierarchicalValue

func (p *ProxyStore) GetHierarchicalValue(profileName string, propertyName string) any

GetHierarchicalValue routes to secure or insecure store based on property type. For secure properties, it first checks the insecure store for a value, in the case that environment variables are used. If no value is found, it will proceed with secure store.

func (*ProxyStore) GetProfileNames

func (p *ProxyStore) GetProfileNames() []string

GetProfileNames returns profile names from the insecure store.

func (*ProxyStore) GetProfileStringMap

func (p *ProxyStore) GetProfileStringMap(profileName string) map[string]string

GetProfileStringMap returns insecure properties only, excluding secure values.

func (*ProxyStore) GetProfileValue

func (p *ProxyStore) GetProfileValue(profileName string, propertyName string) any

GetProfileValue routes to secure or insecure store based on property type.

func (*ProxyStore) IsSecure

func (*ProxyStore) IsSecure() bool

IsSecure returns true as ProxyStore provides secure storage capabilities.

func (*ProxyStore) IsSetGlobal

func (p *ProxyStore) IsSetGlobal(propertyName string) bool

IsSetGlobal checks only insecure store for global property existence as no secure properties are global

func (*ProxyStore) RenameProfile

func (p *ProxyStore) RenameProfile(oldProfileName string, newProfileName string) error

RenameProfile delegates to the insecure store for profile management.

func (*ProxyStore) Save

func (p *ProxyStore) Save() error

Save persists both secure and insecure stores, collecting any errors.

func (*ProxyStore) SetGlobalValue

func (p *ProxyStore) SetGlobalValue(propertyName string, value any)

SetGlobalValue routes to secure or insecure store based on property type.

func (*ProxyStore) SetProfileValue

func (p *ProxyStore) SetProfileValue(profileName string, propertyName string, value any)

SetProfileValue routes to secure or insecure store based on property type.

type SecureStore

type SecureStore interface {
	Available() bool
	Save() error

	Set(profileName string, propertyName string, value string)
	Get(profileName string, propertyName string) string
	DeleteKey(profileName string, propertyName string)
	DeleteProfile(profileName string)
}

type Store

type Store interface {
	IsSecure() bool
	Save() error

	GetProfileNames() []string
	RenameProfile(oldProfileName string, newProfileName string) error
	DeleteProfile(profileName string) error

	GetHierarchicalValue(profileName string, propertyName string) any

	SetProfileValue(profileName string, propertyName string, value any)
	GetProfileValue(profileName string, propertyName string) any
	GetProfileStringMap(profileName string) map[string]string

	SetGlobalValue(propertyName string, value any)
	GetGlobalValue(propertyName string) any
	IsSetGlobal(propertyName string) bool
}

func NewDefaultStore

func NewDefaultStore() (Store, error)

NewDefaultStore creates a store with default filesystem and secure storage if available.

func NewStore

func NewStore(insecureStore Store, secureStore SecureStore) Store

NewStore creates a ProxyStore if secure storage is available, otherwise returns insecure store.

func NewStoreWithEnvOption

func NewStoreWithEnvOption(loadEnvVars bool) (Store, error)

NewStoreWithEnvOption creates a store with default filesystem and secure storage if available. It will load environment variables according to the loadEnvVars input.

type ViperConfigStore

type ViperConfigStore struct {
	// contains filtered or unexported fields
}

ViperConfigStore implements the config.Store interface

func NewViperStore

func NewViperStore(fs afero.Fs, loadEnvVars bool) (*ViperConfigStore, error)

ViperConfigStore specific methods NewViperStore creates a ViperConfigStore with filesystem, config paths, and optional environment variable loading.

func (*ViperConfigStore) DeleteProfile

func (s *ViperConfigStore) DeleteProfile(profileName string) error

DeleteProfile removes profile from configuration using direct TOML manipulation.

func (*ViperConfigStore) Filename

func (s *ViperConfigStore) Filename() string

Filename returns this store's configuration file path.

func (*ViperConfigStore) GetGlobalValue

func (s *ViperConfigStore) GetGlobalValue(propertyName string) any

GetGlobalValue retrieves global configuration property.

func (*ViperConfigStore) GetHierarchicalValue

func (s *ViperConfigStore) GetHierarchicalValue(profileName string, propertyName string) any

GetHierarchicalValue checks global settings first, then profile-specific values.

func (*ViperConfigStore) GetProfileNames

func (s *ViperConfigStore) GetProfileNames() []string

GetProfileNames returns all profile names in the configuration file.

func (*ViperConfigStore) GetProfileStringMap

func (s *ViperConfigStore) GetProfileStringMap(profileName string) map[string]string

GetProfileStringMap returns all profile properties as string map.

func (*ViperConfigStore) GetProfileValue

func (s *ViperConfigStore) GetProfileValue(profileName string, propertyName string) any

GetProfileValue retrieves property value from profile configuration.

func (*ViperConfigStore) IsSecure

func (*ViperConfigStore) IsSecure() bool

IsSecure returns false as this store uses plaintext TOML files.

func (*ViperConfigStore) IsSetGlobal

func (s *ViperConfigStore) IsSetGlobal(propertyName string) bool

IsSetGlobal checks if global property is set.

func (*ViperConfigStore) RenameProfile

func (s *ViperConfigStore) RenameProfile(oldProfileName string, newProfileName string) error

RenameProfile copies profile configuration to new name and deletes old one.

func (*ViperConfigStore) Save

func (s *ViperConfigStore) Save() error

Save persists configuration to disk, creating directories if needed.

func (*ViperConfigStore) SetGlobalValue

func (s *ViperConfigStore) SetGlobalValue(propertyName string, value any)

SetGlobalValue sets global configuration property.

func (*ViperConfigStore) SetProfileValue

func (s *ViperConfigStore) SetProfileValue(profileName string, propertyName string, value any)

SetProfileValue sets profile property.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL