pkg

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MembershipURIFlag specifies the URI for membership
	MembershipURIFlag = "membership-uri"
	// FileFlag specifies the configuration file
	FileFlag = "config"
	// ProfileFlag specifies the profile to use
	ProfileFlag = "profile"
	// OutputFlag specifies the output format
	OutputFlag = "output"
	// DebugFlag specifies whether to run the command in debug mode
	DebugFlag = "debug"
	// TelemetryFlag specifies whether to enable telemetry
	TelemetryFlag = "telemetry"
)
View Source
const AuthClient = "mdz"

AuthClient is the name of the OIDC client.

Variables

View Source
var (

	// ErrOrganizationNotSpecified indicates that no organization was specified when one was required.
	ErrOrganizationNotSpecified = errors.New("organization not specified")

	// ErrMultipleOrganizationsFound indicates that more than one organization was found when only one was expected, and no specific organization was specified.
	ErrMultipleOrganizationsFound = errors.New("found more than one organization and no organization specified")
)
View Source
var ErrOpenningBrowser = errors.New("opening browser")

ErrOpenningBrowser is a struct that return an error when opening browser

Functions

func ContainValue

func ContainValue[V comparable](array []V, value V) bool

ContainValue func that valid if contains value

func GetAuthRelyingParty

func GetAuthRelyingParty(httpClient *http.Client, membershipURI string) (rp.RelyingParty, error)

GetAuthRelyingParty returns a NewRelyingPartyOIDC that creates an (OIDC)

func GetBool

func GetBool(cmd *cobra.Command, flagName string) bool

GetBool retrieves the boolean value of the specified flag from the command.

func GetCurrentProfileName

func GetCurrentProfileName(cmd *cobra.Command, config *Config) string

GetCurrentProfileName retrieves the name of the current profile from the given cobra.Command and configuration. If the ProfileFlag is set, it returns the value of the ProfileFlag. Otherwise, it returns the current profile name from the configuration.

func GetDateTime

func GetDateTime(cmd *cobra.Command, flagName string) (*time.Time, error)

GetDateTime retrieves the time value of the specified flag from the command.

func GetHTTPClient

func GetHTTPClient(cmd *cobra.Command, defaultHeaders map[string][]string) *http.Client

GetHTTPClient creates and returns a new HTTP client configured based on the provided cobra.Command flags and default headers. It utilizes the NewHTTPClient function, passing in the values of the InsecureTlsFlag and DebugFlag, along with any defaultHeaders specified. This allows for the creation of a customized http.Client instance tailored to the needs of the application, including support for insecure TLS connections and debugging capabilities.

func GetInt

func GetInt(cmd *cobra.Command, flagName string) int

GetInt retrieves the integer value of the specified flag from the command.

func GetSelectedOrganization

func GetSelectedOrganization(cmd *cobra.Command) string

GetSelectedOrganization retrieves the selected organization from the command.

func GetSelectedStackID

func GetSelectedStackID(cmd *cobra.Command) string

GetSelectedStackID retrieves the selected stack ID from the command.

func GetString

func GetString(cmd *cobra.Command, flagName string) string

GetString retrieves the string value of the specified flag from the command.

func GetStringSlice

func GetStringSlice(cmd *cobra.Command, flagName string) []string

GetStringSlice retrieves the string slice value of the specified flag from the command.

func IsInvalidAuthentication

func IsInvalidAuthentication(err error) bool

IsInvalidAuthentication checks if the provided error is an instance of ErrInvalidAuthentication.

func ListProfiles

func ListProfiles(cmd *cobra.Command, toComplete string) ([]string, error)

ListProfiles generates a list of profiles based on the toComplete string.

Parameters:

cmd: *cobra.Command - the command object
toComplete: string - the string to complete

Return type:

[]string: list of profile strings
error: any error that occurred

func Map

func Map[SRC, DST any](srcs []SRC, mapper func(SRC) DST) []DST

Map func that appends slices

func MapKeys

func MapKeys[K comparable, V any](m map[K]V) []K

MapKeys func that compare and appends slices

func MapMap

func MapMap[KEY comparable, VALUE, DST any](srcs map[KEY]VALUE, mapper func(KEY, VALUE) DST) []DST

MapMap func that compare and appends slices

func NewCommand

func NewCommand(use string, opts ...CommandOption) *cobra.Command

NewCommand creates a new cobra command with the specified use string and options.

Parameters: - use: a string representing the use of the command. - opts: variadic CommandOption options. Return type: - *cobra.Command: a pointer to the created cobra command.

func NewHTTPClient

func NewHTTPClient(debug bool, defaultHeaders map[string][]string) *http.Client

NewHTTPClient initializes and returns a new http.Client with customizable behavior. It allows for the configuration of TLS insecurity (skipping TLS verification), enabling a debug mode for additional logging, and setting default headers for all requests.

Parameters:

  • insecureTLS: If true, the client will accept any TLS certificate presented by the server and any host name in that certificate. This is useful for testing with self-signed certificates.
  • debug: If true, wraps the transport in a debugging layer that logs all requests and responses. This is helpful for development and troubleshooting.
  • defaultHeaders: A map of header names to their values which will be added to every request made by this client. Useful for setting headers like `Authorization` or `User-Agent` that should be included in all requests.

Returns: - A pointer to an initialized http.Client configured as specified.

func NewMembershipCommand

func NewMembershipCommand(use string, opts ...CommandOption) *cobra.Command

NewMembershipCommand is a helper function that returns a new membership command with the given options.

func NewStackCommand

func NewStackCommand(use string, opts ...CommandOption) *cobra.Command

NewStackCommand is a helper function that returns a new stack command with the given options.

func Prepend

func Prepend[V any](array []V, items ...V) []V

Prepend func that return two append items

func Ptr

func Ptr[T any](t T) *T

Ptr returns a pointer to the given value.

func RetrieveOrganizationIDFromFlagOrProfile

func RetrieveOrganizationIDFromFlagOrProfile(cmd *cobra.Command, cfg *Config) (string, error)

RetrieveOrganizationIDFromFlagOrProfile retrieves the organization ID from the command flag or profile.

func WithRender

func WithRender[T any](cmd *cobra.Command, args []string, controller Controller[T], renderable Renderable) error

WithRender handles the rendering of the output based on the output flag.

Types

type CommandOption

type CommandOption interface {
	// contains filtered or unexported methods
}

CommandOption is an interface for options that can be applied to a cobra.Command.

type CommandOptionFn

type CommandOptionFn func(cmd *cobra.Command)

CommandOptionFn is a function that applies a CommandOption to a cobra.Command.

func WithAliases

func WithAliases(aliases ...string) CommandOptionFn

WithAliases is a helper function that returns a CommandOptionFn that applies aliases to a cobra.Command.

func WithArgs

WithArgs is a helper function that returns a CommandOptionFn that applies positional arguments to a cobra.Command.

func WithBoolFlag

func WithBoolFlag(name string, defaultValue bool, help string) CommandOptionFn

WithBoolFlag is a helper function that returns a CommandOptionFn that applies a boolean flag to a cobra.Command.

func WithChildCommands

func WithChildCommands(cmds ...*cobra.Command) CommandOptionFn

WithChildCommands is a helper function that returns a CommandOptionFn that applies child commands to a cobra.Command.

func WithController

func WithController[T any](c Controller[T]) CommandOptionFn

WithController wraps a controller's Run method as a cobra command's RunE function.

func WithDeprecated

func WithDeprecated(message string) CommandOptionFn

WithDeprecated is a helper function that returns a CommandOptionFn that applies a deprecated flag to a cobra.Command.

func WithDeprecatedFlag

func WithDeprecatedFlag(name, message string) CommandOptionFn

WithDeprecatedFlag is a helper function that returns a CommandOptionFn that applies a deprecated flag to a cobra.Command.

func WithDescription

func WithDescription(v string) CommandOptionFn

WithDescription is a helper function that returns a CommandOptionFn that applies a description to a cobra.Command.

func WithHidden

func WithHidden() CommandOptionFn

WithHidden is a helper function that returns a CommandOptionFn that applies a hidden flag to a cobra.Command.

func WithHiddenFlag

func WithHiddenFlag(name string) CommandOptionFn

WithHiddenFlag is a helper function that returns a CommandOptionFn that applies a hidden flag to a cobra.Command.

func WithIntFlag

func WithIntFlag(name string, defaultValue int, help string) CommandOptionFn

WithIntFlag is a helper function that returns a CommandOptionFn that applies an integer flag to a cobra.Command.

func WithPersistentBoolFlag

func WithPersistentBoolFlag(name string, defaultValue bool, help string) CommandOptionFn

WithPersistentBoolFlag is a helper function that returns a CommandOptionFn that applies a persistent boolean flag to a cobra.Command.

func WithPersistentBoolPFlag

func WithPersistentBoolPFlag(name, short string, defaultValue bool, help string) CommandOptionFn

WithPersistentBoolPFlag is a helper function that returns a CommandOptionFn that applies a persistent boolean flag to a cobra.Command.

func WithPersistentPreRunE

func WithPersistentPreRunE(fn func(cmd *cobra.Command, args []string) error) CommandOptionFn

WithPersistentPreRunE is a helper function that returns a CommandOptionFn that applies a persistent pre-run function to a cobra.Command.

func WithPersistentStringFlag

func WithPersistentStringFlag(name, defaultValue, help string) CommandOptionFn

WithPersistentStringFlag is a helper function that returns a CommandOptionFn that applies a persistent string flag to a cobra.Command.

func WithPersistentStringPFlag

func WithPersistentStringPFlag(name, short, defaultValue, help string) CommandOptionFn

WithPersistentStringPFlag is a helper function that returns a CommandOptionFn that applies a persistent string flag to a cobra.Command.

func WithPreRunE

func WithPreRunE(fn func(cmd *cobra.Command, args []string) error) CommandOptionFn

WithPreRunE is a helper function that returns a CommandOptionFn that applies a pre-run function to a cobra.Command.

func WithRunE

func WithRunE(fn func(cmd *cobra.Command, args []string) error) CommandOptionFn

WithRunE is a helper function that returns a CommandOptionFn that applies a run function to a cobra.Command.

func WithShortDescription

func WithShortDescription(v string) CommandOptionFn

WithShortDescription is a helper function that returns a CommandOptionFn that applies a short description to a cobra.Command.

func WithSilenceError

func WithSilenceError() CommandOptionFn

WithSilenceError is a helper function that returns a CommandOptionFn that applies silence error to a cobra.Command.

func WithSilenceUsage

func WithSilenceUsage() CommandOptionFn

WithSilenceUsage is a helper function that returns a CommandOptionFn that applies silence usage to a cobra.Command.

func WithStringArrayFlag

func WithStringArrayFlag(name string, defaultValue []string, help string) CommandOptionFn

WithStringArrayFlag is a helper function that returns a CommandOptionFn that applies a string array flag to a cobra.Command.

func WithStringFlag

func WithStringFlag(name, defaultValue, help string) CommandOptionFn

WithStringFlag is a helper function that returns a CommandOptionFn that applies a string flag to a cobra.Command.

func WithStringSliceFlag

func WithStringSliceFlag(name string, defaultValue []string, help string) CommandOptionFn

WithStringSliceFlag is a helper function that returns a CommandOptionFn that applies a string slice flag to a cobra.Command.

func WithValidArgs

func WithValidArgs(validArgs ...string) CommandOptionFn

WithValidArgs is a helper function that returns a CommandOptionFn that applies valid arguments to a cobra.Command.

func WithValidArgsFunction

func WithValidArgsFunction(fn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)) CommandOptionFn

WithValidArgsFunction returns a CommandOptionFn that sets a custom validation function for command arguments.

type Config

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

Config represents the configuration for the application. It encapsulates details such as the current profile in use, a unique identifier for the configuration, a map of profile names to Profile instances, and a reference to the ConfigManager responsible for managing this configuration.

func GetConfig

func GetConfig(cmd *cobra.Command) (*Config, error)

GetConfig retrieves the configuration from the file system. It uses the ConfigManager to load the configuration from the file system.

func (*Config) DeleteProfile

func (c *Config) DeleteProfile(s string) error

DeleteProfile removes a profile from the configuration by name. If the profile does not exist, it returns an error.

func (*Config) GetCurrentProfileName

func (c *Config) GetCurrentProfileName() string

GetCurrentProfileName retrieves the name of the current profile from the configuration.

func (*Config) GetProfile

func (c *Config) GetProfile(name string) *Profile

GetProfile retrieves a profile by name from the configuration. If the profile does not exist, it returns nil.

func (*Config) GetProfileOrDefault

func (c *Config) GetProfileOrDefault(name, membershipURI string) *Profile

GetProfileOrDefault retrieves a profile by name from the configuration. If the profile does not exist, it creates a new profile with the given membership URI and returns it.

func (*Config) GetProfiles

func (c *Config) GetProfiles() map[string]*Profile

GetProfiles retrieves the map of profile names to Profile instances from the configuration.

func (*Config) GetUniqueID

func (c *Config) GetUniqueID() string

GetUniqueID retrieves the unique identifier for the configuration.

func (*Config) MarshalJSON

func (c *Config) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Config. It converts the Config instance into a JSON-encoded byte slice.

func (*Config) Persist

func (c *Config) Persist() error

Persist saves the configuration to the file system. It uses the ConfigManager to update the configuration file with the current configuration.

func (*Config) SetCurrentProfile

func (c *Config) SetCurrentProfile(name string, profile *Profile)

SetCurrentProfile sets the current profile to the given name and profile. It also updates the current profile name to the given name.

func (*Config) SetCurrentProfileName

func (c *Config) SetCurrentProfileName(s string)

SetCurrentProfileName sets the name of the current profile to the given string.

func (*Config) SetProfile

func (c *Config) SetProfile(name string, profile *Profile)

SetProfile sets the profile with the given name to the given profile.

func (*Config) SetUniqueID

func (c *Config) SetUniqueID(id string)

SetUniqueID sets the unique identifier for the configuration.

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Config. It populates the Config instance with data from a JSON-encoded byte slice.

type ConfigManager

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

ConfigManager is a struct that use a string configFilePath

func GetConfigManager

func GetConfigManager(cmd *cobra.Command) *ConfigManager

GetConfigManager retrieves the ConfigManager instance associated with the given cobra.Command. It uses the FileFlag to determine the configuration file path and returns a new ConfigManager instance.

func NewConfigManager

func NewConfigManager(configFilePath string) *ConfigManager

NewConfigManager is a func that return a struc of *ConfigManager

func (*ConfigManager) Load

func (m *ConfigManager) Load() (*Config, error)

Load is a func that load a filepath and return a *Config of application

func (*ConfigManager) UpdateConfig

func (m *ConfigManager) UpdateConfig(config *Config) error

UpdateConfig is a func that update a *Config of application

type Controller

type Controller[T any] interface {
	GetStore() T
	Run(cmd *cobra.Command, args []string) (Renderable, error)
}

Controller is a generic interface that defines the structure of a controller capable of handling commands. It requires two methods:

  • GetStore, which returns a store of type T.
  • Run, which takes a cobra.Command and a slice of strings (arguments), and returns a Renderable and an error. This method is responsible for executing the command's logic.

type CurrentProfile

type CurrentProfile Profile

CurrentProfile profile

type ErrInvalidAuthentication

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

ErrInvalidAuthentication is an error that occurs when the authentication token is invalid.

func (ErrInvalidAuthentication) Error

func (e ErrInvalidAuthentication) Error() string

Error returns the error message for ErrInvalidAuthentication.

No parameters. Returns a string.

func (ErrInvalidAuthentication) Is

Is checks if the provided error is of type ErrInvalidAuthentication.

func (ErrInvalidAuthentication) Unwrap

func (e ErrInvalidAuthentication) Unwrap() error

Unwrap returns the underlying error of ErrInvalidAuthentication.

No parameters. Returns an error.

type ExportedData

type ExportedData struct {
	Data any `json:"data"`
}

ExportedData represents a generic structure for data that can be exported. It contains a single field, Data, which can hold any type of value. The field is tagged with `json:"data"` to specify its JSON key when serialized.

type Profile

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

Profile represents a user profile.

func GetCurrentProfile

func GetCurrentProfile(cmd *cobra.Command, cfg *Config) *Profile

GetCurrentProfile retrieves the current profile from the given cobra.Command and configuration. It returns the current profile from the configuration. If the current profile does not exist, it creates a new profile with the default membership URI and returns it.

func (*Profile) GetClaims

func (p *Profile) GetClaims() (jwt.MapClaims, error)

GetClaims returns the jwt claims and an error.

No parameters. Returns jwt.MapClaims and error.

func (*Profile) GetDefaultOrganization

func (p *Profile) GetDefaultOrganization() string

GetDefaultOrganization returns the default organization for the Profile.

No parameters. Returns a string.

func (*Profile) GetMembershipURI

func (p *Profile) GetMembershipURI() string

GetMembershipURI returns the membership URI of the Profile.

No parameters. Returns a string.

func (*Profile) GetToken

func (p *Profile) GetToken(ctx context.Context, httpClient *http.Client) (*oauth2.Token, error)

GetToken retrieves and refreshes the OAuth2 token if needed.

Parameters:

  • ctx: the context for the HTTP request
  • httpClient: the HTTP client to use for making HTTP requests

Returns:

  • *oauth2.Token: the OAuth2 token retrieved or refreshed
  • error: an error if any occurred during the token retrieval or refresh

func (*Profile) IsConnected

func (p *Profile) IsConnected() bool

IsConnected is connected

func (*Profile) MarshalJSON

func (p *Profile) MarshalJSON() ([]byte, error)

MarshalJSON generates the JSON encoding for the Profile struct.

No parameters. Returns a byte slice and an error.

func (*Profile) SetDefaultOrganization

func (p *Profile) SetDefaultOrganization(o string)

SetDefaultOrganization sets the default organization

func (*Profile) SetMembershipURI

func (p *Profile) SetMembershipURI(v string)

SetMembershipURI sets the membership URI for the Profile.

Takes a string parameter.

func (*Profile) UnmarshalJSON

func (p *Profile) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON-encoded data and stores the result in the Profile struct.

It takes a byte slice data as a parameter. Returns an error.

func (*Profile) UpdateToken

func (p *Profile) UpdateToken(token *oidc.AccessTokenResponse)

UpdateToken updates the token for the Profile.

token *oidc.AccessTokenResponse - The new access token to be set.

type Renderable

type Renderable interface {
	Render(cmd *cobra.Command, args []string) error
}

Renderable defines an interface for objects that can render themselves. It requires implementing the Render method, which takes a cobra.Command and a slice of strings (arguments) and returns an error.

type RoundTripperFn

type RoundTripperFn func(req *http.Request) (*http.Response, error)

RoundTripperFn is a function type that implements the http.RoundTripper interface. It allows any function with the appropriate signature to be used as an http.RoundTripper. This is useful for creating custom transport behaviors in an http.Client.

func (RoundTripperFn) RoundTrip

func (fn RoundTripperFn) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip executes the RoundTripperFn function, effectively making RoundTripperFn an http.RoundTripper. This method allows RoundTripperFn to satisfy the http.RoundTripper interface, enabling its use as a custom transport mechanism within an http.Client.

Jump to

Keyboard shortcuts

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