config

package
v0.5.9 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const ConfigVersion = 1

ConfigVersion is the current version of the config file format

Variables

View Source
var (
	ErrProfileNotFound = errors.New("profile not found")
)
View Source
var LoginFunc = func(ctx context.Context) (*megaport.Client, error) {
	return LoginFuncWithOutput(ctx, "")
}

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

View Source
var LoginFuncWithOutput = func(ctx context.Context, outputFormat string) (*megaport.Client, error) {
	var accessKey, secretKey string

	env, err := resolveEnvironment(false)
	if err != nil {
		return nil, err
	}

	if utils.ProfileOverride != "" {
		manager, err := NewConfigManager()
		if err != nil {
			return nil, fmt.Errorf("failed to load config: %w", err)
		}
		profile, err := manager.GetProfile(utils.ProfileOverride)
		if err != nil {
			return nil, fmt.Errorf("profile %q not found. Use 'megaport config list-profiles' to see available profiles", utils.ProfileOverride)
		}
		accessKey = profile.AccessKey
		secretKey = profile.SecretKey
	} else {

		if utils.Env != "" {

			accessKey = os.Getenv("MEGAPORT_ACCESS_KEY")
			secretKey = os.Getenv("MEGAPORT_SECRET_KEY")

			if accessKey == "" || secretKey == "" {
				manager, err := NewConfigManager()
				if err == nil {
					profile, _, err := manager.GetCurrentProfile()
					if err == nil {
						if accessKey == "" {
							accessKey = profile.AccessKey
						}
						if secretKey == "" {
							secretKey = profile.SecretKey
						}
					}
				}
			}
		} else {

			manager, err := NewConfigManager()
			if err == nil {
				profile, _, err := manager.GetCurrentProfile()
				if err == nil {
					accessKey = profile.AccessKey
					secretKey = profile.SecretKey
				}
			}

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

	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")
	}

	envOpt := environmentOption(env)
	httpClient := &http.Client{Timeout: 30 * time.Second}

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

	spinner := output.PrintLoggingInWithOutput(false, outputFormat)
	_, err = megaportClient.Authorize(ctx)

	if err != nil {
		spinner.Stop()
		return nil, err
	} else {

		envDisplay := env
		if len(envDisplay) > 0 {
			envDisplay = strings.ToUpper(envDisplay[:1]) + envDisplay[1:]
		}
		spinner.StopWithSuccess(fmt.Sprintf("Successfully logged in to Megaport %s", envDisplay))
	}

	return megaportClient, nil
}

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

View Source
var NewUnauthenticatedClientFunc = func() (*megaport.Client, error) {
	env, err := resolveEnvironment(true)
	if err != nil {
		return nil, err
	}

	envOpt := environmentOption(env)
	httpClient := &http.Client{Timeout: 30 * time.Second}
	return megaport.New(httpClient, envOpt)
}

NewUnauthenticatedClientFunc creates a Megaport API client without authentication. Used for public API endpoints (e.g., locations) that don't require credentials.

Functions

func AddCommandsTo

func AddCommandsTo(rootCmd *cobra.Command)

func ClearDefaults

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

func CreateProfile

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

func DeleteProfile

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

func ExportConfig

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

func GetConfigDir

func GetConfigDir() (string, error)

func GetConfigFilePath

func GetConfigFilePath() (string, error)

func GetDefault

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

func ImportConfig

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

func ListProfiles

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

func Login

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

func LoginWithOutput added in v0.4.7

func LoginWithOutput(ctx context.Context, outputFormat string) (*megaport.Client, error)

func NewUnauthenticatedClient added in v0.5.9

func NewUnauthenticatedClient() (*megaport.Client, error)

NewUnauthenticatedClient creates an unauthenticated Megaport API client.

func RemoveDefault

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

func SetDefault

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

func UpdateProfile

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

func UseProfile

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

func ViewConfig

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

Types

type ConfigFile

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

ConfigFile represents the configuration file structure

func NewConfigFile

func NewConfigFile() *ConfigFile

NewConfigFile creates a new empty configuration file

type ConfigManager

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

ConfigManager handles configuration operations

func NewConfigManager

func NewConfigManager() (*ConfigManager, error)

func (*ConfigManager) ClearDefaults

func (m *ConfigManager) ClearDefaults() error

func (*ConfigManager) CreateProfile

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

func (*ConfigManager) DeleteProfile

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

func (*ConfigManager) Export

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

func (*ConfigManager) GetCurrentProfile

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

func (*ConfigManager) GetDefault

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

func (*ConfigManager) GetProfile added in v0.5.4

func (m *ConfigManager) GetProfile(name string) (*Profile, error)

func (*ConfigManager) ListProfiles

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

func (*ConfigManager) RemoveDefault

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

func (*ConfigManager) Save

func (m *ConfigManager) Save() error

func (*ConfigManager) SetDefault

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

func (*ConfigManager) UpdateProfile

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

func (*ConfigManager) UseProfile

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

type Module

type Module struct{}

func NewModule

func NewModule() *Module

func (*Module) Name

func (m *Module) Name() string

func (*Module) RegisterCommands

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

type Profile

type Profile struct {
	AccessKey   string `json:"accessKey"`
	SecretKey   string `json:"secretKey"`
	Environment string `json:"environment"`
	Description string `json:"description,omitempty"`
}

Profile represents a credential profile

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"`
}

Jump to

Keyboard shortcuts

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