Skip to content

Commit

Permalink
feat(nav): move config readers into command pkg (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 29, 2023
1 parent 6f67c65 commit 906001a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 55 deletions.
8 changes: 4 additions & 4 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type ConfigInfo struct {
ConfigType string
ConfigPath string
Viper configuration.ViperConfig
Readers proxy.ConfigReaders
Readers ConfigReaders
}

// Bootstrap represents construct that performs start up of the cli
Expand Down Expand Up @@ -96,9 +96,9 @@ func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command {
ConfigType: "yaml",
ConfigPath: home,
Viper: &configuration.GlobalViperConfig{},
Readers: proxy.ConfigReaders{
Profiles: &proxy.MsProfilesConfigReader{},
Sampler: &proxy.MsSamplerConfigReader{},
Readers: ConfigReaders{
Profiles: &MsProfilesConfigReader{},
Sampler: &MsSamplerConfigReader{},
},
},
}
Expand Down
57 changes: 57 additions & 0 deletions src/app/command/config-readers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package command

import (
"fmt"

"github.com/snivilised/cobrass/src/assistant/configuration"
"github.com/snivilised/cobrass/src/clif"
"github.com/snivilised/pixa/src/app/proxy"
)

type MsProfilesConfigReader struct {
}

func (r *MsProfilesConfigReader) Read(viper configuration.ViperConfig) (proxy.ProfilesConfig, error) {
// Ideally, the ProfileParameterSet would perform a check against
// the config, but extendio is not aware of config, so it can't
// check. Instead, we can check here.
//
profilesCFG := &proxy.MsProfilesConfig{
Profiles: make(proxy.ProfilesConfigMap),
}

if raw := viper.Get("profiles"); raw != nil {
if profiles, ok := raw.(proxy.ProfilesFlagOptionAsAnyPair); ok {
for profile, pv := range profiles {
if pair, ok := pv.(proxy.ProfilesFlagOptionAsAnyPair); ok {
profilesCFG.Profiles[profile] = make(clif.ChangedFlagsMap)

for flag, optionAsAny := range pair {
profilesCFG.Profiles[profile][flag] = fmt.Sprint(optionAsAny)
}
}
}
} else {
return nil, fmt.Errorf("invalid type for 'profiles'")
}
}

return profilesCFG, nil
}

type MsSamplerConfigReader struct{}

func (r *MsSamplerConfigReader) Read(viper configuration.ViperConfig) (proxy.SamplerConfig, error) {
var (
samplerCFG proxy.MsSamplerConfig
)

err := viper.UnmarshalKey("sampler", &samplerCFG)

return &samplerCFG, err
}

type ConfigReaders struct {
Profiles proxy.ProfilesConfigReader
Sampler proxy.SamplerConfigReader
}
6 changes: 3 additions & 3 deletions src/app/proxy/controller-sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const (
var (
_ proxy.ProfilesConfig = proxy.MsProfilesConfig{}
_ proxy.SamplerConfig = &proxy.MsSamplerConfig{}
_ proxy.ProfilesConfigReader = &proxy.MsProfilesConfigReader{}
_ proxy.SamplerConfigReader = &proxy.MsSamplerConfigReader{}
_ proxy.ProfilesConfigReader = &command.MsProfilesConfigReader{}
_ proxy.SamplerConfigReader = &command.MsSamplerConfigReader{}
)

type controllerTE struct {
Expand Down Expand Up @@ -118,7 +118,7 @@ var _ = Describe("SamplerController", Ordered, func() {
co.Config.Name = helpers.PixaConfigTestFilename
co.Config.ConfigPath = configPath
co.Viper = &configuration.GlobalViperConfig{}
co.Config.Readers = proxy.ConfigReaders{
co.Config.Readers = command.ConfigReaders{
Profiles: mockProfilesReader,
Sampler: mockSamplerReader,
}
Expand Down
48 changes: 0 additions & 48 deletions src/app/proxy/sampler-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ type (
SamplerConfigReader interface {
Read(configuration.ViperConfig) (SamplerConfig, error)
}

ConfigReaders struct {
Profiles ProfilesConfigReader
Sampler SamplerConfigReader
}
)

type MsProfilesConfig struct {
Expand Down Expand Up @@ -95,46 +90,3 @@ func (cfg *MsSamplerConfig) NoFiles() uint {
func (cfg *MsSamplerConfig) NoFolders() uint {
return cfg.Folders
}

type MsProfilesConfigReader struct {
}

func (r *MsProfilesConfigReader) Read(viper configuration.ViperConfig) (ProfilesConfig, error) {
// Ideally, the ProfileParameterSet would perform a check against
// the config, but extendio is not aware of config, so it can't
// check. Instead, we can check here.
//
profilesCFG := &MsProfilesConfig{
Profiles: make(ProfilesConfigMap),
}

if raw := viper.Get("profiles"); raw != nil {
if profiles, ok := raw.(ProfilesFlagOptionAsAnyPair); ok {
for profile, pv := range profiles {
if pair, ok := pv.(ProfilesFlagOptionAsAnyPair); ok {
profilesCFG.Profiles[profile] = make(clif.ChangedFlagsMap)

for flag, optionAsAny := range pair {
profilesCFG.Profiles[profile][flag] = fmt.Sprint(optionAsAny)
}
}
}
} else {
return nil, fmt.Errorf("invalid type for 'profiles'")
}
}

return profilesCFG, nil
}

type MsSamplerConfigReader struct{}

func (r *MsSamplerConfigReader) Read(viper configuration.ViperConfig) (SamplerConfig, error) {
var (
samplerCFG MsSamplerConfig
)

err := viper.UnmarshalKey("sampler", &samplerCFG)

return &samplerCFG, err
}

0 comments on commit 906001a

Please sign in to comment.