Skip to content

Commit

Permalink
added GetAll functions for missing credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
daolis committed Dec 6, 2022
1 parent 3623c9f commit 3cac3ca
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 6 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var favCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(cmd.Help())
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// do not call PersistentPreRun from parent
},
}

var favListCmd = &cobra.Command{
Expand All @@ -31,6 +34,9 @@ var favListCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
cobra.CheckErr(listFavourites(cmd.Root().Name()))
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// do not call PersistentPreRun from parent
},
}

func newToolsConfig(options ...toolsconfig.ConfigOption) (toolsconfig.Configuration, error) {
Expand Down
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

var _ Configuration = new(ToolConfiguration)
var _ Configuration = &ToolConfiguration{}

type ToolConfiguration struct {
config *Config
Expand Down
18 changes: 15 additions & 3 deletions toolsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ type Configuration interface {
SetAzureSubscriptionCredentials(entry AzureSubscriptionCredential) error
// GetAzureSubscriptionCredentials get the azure subscription credentials.
GetAzureSubscriptionCredentials(nameOrID string) (*AzureSubscriptionCredential, error)
// GetAllAzureSubscriptionCredentials returns all azure subscription credentials availabel in config file.
// GetAllAzureSubscriptionCredentials returns all azure subscription credentials available in config file.
GetAllAzureSubscriptionCredentials() []AzureSubscriptionCredential
// SetServerCredentials set the server credentials.
SetServerCredentials(entry ServerCredential) error
// GetServerCredentials get the server credentials.
GetServerCredentials(url string) (*ServerCredential, error)
// GetAllServerCredentials returns all generic credentials available in config file.
GetAllServerCredentials() []ServerCredential
// SetGenericCredentials set the generic credentials.
SetGenericCredentials(entry GenericCredential) error
// GetGenericCredentials set the generic credentials.
GetGenericCredentials(key string) (*GenericCredential, error)
// GetAllGenericCredentials returns all generic credentials available in config file.
GetAllGenericCredentials() []GenericCredential
// GetGeneric ...
GetGeneric(key string) string
// SetDefaultSubscription set the default azure subscription.
Expand Down Expand Up @@ -58,9 +62,9 @@ func ConfigFileLocation(dir, filename string) {
// * RequiredGeneric(..)
// to specify which credentials are required. If the credentials are not available in the configuration,
// an error is returned immediately.
func NewToolConfiguration(options ...ConfigOption) (Configuration, error) {
var NewToolConfiguration = func(options ...ConfigOption) (Configuration, error) {
if configDirectory == nil || configFile == nil {
panic(`Configuration file location not set. Call toolconfig.ConfigFileLocation("dir", "filename")`)
return nil, fmt.Errorf(`configuration file location not set (Call toolconfig.ConfigFileLocation("dir", "filename"))`)
}
opts := ConfigOptions{
updateConfig: true,
Expand Down Expand Up @@ -187,6 +191,10 @@ func (c *ToolConfiguration) GetServerCredentials(url string) (*ServerCredential,
return credential, nil
}

func (c *ToolConfiguration) GetAllServerCredentials() []ServerCredential {
return c.config.Servers
}

// GetAzureSubscriptionCredentials find the credentials for the given name or subscription id. Returns errNotFound if not found.
func (c *ToolConfiguration) GetAzureSubscriptionCredentials(nameOrID string) (*AzureSubscriptionCredential, error) {
if fromEnv := (AzureSubscriptionCredential{}.FromEnv(nameOrID)); fromEnv != nil {
Expand Down Expand Up @@ -227,6 +235,10 @@ func (c *ToolConfiguration) GetGenericCredentials(key string) (*GenericCredentia
return credential, nil
}

func (c *ToolConfiguration) GetAllGenericCredentials() []GenericCredential {
return c.config.Generic
}

// GetGeneric is a simple call to get only the value of a generic key. Empty string if not exists.
func (c *ToolConfiguration) GetGeneric(key string) string {
credentials, err := c.GetGenericCredentials(key)
Expand Down

0 comments on commit 3cac3ca

Please sign in to comment.