Documentation
¶
Index ¶
- Constants
- Variables
- func AddCommandsTo(rootCmd *cobra.Command)
- func ClearDefaults(cmd *cobra.Command, args []string, noColor bool) error
- func CreateProfile(cmd *cobra.Command, args []string, noColor bool) error
- func DeleteProfile(cmd *cobra.Command, args []string, noColor bool) error
- func ExportConfig(cmd *cobra.Command, args []string, noColor bool) error
- func GetConfigDir() (string, error)
- func GetConfigFilePath() (string, error)
- func GetDefault(cmd *cobra.Command, args []string, noColor bool) error
- func ImportConfig(cmd *cobra.Command, args []string, noColor bool) error
- func ListProfiles(cmd *cobra.Command, args []string, noColor bool, outputFormat string) error
- func Login(ctx context.Context) (*megaport.Client, error)
- func RemoveDefault(cmd *cobra.Command, args []string, noColor bool) error
- func SetDefault(cmd *cobra.Command, args []string, noColor bool) error
- func UpdateProfile(cmd *cobra.Command, args []string, noColor bool) error
- func UseProfile(cmd *cobra.Command, args []string, noColor bool) error
- func ViewConfig(cmd *cobra.Command, args []string, noColor bool) error
- type ConfigFile
- type ConfigManager
- func (m *ConfigManager) ClearDefaults() error
- func (m *ConfigManager) CreateProfile(name, accessKey, secretKey, environment, description string) error
- func (m *ConfigManager) DeleteProfile(name string) error
- func (m *ConfigManager) Export() (*ConfigFile, error)
- func (m *ConfigManager) GetCurrentProfile() (*Profile, string, error)
- func (m *ConfigManager) GetDefault(key string) (interface{}, bool)
- func (m *ConfigManager) ListProfiles() (map[string]*Profile, error)
- func (m *ConfigManager) RemoveDefault(key string) error
- func (m *ConfigManager) Save() error
- func (m *ConfigManager) SetDefault(key string, value interface{}) error
- func (m *ConfigManager) UpdateProfile(name, accessKey, secretKey, environment string, updateDescription bool, ...) error
- func (m *ConfigManager) UseProfile(name string) error
- type Module
- type Profile
- type ProfileOutput
Constants ¶
const (
// ConfigVersion is the current version of the config file format
ConfigVersion = 1
)
Variables ¶
var ( // ErrProfileNotFound is returned when a profile doesn't exist ErrProfileNotFound = errors.New("profile not found") )
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 ¶
AddCommandsTo builds the config commands and adds them to the root command
func ClearDefaults ¶
ClearDefaults removes all default settings
func CreateProfile ¶
CreateProfile creates a new profile
func DeleteProfile ¶
DeleteProfile deletes a profile
func ExportConfig ¶
ExportConfig exports the configuration
func GetConfigDir ¶
GetConfigDir returns the directory where the config file is stored
func GetConfigFilePath ¶
GetConfigFilePath returns the path to the config file
func GetDefault ¶
GetDefault gets a default value
func ImportConfig ¶
ImportConfig imports configuration from a file
func ListProfiles ¶
ListProfiles lists all profiles
func RemoveDefault ¶
RemoveDefault removes a default setting
func SetDefault ¶
SetDefault sets a default value
func UpdateProfile ¶
UpdateProfile updates an existing profile
func UseProfile ¶
UseProfile sets the active profile
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) 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 (*Module) RegisterCommands ¶
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