profile

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MPL-2.0 Imports: 26 Imported by: 0

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

View Source
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"
)
View Source
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

View Source
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")
)
View Source
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")
)
View Source
var (
	// ConfigDir is the directory that contains CLI configuration.
	ConfigDir = fmt.Sprintf("~/.config/%s/", version.Name)
)
View Source
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.

func (HostCacheLoader) Write

func (h HostCacheLoader) Write(fileID FileID, data []byte, lastModified *time.Time) error

Write writes the given data to the cache with the given fileID, overwriting any existing data.

type Loader

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

Loader is used to load and interact with profiles on disk.

func NewLoader

func NewLoader() (*Loader, error)

NewLoader returns a new loader or an error if the loader can't be instantiated.

func TestLoader

func TestLoader(t *testing.T) *Loader

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

func (l *Loader) DefaultProfile() *Profile

DefaultProfile returns the minimal default profile. If environment variables related to organization and project are set, they are honored here.

func (*Loader) DeleteProfile

func (l *Loader) DeleteProfile(name string) error

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

func (l *Loader) GetDeviceID(ctx context.Context) string

GetDeviceID returns the unique identifier for this CLI installation, used for telemetry purposes.

func (*Loader) ListProfiles

func (l *Loader) ListProfiles() ([]string, error)

ListProfiles returns the available profile names.

func (*Loader) LoadProfile

func (l *Loader) LoadProfile(name string) (*Profile, error)

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

func (l *Loader) LoadProfiles() ([]*Profile, error)

LoadProfiles loads all the available profiles.

func (*Loader) NewProfile

func (l *Loader) NewProfile(name string) (*Profile, error)

NewProfile returns an new profile with defaults.

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

func TestProfile(t *testing.T) *Profile

TestProfile returns a profile appropriate for use during testing. If interacting with more than one profile, prefer using TestLoader.

func (*Profile) Clean

func (p *Profile) Clean()

Clean nils any empty component.

func (*Profile) GetHostname

func (p *Profile) GetHostname() string

GetHostname returns the set hostname or the default hostname if it has not been configured.

func (*Profile) GetTelemetry

func (p *Profile) GetTelemetry() string

GetTelemetry returns the telemetry setting or an empty string if unset.

func (*Profile) GetToken

func (p *Profile) GetToken() string

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) Predict

func (p *Profile) Predict(args complete.Args) []string

Predict predicts the HCL key names and basic settable values.

func (*Profile) SetDefaultOrganization

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

SetDefaultOrganization sets the default organization.

func (Profile) String

func (p Profile) String() string

String returns an HCL formatted string representation of the profile.

func (*Profile) Validate

func (p *Profile) Validate() error

Validate validates that the set values are valid. It validates parameters that do not require any communication with HCP.

func (*Profile) Write

func (p *Profile) Write() error

Write writes the profile to disk.

type RefreshResult

type RefreshResult struct {
	DataIfNew    []byte
	Err          error
	LastModified *time.Time
}

RefreshResult represents the result of a network refresh check, including the new data (if any).

Jump to

Keyboard shortcuts

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