Documentation
¶
Overview ¶
Package profile stores the CLI configuration for the named profile.
The profile stores common configuration values such as the organization and hostname when running commands. It also stores user settable values that customize CLI output, like disabling color.
Index ¶
- Constants
- Variables
- func PropertyNames() map[string]struct{}
- type ActiveProfile
- type CheckRefreshFunc
- type FileID
- type HostCacheLoader
- type Loader
- func (l *Loader) DefaultActiveProfile() *ActiveProfile
- func (l *Loader) DefaultProfile() *Profile
- func (l *Loader) DeleteProfile(name string) error
- func (l *Loader) GetActiveProfile() (*ActiveProfile, error)
- func (l *Loader) GetDeviceID(ctx context.Context) string
- func (l *Loader) ListProfiles() ([]string, error)
- func (l *Loader) LoadProfile(name string) (*Profile, error)
- func (l *Loader) LoadProfiles() ([]*Profile, error)
- func (l *Loader) NewProfile(name string) (*Profile, error)
- type Profile
- func (p *Profile) Clean()
- func (p *Profile) GetHostname() string
- func (p *Profile) GetTelemetry() string
- func (p *Profile) GetToken() string
- func (p *Profile) HostCache(ctx context.Context) (*HostCacheLoader, error)
- func (p *Profile) Predict(args complete.Args) []string
- func (p *Profile) SetDefaultOrganization(name string) *Profile
- func (p Profile) String() string
- func (p *Profile) Validate() error
- func (p *Profile) Write() error
- type RefreshResult
Constants ¶
const ( // ProfileDir is the directory that contains CLI configuration profiles. ProfileDir = "profiles/" // ProfileNameDefault is the default profile name. ProfileNameDefault = "default" // TerraformCredentialsPath is the path to the terraform credentials file that we will check for // tokens if they're not set in the profiler. TerraformCredentialsPath = "~/.terraform.d/credentials.tfrc.json" // DefaultHostname is the default hostname to use if one is not set in the profile or environment variable. DefaultHostname = "app.terraform.io" )
const ( // ActiveProfileFileName is the file name of the active profile stored in // the ConfigDir. ActiveProfileFileName = "active_profile.hcl" // DeviceIDFileName is the file name of the uuid used to identify this CLI installation for // telemetry purposes, stored in the ConfigDir. DeviceIDFileName = "device_id" )
Variables ¶
var ( // ErrNoActiveProfileFilePresent is returned if no active profile file // exists. ErrNoActiveProfileFilePresent = errors.New("active profile file doesn't exist") // ErrActiveProfileFileEmpty is returned if the active profile file is // empty. ErrActiveProfileFileEmpty = errors.New("active profile is unset") )
var ( // ErrNoProfileFilePresent is returned when the requested profile does not // exist. ErrNoProfileFilePresent = errors.New("profile configuration file doesn't exist") // ErrInvalidProfileName is returned if a profile is created with an invalid // profile name. ErrInvalidProfileName = errors.New("profile name may only include a-z, A-Z, 0-9, or '_', must start with a letter, and can be no longer than 64 characters") )
var ( // ConfigDir is the directory that contains CLI configuration. ConfigDir = fmt.Sprintf("~/.config/%s/", version.Name) )
var ErrInvalidFileID = errors.New("invalid file ID: must be a file name without path components")
ErrInvalidFileID is returned when a FileID contains path components, is a directory, or is otherwise invalid.
Functions ¶
func PropertyNames ¶
func PropertyNames() map[string]struct{}
PropertyNames returns the name of the properties in a profile. If the property is in a struct, such as Core, the property name will be <struct_name>/<property_name>, such as "core/no_color".
Types ¶
type ActiveProfile ¶
type ActiveProfile struct {
Name string `hcl:"name"`
// contains filtered or unexported fields
}
ActiveProfile stores the active profile.
func (*ActiveProfile) Write ¶
func (c *ActiveProfile) Write() error
Write writes the active profile to disk.
type CheckRefreshFunc ¶
type CheckRefreshFunc func(mTime *time.Time) RefreshResult
CheckRefreshFunc checks the upstream source for new data, returning nil if the cached data is still valid, or the new data if it is not. Return an error if the refresh check fails.
type FileID ¶
type FileID string
FileID is an identifier for a specific cache file. It should only contain the file name and no path components.
type HostCacheLoader ¶
type HostCacheLoader struct {
// contains filtered or unexported fields
}
HostCacheLoader is responsible for loading and refreshing cached data for a host.
func NewHostCacheLoader ¶
func NewHostCacheLoader(ctx context.Context, baseDir, hostname string) (*HostCacheLoader, error)
NewHostCacheLoader creates a new HostCacheLoader for the given hostname, using the provided logger for logging.
func (HostCacheLoader) ReadOrRefresh ¶
func (h HostCacheLoader) ReadOrRefresh(fileID FileID, check CheckRefreshFunc) ([]byte, error)
ReadOrRefresh checks if the cached data for the given fileID is still valid by calling the provided refresh function with the modification time of the cached data. If the refresh function returns nil data, the cached data is still valid and will be returned. If the refresh function returns new data, it will be written to the cache and returned. An error is returned if there is an issue checking the cache, refreshing the data, or writing new data to the cache.
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader is used to load and interact with profiles on disk.
func TestLoader ¶
TestLoader returns a Loader suitable for testing. All profiles that are accessed will be in the context of a temporary directory.
func (*Loader) DefaultActiveProfile ¶
func (l *Loader) DefaultActiveProfile() *ActiveProfile
DefaultActiveProfile returns an active profile set to default.
func (*Loader) DefaultProfile ¶
DefaultProfile returns the minimal default profile. If environment variables related to organization and project are set, they are honored here.
func (*Loader) DeleteProfile ¶
DeleteProfile deletes the profile with the given name. If the profile can not be found, ErrNoProfileFilePresent will be returned. Otherwise, an error will be returned if the profile can not be deleted for any other reason..
func (*Loader) GetActiveProfile ¶
func (l *Loader) GetActiveProfile() (*ActiveProfile, error)
GetActiveProfile returns the current profile.
func (*Loader) GetDeviceID ¶
GetDeviceID returns the unique identifier for this CLI installation, used for telemetry purposes.
func (*Loader) ListProfiles ¶
ListProfiles returns the available profile names.
func (*Loader) LoadProfile ¶
LoadProfile loads a profile given its name. If the profile can not be found, ErrNoProfileFilePresent will be returned. Otherwise, an error will be returned if the profile is invalid.
func (*Loader) LoadProfiles ¶
LoadProfiles loads all the available profiles.
type Profile ¶
type Profile struct {
// Name is the name of the profile
Name string `hcl:"name"`
// DefaultOrganization stores the default organization to make requests against.
DefaultOrganization string `hcl:"default_organization"`
// NoColor disables color output
NoColor *bool `hcl:"no_color,optional" json:",omitempty"`
// Hostname is the profile's configured hostname for API requests. If not set, the default is app.terraform.io.
Hostname string `hcl:"hostname,optional" json:",omitempty"`
// Token is the API token to use for API requests. If not set, the CLI will look for the token in the environment or terraform credentials.
Token string `hcl:"token,optional" json:",omitempty"`
// Telemetry controls telemetry behavior. Values: "false"/"disabled" to disable,
// "log" to write spans to stderr, or any other value (including empty) to enable OTLP export.
Telemetry *string `hcl:"telemetry,optional" json:",omitempty"`
// contains filtered or unexported fields
}
Profile is a named set of configuration for the CLI. It captures common configuration values such as the organization and project being interacted with, but also allows storing service specific configuration.
func TestProfile ¶
TestProfile returns a profile appropriate for use during testing. If interacting with more than one profile, prefer using TestLoader.
func (*Profile) GetHostname ¶
GetHostname returns the set hostname or the default hostname if it has not been configured.
func (*Profile) GetTelemetry ¶
GetTelemetry returns the telemetry setting or an empty string if unset.
func (*Profile) GetToken ¶
GetToken returns the token set on the profile, or the token extracted from the environment if one is available.
func (*Profile) HostCache ¶
func (p *Profile) HostCache(ctx context.Context) (*HostCacheLoader, error)
HostCache returns a HostCacheLoader for the profile, which can be used to read and write host-specific cache files.
func (*Profile) SetDefaultOrganization ¶
SetDefaultOrganization sets the default organization.