config

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2025 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ConfigVersion is the current version of the config file format
	ConfigVersion = 1
)

Variables

View Source
var (
	// ErrProfileNotFound is returned when a profile doesn't exist
	ErrProfileNotFound = errors.New("profile not found")
)
View Source
var LoginFunc = func(ctx context.Context) (*megaport.Client, error) {

	var accessKey, secretKey, env string

	manager, err := NewConfigManager()
	if err == nil {
		profile, _, err := manager.GetCurrentProfile()
		if err == nil {

			accessKey = profile.AccessKey
			secretKey = profile.SecretKey
			env = profile.Environment
		}
	}

	if accessKey == "" {
		accessKey = os.Getenv("MEGAPORT_ACCESS_KEY")
	}
	if secretKey == "" {
		secretKey = os.Getenv("MEGAPORT_SECRET_KEY")
	}
	if env == "" {
		env = os.Getenv("MEGAPORT_ENVIRONMENT")
	}

	if accessKey == "" {
		return nil, fmt.Errorf("megaport API access key not provided. Configure an active profile or set MEGAPORT_ACCESS_KEY environment variable")
	}
	if secretKey == "" {
		return nil, fmt.Errorf("megaport API secret key not provided. Configure an active profile or set MEGAPORT_SECRET_KEY environment variable")
	}

	if env == "" {
		env = "production"
	}

	var envOpt megaport.ClientOpt
	switch env {
	case "production":
		envOpt = megaport.WithEnvironment(megaport.EnvironmentProduction)
	case "staging":
		envOpt = megaport.WithEnvironment(megaport.EnvironmentStaging)
	case "development":
		envOpt = megaport.WithEnvironment(megaport.EnvironmentDevelopment)
	default:
		envOpt = megaport.WithEnvironment(megaport.EnvironmentProduction)
	}

	httpClient := &http.Client{}

	megaportClient, err := megaport.New(httpClient, megaport.WithCredentials(accessKey, secretKey), envOpt)
	if err != nil {
		return nil, err
	}
	if _, err := megaportClient.Authorize(ctx); err != nil {
		return nil, err
	}
	return megaportClient, nil
}

Login logs into the Megaport API using the current profile or environment variables.

Functions

func AddCommandsTo

func AddCommandsTo(rootCmd *cobra.Command)

AddCommandsTo builds the config commands and adds them to the root command

func ClearDefaults

func ClearDefaults(cmd *cobra.Command, args []string, noColor bool) error

ClearDefaults removes all default settings

func CreateProfile

func CreateProfile(cmd *cobra.Command, args []string, noColor bool) error

CreateProfile creates a new profile

func DeleteProfile

func DeleteProfile(cmd *cobra.Command, args []string, noColor bool) error

DeleteProfile deletes a profile

func ExportConfig

func ExportConfig(cmd *cobra.Command, args []string, noColor bool) error

ExportConfig exports the configuration

func GetConfigDir

func GetConfigDir() (string, error)

GetConfigDir returns the directory where the config file is stored

func GetConfigFilePath

func GetConfigFilePath() (string, error)

GetConfigFilePath returns the path to the config file

func GetDefault

func GetDefault(cmd *cobra.Command, args []string, noColor bool) error

GetDefault gets a default value

func ImportConfig

func ImportConfig(cmd *cobra.Command, args []string, noColor bool) error

ImportConfig imports configuration from a file

func ListProfiles

func ListProfiles(cmd *cobra.Command, args []string, noColor bool, outputFormat string) error

ListProfiles lists all profiles

func Login

func Login(ctx context.Context) (*megaport.Client, error)

func RemoveDefault

func RemoveDefault(cmd *cobra.Command, args []string, noColor bool) error

RemoveDefault removes a default setting

func SetDefault

func SetDefault(cmd *cobra.Command, args []string, noColor bool) error

SetDefault sets a default value

func UpdateProfile

func UpdateProfile(cmd *cobra.Command, args []string, noColor bool) error

UpdateProfile updates an existing profile

func UseProfile

func UseProfile(cmd *cobra.Command, args []string, noColor bool) error

UseProfile sets the active profile

func ViewConfig

func ViewConfig(cmd *cobra.Command, args []string, noColor bool) error

ViewConfig displays the current configuration

Types

type ConfigFile

type ConfigFile struct {
	Version       int                    `json:"version"`
	ActiveProfile string                 `json:"active_profile"`
	Profiles      map[string]*Profile    `json:"profiles"`
	Defaults      map[string]interface{} `json:"defaults"`
}

ConfigFile represents the structure of the configuration file

func NewConfigFile

func NewConfigFile() *ConfigFile

NewConfigFile creates a new configuration file with default values

type ConfigManager

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

ConfigManager provides methods for managing the configuration

func NewConfigManager

func NewConfigManager() (*ConfigManager, error)

NewConfigManager creates a new config manager

func (*ConfigManager) ClearDefaults

func (m *ConfigManager) ClearDefaults() error

func (*ConfigManager) CreateProfile

func (m *ConfigManager) CreateProfile(name, accessKey, secretKey, environment, description string) error

CreateProfile creates a new profile with the given credentials and settings

func (*ConfigManager) DeleteProfile

func (m *ConfigManager) DeleteProfile(name string) error

DeleteProfile deletes a profile

func (*ConfigManager) Export

func (m *ConfigManager) Export() (*ConfigFile, error)

Export exports the configuration (excluding sensitive data)

func (*ConfigManager) GetCurrentProfile

func (m *ConfigManager) GetCurrentProfile() (*Profile, string, error)

GetCurrentProfile returns the currently active profile

func (*ConfigManager) GetDefault

func (m *ConfigManager) GetDefault(key string) (interface{}, bool)

GetDefault gets a default value from config

func (*ConfigManager) ListProfiles

func (m *ConfigManager) ListProfiles() (map[string]*Profile, error)

ListProfiles returns all profiles

func (*ConfigManager) RemoveDefault

func (m *ConfigManager) RemoveDefault(key string) error

func (*ConfigManager) Save

func (m *ConfigManager) Save() error

Save saves the configuration

func (*ConfigManager) SetDefault

func (m *ConfigManager) SetDefault(key string, value interface{}) error

SetDefault sets a default value in config

func (*ConfigManager) UpdateProfile

func (m *ConfigManager) UpdateProfile(name, accessKey, secretKey, environment string, updateDescription bool, description string) error

UpdateProfile updates an existing profile

func (*ConfigManager) UseProfile

func (m *ConfigManager) UseProfile(name string) error

UseProfile sets the active profile

type Module

type Module struct{}

Module implements the cmdbuilder.Module interface for config

func NewModule

func NewModule() *Module

NewModule creates a new config module

func (*Module) Name

func (m *Module) Name() string

Name returns the module name

func (*Module) RegisterCommands

func (m *Module) RegisterCommands(rootCmd *cobra.Command)

RegisterCommands adds the config command to the root command

type Profile

type Profile struct {
	AccessKey   string `json:"access_key"`
	SecretKey   string `json:"secret_key"`
	Environment string `json:"environment"`           // "production", "staging", or "development"
	Description string `json:"description,omitempty"` // Optional description
}

Profile stores authentication and environment settings

type ProfileOutput

type ProfileOutput struct {
	output.Output `json:"-" header:"-"`
	Name          string `json:"name" header:"Name"`
	AccessKey     string `json:"access_key" header:"Access Key"`
	Environment   string `json:"environment" header:"Environment"`
	Description   string `json:"description" header:"Description"`
	IsActive      bool   `json:"is_active" header:"Active"`
}

ProfileOutput represents the output format for profiles

Jump to

Keyboard shortcuts

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