diff --git a/src/app/command/bootstrap.go b/src/app/command/bootstrap.go index 20c5b54..8315cde 100644 --- a/src/app/command/bootstrap.go +++ b/src/app/command/bootstrap.go @@ -22,6 +22,7 @@ import ( "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/extendio/xfs/utils" "github.com/snivilised/pixa/src/app/proxy" + "github.com/snivilised/pixa/src/cfg" "github.com/snivilised/pixa/src/i18n" ) @@ -66,7 +67,7 @@ type ConfigInfo struct { ConfigType string ConfigPath string Viper configuration.ViperConfig - Readers ConfigReaders + Readers cfg.ConfigReaders } // Bootstrap represents construct that performs start up of the cli @@ -75,11 +76,11 @@ type ConfigInfo struct { type Bootstrap struct { Container *assistant.CobraContainer OptionsInfo ConfigureOptionsInfo - ProfilesCFG proxy.ProfilesConfig - SchemesCFG proxy.SchemesConfig - SamplerCFG proxy.SamplerConfig - AdvancedCFG proxy.AdvancedConfig - LoggingCFG proxy.LoggingConfig + ProfilesCFG cfg.ProfilesConfig + SchemesCFG cfg.SchemesConfig + SamplerCFG cfg.SamplerConfig + AdvancedCFG cfg.AdvancedConfig + LoggingCFG cfg.LoggingConfig Vfs storage.VirtualFS } @@ -107,12 +108,12 @@ func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command { ConfigType: "yaml", ConfigPath: home, Viper: &configuration.GlobalViperConfig{}, - Readers: ConfigReaders{ - Profiles: &MsProfilesConfigReader{}, - Schemes: &MsSchemesConfigReader{}, - Sampler: &MsSamplerConfigReader{}, - Advanced: &MsAdvancedConfigReader{}, - Logging: &MsLoggingConfigReader{}, + Readers: cfg.ConfigReaders{ + Profiles: &cfg.MsProfilesConfigReader{}, + Schemes: &cfg.MsSchemesConfigReader{}, + Sampler: &cfg.MsSamplerConfigReader{}, + Advanced: &cfg.MsAdvancedConfigReader{}, + Logging: &cfg.MsLoggingConfigReader{}, }, }, } diff --git a/src/app/command/bootstrap_test.go b/src/app/command/bootstrap_test.go index 7350864..3a93f63 100644 --- a/src/app/command/bootstrap_test.go +++ b/src/app/command/bootstrap_test.go @@ -8,6 +8,7 @@ import ( "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/pixa/src/app/command" "github.com/snivilised/pixa/src/app/mocks" + "github.com/snivilised/pixa/src/cfg" "github.com/snivilised/pixa/src/internal/helpers" "go.uber.org/mock/gomock" "golang.org/x/text/language" @@ -98,7 +99,7 @@ var _ = Describe("Bootstrap", Ordered, func() { co.Config.Name = helpers.PixaConfigTestFilename co.Config.ConfigPath = configPath co.Config.Viper = &configuration.GlobalViperConfig{} - co.Config.Readers = command.ConfigReaders{ + co.Config.Readers = cfg.ConfigReaders{ Profiles: mockProfilesReader, Schemes: mockSchemesReader, Sampler: mockSamplerReader, diff --git a/src/app/command/magick-cmd_test.go b/src/app/command/magick-cmd_test.go index f5706f4..3a545f0 100644 --- a/src/app/command/magick-cmd_test.go +++ b/src/app/command/magick-cmd_test.go @@ -11,6 +11,7 @@ import ( "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/pixa/src/app/command" "github.com/snivilised/pixa/src/app/mocks" + "github.com/snivilised/pixa/src/cfg" "github.com/snivilised/pixa/src/internal/helpers" ) @@ -75,7 +76,7 @@ var _ = Describe("MagickCmd", Ordered, func() { co.Config.Name = helpers.PixaConfigTestFilename co.Config.ConfigPath = configPath co.Config.Viper = &configuration.GlobalViperConfig{} - co.Config.Readers = command.ConfigReaders{ + co.Config.Readers = cfg.ConfigReaders{ Profiles: mockProfilesReader, Schemes: mockSchemesReader, Sampler: mockSamplerReader, diff --git a/src/app/command/ms-config.go b/src/app/command/ms-config.go deleted file mode 100644 index a499acd..0000000 --- a/src/app/command/ms-config.go +++ /dev/null @@ -1,169 +0,0 @@ -package command - -import ( - "fmt" - "time" - - "github.com/snivilised/cobrass/src/clif" - "github.com/snivilised/pixa/src/app/proxy" -) - -type MsProfilesConfig struct { - Profiles proxy.ProfilesConfigMap -} - -func (cfg MsProfilesConfig) Profile(name string) (clif.ChangedFlagsMap, bool) { - profile, found := cfg.Profiles[name] - - return profile, found -} - -type ( - MsSchemeConfig struct { - Profiles []string `mapstructure:"profiles"` - } - - MsSchemesConfig map[string]proxy.SchemeConfig -) - -func (cfg MsSchemesConfig) Validate(name string, profiles proxy.ProfilesConfig) error { - if name == "" { - return nil - } - - var ( - found bool - scheme proxy.SchemeConfig - ) - - if scheme, found = cfg[name]; !found { - return fmt.Errorf("scheme: '%v' not found in config", name) - } - - for _, p := range scheme.Profiles() { - if _, found := profiles.Profile(p); !found { - return fmt.Errorf("profile(referenced by scheme: '%v'): '%v' not found in config", - name, p, - ) - } - } - - return nil -} - -func (cfg MsSchemesConfig) Scheme(name string) (proxy.SchemeConfig, bool) { - config, found := cfg[name] - - return config, found -} - -type MsSamplerConfig struct { - Files uint `mapstructure:"files"` - Folders uint `mapstructure:"folders"` -} - -func (cfg *MsSamplerConfig) NoFiles() uint { - return cfg.Files -} - -func (cfg *MsSamplerConfig) NoFolders() uint { - return cfg.Folders -} - -type MsLabelsConfig struct { - Adhoc string `mapstructure:"adhoc"` - Journal string `mapstructure:"journal-suffix"` - Legacy string `mapstructure:"legacy"` - Trash string `mapstructure:"trash"` -} - -type MsExtensionsConfig struct { - FileSuffixes string `mapstructure:"suffixes"` - TransformsCSV string `mapstructure:"transforms"` - Remap map[string]string `mapstructure:"map"` -} - -func (cfg *MsExtensionsConfig) Suffixes() string { - return cfg.FileSuffixes -} - -func (cfg *MsExtensionsConfig) Transforms() string { - return cfg.TransformsCSV -} - -func (cfg *MsExtensionsConfig) Map() map[string]string { - return cfg.Remap -} - -type MsAdvancedConfig struct { - Abort bool `mapstructure:"abort-on-error"` - Timeout string `mapstructure:"program-timeout"` - NoProgramRetries uint `mapstructure:"no-program-retries"` - LabelsCFG MsLabelsConfig `mapstructure:"labels"` - ExtensionsCFG MsExtensionsConfig `mapstructure:"extensions"` -} - -func (cfg *MsAdvancedConfig) AbortOnError() bool { - return cfg.Abort -} - -func (cfg *MsAdvancedConfig) ProgramTimeout() (duration time.Duration, err error) { - return time.ParseDuration(cfg.Timeout) -} - -func (cfg *MsAdvancedConfig) NoRetries() uint { - return cfg.NoProgramRetries -} - -func (cfg *MsAdvancedConfig) AdhocLabel() string { - return cfg.LabelsCFG.Adhoc -} - -func (cfg *MsAdvancedConfig) JournalLabel() string { - return cfg.LabelsCFG.Journal -} - -func (cfg *MsAdvancedConfig) LegacyLabel() string { - return cfg.LabelsCFG.Legacy -} - -func (cfg *MsAdvancedConfig) TrashLabel() string { - return cfg.LabelsCFG.Trash -} - -func (cfg *MsAdvancedConfig) Extensions() proxy.ExtensionsConfig { - return &cfg.ExtensionsCFG -} - -type MsLoggingConfig struct { - LogPath string `mapstructure:"log-path"` - MaxSize uint `mapstructure:"max-size"` - MaxBackups uint `mapstructure:"max-backups"` - MaxAge uint `mapstructure:"max-age"` - LogLevel string `mapstructure:"level"` - Format string `mapstructure:"time-format"` -} - -func (cfg *MsLoggingConfig) Path() string { - return cfg.LogPath -} - -func (cfg *MsLoggingConfig) MaxSizeInMb() uint { - return cfg.MaxSize -} - -func (cfg *MsLoggingConfig) MaxNoOfBackups() uint { - return cfg.MaxBackups -} - -func (cfg *MsLoggingConfig) MaxAgeInDays() uint { - return cfg.MaxAge -} - -func (cfg *MsLoggingConfig) Level() string { - return cfg.LogLevel -} - -func (cfg *MsLoggingConfig) TimeFormat() string { - return cfg.Format -} diff --git a/src/app/command/shrink-cmd_test.go b/src/app/command/shrink-cmd_test.go index 5e50f83..8ff2bf3 100644 --- a/src/app/command/shrink-cmd_test.go +++ b/src/app/command/shrink-cmd_test.go @@ -9,7 +9,7 @@ import ( cmocks "github.com/snivilised/cobrass/src/assistant/mocks" "github.com/snivilised/pixa/src/app/command" "github.com/snivilised/pixa/src/app/mocks" - "github.com/snivilised/pixa/src/app/proxy" + "github.com/snivilised/pixa/src/cfg" "github.com/snivilised/pixa/src/internal/helpers" "go.uber.org/mock/gomock" @@ -17,12 +17,12 @@ import ( ) var ( - _ proxy.ProfilesConfig = &command.MsProfilesConfig{} - _ proxy.SamplerConfig = &command.MsSamplerConfig{} - _ proxy.ProfilesConfigReader = &command.MsProfilesConfigReader{} - _ proxy.SamplerConfigReader = &command.MsSamplerConfigReader{} - _ proxy.AdvancedConfigReader = &command.MsAdvancedConfigReader{} - _ proxy.LoggingConfigReader = &command.MsLoggingConfigReader{} + _ cfg.ProfilesConfig = &cfg.MsProfilesConfig{} + _ cfg.SamplerConfig = &cfg.MsSamplerConfig{} + _ cfg.ProfilesConfigReader = &cfg.MsProfilesConfigReader{} + _ cfg.SamplerConfigReader = &cfg.MsSamplerConfigReader{} + _ cfg.AdvancedConfigReader = &cfg.MsAdvancedConfigReader{} + _ cfg.LoggingConfigReader = &cfg.MsLoggingConfigReader{} ) const ( @@ -96,7 +96,7 @@ func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *shrinkTE, root co.Config.ConfigPath = entry.configPath co.Config.Viper = &configuration.GlobalViperConfig{} - co.Config.Readers = command.ConfigReaders{ + co.Config.Readers = cfg.ConfigReaders{ Profiles: mockProfilesReader, Schemes: mockSchemesReader, Sampler: mockSamplerReader, diff --git a/src/app/mocks/mocks-config.go b/src/app/mocks/mocks-config.go index f2d1fc4..d175312 100644 --- a/src/app/mocks/mocks-config.go +++ b/src/app/mocks/mocks-config.go @@ -3,7 +3,7 @@ // // Generated by this command: // -// mockgen -destination ../mocks/mocks-config.go -package mocks -source config.go +// mockgen -destination ../app/mocks/mocks-config.go -package mocks -source config.go // // Package mocks is a generated GoMock package. package mocks @@ -14,7 +14,7 @@ import ( configuration "github.com/snivilised/cobrass/src/assistant/configuration" clif "github.com/snivilised/cobrass/src/clif" - proxy "github.com/snivilised/pixa/src/app/proxy" + cfg "github.com/snivilised/pixa/src/cfg" gomock "go.uber.org/mock/gomock" ) @@ -80,10 +80,10 @@ func (m *MockProfilesConfigReader) EXPECT() *MockProfilesConfigReaderMockRecorde } // Read mocks base method. -func (m *MockProfilesConfigReader) Read(arg0 configuration.ViperConfig) (proxy.ProfilesConfig, error) { +func (m *MockProfilesConfigReader) Read(arg0 configuration.ViperConfig) (cfg.ProfilesConfig, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Read", arg0) - ret0, _ := ret[0].(proxy.ProfilesConfig) + ret0, _ := ret[0].(cfg.ProfilesConfig) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -155,10 +155,10 @@ func (m *MockSchemesConfig) EXPECT() *MockSchemesConfigMockRecorder { } // Scheme mocks base method. -func (m *MockSchemesConfig) Scheme(name string) (proxy.SchemeConfig, bool) { +func (m *MockSchemesConfig) Scheme(name string) (cfg.SchemeConfig, bool) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Scheme", name) - ret0, _ := ret[0].(proxy.SchemeConfig) + ret0, _ := ret[0].(cfg.SchemeConfig) ret1, _ := ret[1].(bool) return ret0, ret1 } @@ -170,7 +170,7 @@ func (mr *MockSchemesConfigMockRecorder) Scheme(name any) *gomock.Call { } // Validate mocks base method. -func (m *MockSchemesConfig) Validate(name string, profiles proxy.ProfilesConfig) error { +func (m *MockSchemesConfig) Validate(name string, profiles cfg.ProfilesConfig) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Validate", name, profiles) ret0, _ := ret[0].(error) @@ -207,10 +207,10 @@ func (m *MockSchemesConfigReader) EXPECT() *MockSchemesConfigReaderMockRecorder } // Read mocks base method. -func (m *MockSchemesConfigReader) Read(arg0 configuration.ViperConfig) (proxy.SchemesConfig, error) { +func (m *MockSchemesConfigReader) Read(arg0 configuration.ViperConfig) (cfg.SchemesConfig, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Read", arg0) - ret0, _ := ret[0].(proxy.SchemesConfig) + ret0, _ := ret[0].(cfg.SchemesConfig) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -296,10 +296,10 @@ func (m *MockSamplerConfigReader) EXPECT() *MockSamplerConfigReaderMockRecorder } // Read mocks base method. -func (m *MockSamplerConfigReader) Read(arg0 configuration.ViperConfig) (proxy.SamplerConfig, error) { +func (m *MockSamplerConfigReader) Read(arg0 configuration.ViperConfig) (cfg.SamplerConfig, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Read", arg0) - ret0, _ := ret[0].(proxy.SamplerConfig) + ret0, _ := ret[0].(cfg.SamplerConfig) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -333,6 +333,20 @@ func (m *MockExtensionsConfig) EXPECT() *MockExtensionsConfigMockRecorder { return m.recorder } +// Map mocks base method. +func (m *MockExtensionsConfig) Map() map[string]string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Map") + ret0, _ := ret[0].(map[string]string) + return ret0 +} + +// Map indicates an expected call of Map. +func (mr *MockExtensionsConfigMockRecorder) Map() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Map", reflect.TypeOf((*MockExtensionsConfig)(nil).Map)) +} + // Suffixes mocks base method. func (m *MockExtensionsConfig) Suffixes() string { m.ctrl.T.Helper() @@ -413,10 +427,10 @@ func (mr *MockAdvancedConfigMockRecorder) AdhocLabel() *gomock.Call { } // Extensions mocks base method. -func (m *MockAdvancedConfig) Extensions() proxy.ExtensionsConfig { +func (m *MockAdvancedConfig) Extensions() cfg.ExtensionsConfig { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Extensions") - ret0, _ := ret[0].(proxy.ExtensionsConfig) + ret0, _ := ret[0].(cfg.ExtensionsConfig) return ret0 } @@ -521,10 +535,10 @@ func (m *MockAdvancedConfigReader) EXPECT() *MockAdvancedConfigReaderMockRecorde } // Read mocks base method. -func (m *MockAdvancedConfigReader) Read(arg0 configuration.ViperConfig) (proxy.AdvancedConfig, error) { +func (m *MockAdvancedConfigReader) Read(arg0 configuration.ViperConfig) (cfg.AdvancedConfig, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Read", arg0) - ret0, _ := ret[0].(proxy.AdvancedConfig) + ret0, _ := ret[0].(cfg.AdvancedConfig) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -666,10 +680,10 @@ func (m *MockLoggingConfigReader) EXPECT() *MockLoggingConfigReaderMockRecorder } // Read mocks base method. -func (m *MockLoggingConfigReader) Read(arg0 configuration.ViperConfig) (proxy.LoggingConfig, error) { +func (m *MockLoggingConfigReader) Read(arg0 configuration.ViperConfig) (cfg.LoggingConfig, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Read", arg0) - ret0, _ := ret[0].(proxy.LoggingConfig) + ret0, _ := ret[0].(cfg.LoggingConfig) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/src/app/proxy/config_test.go b/src/app/proxy/config_test.go index bc2513a..400a9da 100644 --- a/src/app/proxy/config_test.go +++ b/src/app/proxy/config_test.go @@ -13,6 +13,7 @@ import ( "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/pixa/src/app/command" "github.com/snivilised/pixa/src/app/mocks" + "github.com/snivilised/pixa/src/cfg" "github.com/snivilised/pixa/src/internal/helpers" ) @@ -61,7 +62,7 @@ func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *configTE, co.Config.Name = helpers.PixaConfigTestFilename co.Config.ConfigPath = configPath co.Config.Viper = &configuration.GlobalViperConfig{} - co.Config.Readers = command.ConfigReaders{ + co.Config.Readers = cfg.ConfigReaders{ Profiles: mockProfilesReader, Schemes: mockSchemesReader, Sampler: mockSamplerReader, diff --git a/src/app/proxy/controller-sampler_test.go b/src/app/proxy/controller-sampler_test.go index e4c309e..6f843fa 100644 --- a/src/app/proxy/controller-sampler_test.go +++ b/src/app/proxy/controller-sampler_test.go @@ -10,6 +10,7 @@ import ( cmocks "github.com/snivilised/cobrass/src/assistant/mocks" "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/pixa/src/app/command" + "github.com/snivilised/pixa/src/cfg" "github.com/snivilised/pixa/src/app/mocks" "github.com/snivilised/pixa/src/internal/helpers" @@ -122,7 +123,7 @@ var _ = Describe("SamplerController", Ordered, func() { co.Config.Name = helpers.PixaConfigTestFilename co.Config.ConfigPath = configPath co.Config.Viper = &configuration.GlobalViperConfig{} - co.Config.Readers = command.ConfigReaders{ + co.Config.Readers = cfg.ConfigReaders{ Profiles: mockProfilesReader, Schemes: mockSchemesReader, Sampler: mockSamplerReader, diff --git a/src/app/proxy/enter-shrink.go b/src/app/proxy/enter-shrink.go index 46ec224..f47c79d 100644 --- a/src/app/proxy/enter-shrink.go +++ b/src/app/proxy/enter-shrink.go @@ -11,6 +11,7 @@ import ( "github.com/snivilised/cobrass/src/assistant/configuration" "github.com/snivilised/extendio/xfs/nav" "github.com/snivilised/extendio/xfs/storage" + "github.com/snivilised/pixa/src/cfg" ) type ShrinkEntry struct { @@ -314,10 +315,10 @@ type ShrinkParams struct { Inputs *ShrinkCommandInputs Program Executor Config configuration.ViperConfig - ProfilesCFG ProfilesConfig - SchemesCFG SchemesConfig - SamplerCFG SamplerConfig - AdvancedCFG AdvancedConfig + ProfilesCFG cfg.ProfilesConfig + SchemesCFG cfg.SchemesConfig + SamplerCFG cfg.SamplerConfig + AdvancedCFG cfg.AdvancedConfig Logger *slog.Logger Vfs storage.VirtualFS } diff --git a/src/app/proxy/entry-base.go b/src/app/proxy/entry-base.go index 1d63fbe..984cf0c 100644 --- a/src/app/proxy/entry-base.go +++ b/src/app/proxy/entry-base.go @@ -13,6 +13,7 @@ import ( "github.com/snivilised/extendio/xfs/nav" "github.com/snivilised/extendio/xfs/storage" "github.com/snivilised/lorax/boost" + "github.com/snivilised/pixa/src/cfg" ) type afterFunc func(*nav.TraverseResult, error) @@ -42,10 +43,10 @@ type EntryBase struct { Config configuration.ViperConfig Options *nav.TraverseOptions Registry *ControllerRegistry - ProfilesCFG ProfilesConfig - SchemesCFG SchemesConfig - SamplerCFG SamplerConfig - AdvancedCFG AdvancedConfig + ProfilesCFG cfg.ProfilesConfig + SchemesCFG cfg.SchemesConfig + SamplerCFG cfg.SamplerConfig + AdvancedCFG cfg.AdvancedConfig Logger *slog.Logger Vfs storage.VirtualFS FileManager *FileManager diff --git a/src/app/proxy/proxy-defs.go b/src/app/proxy/proxy-defs.go index dbd47cd..77eb030 100644 --- a/src/app/proxy/proxy-defs.go +++ b/src/app/proxy/proxy-defs.go @@ -2,15 +2,16 @@ package proxy import ( "github.com/snivilised/extendio/xfs/nav" + "github.com/snivilised/pixa/src/cfg" ) type SharedControllerInfo struct { Options *nav.TraverseOptions program Executor - profiles ProfilesConfig - schemes SchemesConfig - sampler SamplerConfig - advanced AdvancedConfig + profiles cfg.ProfilesConfig + schemes cfg.SchemesConfig + sampler cfg.SamplerConfig + advanced cfg.AdvancedConfig Inputs *ShrinkCommandInputs finder *PathFinder fileManager *FileManager diff --git a/src/app/command/config-defaults.go b/src/cfg/config-defaults.go similarity index 91% rename from src/app/command/config-defaults.go rename to src/cfg/config-defaults.go index c11248b..8be7789 100644 --- a/src/app/command/config-defaults.go +++ b/src/cfg/config-defaults.go @@ -1,4 +1,4 @@ -package command +package cfg import ( "os" @@ -6,7 +6,6 @@ import ( "github.com/pkg/errors" "github.com/snivilised/cobrass/src/clif" - "github.com/snivilised/pixa/src/app/proxy" ) const ( @@ -19,7 +18,7 @@ const ( ) type ( - defaultSchemes map[string]proxy.SchemeConfig + defaultSchemes map[string]SchemeConfig defaultSchemesConfig struct { schemes defaultSchemes } @@ -29,8 +28,8 @@ type ( } ) -func (cfg defaultSchemeConfig) Profiles() []string { - return cfg.profiles +func (c defaultSchemeConfig) Profiles() []string { + return c.profiles } var ( @@ -46,7 +45,7 @@ func init() { // values that don't mean anything. Update to real useable defaults // DefaultProfilesConfig = &MsProfilesConfig{ - Profiles: proxy.ProfilesConfigMap{ + Profiles: ProfilesConfigMap{ "blur": clif.ChangedFlagsMap{ "strip": "true", "interlace": "plane", diff --git a/src/app/command/config-readers.go b/src/cfg/config-readers.go similarity index 84% rename from src/app/command/config-readers.go rename to src/cfg/config-readers.go index 9a1ec15..7f01c5b 100644 --- a/src/app/command/config-readers.go +++ b/src/cfg/config-readers.go @@ -1,4 +1,4 @@ -package command +package cfg import ( "fmt" @@ -7,26 +7,25 @@ import ( "github.com/samber/lo" "github.com/snivilised/cobrass/src/assistant/configuration" "github.com/snivilised/cobrass/src/clif" - "github.com/snivilised/pixa/src/app/proxy" "golang.org/x/exp/maps" ) type MsProfilesConfigReader struct { } -func (r *MsProfilesConfigReader) Read(viper configuration.ViperConfig) (proxy.ProfilesConfig, error) { +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(proxy.ProfilesConfigMap), + Profiles: make(ProfilesConfigMap), } if raw := viper.Get("profiles"); raw != nil { - if profiles, ok := raw.(proxy.ProfilesFlagOptionAsAnyPair); ok { + if profiles, ok := raw.(ProfilesFlagOptionAsAnyPair); ok { for profile, pv := range profiles { - if pair, ok := pv.(proxy.ProfilesFlagOptionAsAnyPair); ok { + if pair, ok := pv.(ProfilesFlagOptionAsAnyPair); ok { profilesCFG.Profiles[profile] = make(clif.ChangedFlagsMap) for flag, optionAsAny := range pair { @@ -44,7 +43,7 @@ func (r *MsProfilesConfigReader) Read(viper configuration.ViperConfig) (proxy.Pr type MsSchemesConfigReader struct{} -func (r *MsSchemesConfigReader) Read(viper configuration.ViperConfig) (proxy.SchemesConfig, error) { +func (r *MsSchemesConfigReader) Read(viper configuration.ViperConfig) (SchemesConfig, error) { var ( schemesCFG MsSchemesConfig ) @@ -56,7 +55,7 @@ func (r *MsSchemesConfigReader) Read(viper configuration.ViperConfig) (proxy.Sch type MsSamplerConfigReader struct{} -func (r *MsSamplerConfigReader) Read(viper configuration.ViperConfig) (proxy.SamplerConfig, error) { +func (r *MsSamplerConfigReader) Read(viper configuration.ViperConfig) (SamplerConfig, error) { var ( samplerCFG MsSamplerConfig ) @@ -110,7 +109,7 @@ func (r *MsAdvancedConfigReader) validateSuffixes(suffixes []string, from string return err } -func (r *MsAdvancedConfigReader) Read(viper configuration.ViperConfig) (proxy.AdvancedConfig, error) { +func (r *MsAdvancedConfigReader) Read(viper configuration.ViperConfig) (AdvancedConfig, error) { var ( advancedCFG MsAdvancedConfig ) @@ -141,7 +140,7 @@ func (r *MsAdvancedConfigReader) Read(viper configuration.ViperConfig) (proxy.Ad type MsLoggingConfigReader struct{} -func (r *MsLoggingConfigReader) Read(viper configuration.ViperConfig) (proxy.LoggingConfig, error) { +func (r *MsLoggingConfigReader) Read(viper configuration.ViperConfig) (LoggingConfig, error) { var ( loggingCFG MsLoggingConfig ) @@ -152,9 +151,9 @@ func (r *MsLoggingConfigReader) Read(viper configuration.ViperConfig) (proxy.Log } type ConfigReaders struct { - Profiles proxy.ProfilesConfigReader - Schemes proxy.SchemesConfigReader - Sampler proxy.SamplerConfigReader - Advanced proxy.AdvancedConfigReader - Logging proxy.LoggingConfigReader + Profiles ProfilesConfigReader + Schemes SchemesConfigReader + Sampler SamplerConfigReader + Advanced AdvancedConfigReader + Logging LoggingConfigReader } diff --git a/src/app/proxy/config.go b/src/cfg/config.go similarity index 92% rename from src/app/proxy/config.go rename to src/cfg/config.go index c90ced3..f926b5d 100644 --- a/src/app/proxy/config.go +++ b/src/cfg/config.go @@ -1,4 +1,4 @@ -package proxy +package cfg import ( "time" @@ -7,7 +7,7 @@ import ( "github.com/snivilised/cobrass/src/clif" ) -//go:generate mockgen -destination ../mocks/mocks-config.go -package mocks -source config.go +//go:generate mockgen -destination ../app/mocks/mocks-config.go -package mocks -source config.go type ( ProfilesConfig interface { diff --git a/src/cfg/ms-config.go b/src/cfg/ms-config.go new file mode 100644 index 0000000..a5ad9fa --- /dev/null +++ b/src/cfg/ms-config.go @@ -0,0 +1,175 @@ +package cfg + +import ( + "fmt" + "time" + + "github.com/snivilised/cobrass/src/clif" +) + +type ( + ProfilesFlagOptionAsAnyPair = map[string]any + ProfilesConfigMap map[string]clif.ChangedFlagsMap +) + +type MsProfilesConfig struct { + Profiles ProfilesConfigMap +} + +func (c MsProfilesConfig) Profile(name string) (clif.ChangedFlagsMap, bool) { + profile, found := c.Profiles[name] + + return profile, found +} + +type MsSchemeConfig struct { + ProfilesData []string `mapstructure:"profiles"` +} + +func (c *MsSchemeConfig) Profiles() []string { + return c.ProfilesData +} + +type MsSchemesConfig map[string]SchemeConfig + +func (c MsSchemesConfig) Validate(name string, profiles ProfilesConfig) error { + if name == "" { + return nil + } + + var ( + found bool + scheme SchemeConfig + ) + + if scheme, found = c[name]; !found { + return fmt.Errorf("scheme: '%v' not found in config", name) + } + + for _, p := range scheme.Profiles() { + if _, found := profiles.Profile(p); !found { + return fmt.Errorf("profile(referenced by scheme: '%v'): '%v' not found in config", + name, p, + ) + } + } + + return nil +} + +func (c MsSchemesConfig) Scheme(name string) (SchemeConfig, bool) { + config, found := c[name] + + return config, found +} + +type MsSamplerConfig struct { + Files uint `mapstructure:"files"` + Folders uint `mapstructure:"folders"` +} + +func (c *MsSamplerConfig) NoFiles() uint { + return c.Files +} + +func (c *MsSamplerConfig) NoFolders() uint { + return c.Folders +} + +type MsLabelsConfig struct { + Adhoc string `mapstructure:"adhoc"` + Journal string `mapstructure:"journal-suffix"` + Legacy string `mapstructure:"legacy"` + Trash string `mapstructure:"trash"` +} + +type MsExtensionsConfig struct { + FileSuffixes string `mapstructure:"suffixes"` + TransformsCSV string `mapstructure:"transforms"` + Remap map[string]string `mapstructure:"map"` +} + +func (c *MsExtensionsConfig) Suffixes() string { + return c.FileSuffixes +} + +func (c *MsExtensionsConfig) Transforms() string { + return c.TransformsCSV +} + +func (c *MsExtensionsConfig) Map() map[string]string { + return c.Remap +} + +type MsAdvancedConfig struct { + Abort bool `mapstructure:"abort-on-error"` + Timeout string `mapstructure:"program-timeout"` + NoProgramRetries uint `mapstructure:"no-program-retries"` + LabelsCFG MsLabelsConfig `mapstructure:"labels"` + ExtensionsCFG MsExtensionsConfig `mapstructure:"extensions"` +} + +func (c *MsAdvancedConfig) AbortOnError() bool { + return c.Abort +} + +func (c *MsAdvancedConfig) ProgramTimeout() (duration time.Duration, err error) { + return time.ParseDuration(c.Timeout) +} + +func (c *MsAdvancedConfig) NoRetries() uint { + return c.NoProgramRetries +} + +func (c *MsAdvancedConfig) AdhocLabel() string { + return c.LabelsCFG.Adhoc +} + +func (c *MsAdvancedConfig) JournalLabel() string { + return c.LabelsCFG.Journal +} + +func (c *MsAdvancedConfig) LegacyLabel() string { + return c.LabelsCFG.Legacy +} + +func (c *MsAdvancedConfig) TrashLabel() string { + return c.LabelsCFG.Trash +} + +func (c *MsAdvancedConfig) Extensions() ExtensionsConfig { + return &c.ExtensionsCFG +} + +type MsLoggingConfig struct { + LogPath string `mapstructure:"log-path"` + MaxSize uint `mapstructure:"max-size"` + MaxBackups uint `mapstructure:"max-backups"` + MaxAge uint `mapstructure:"max-age"` + LogLevel string `mapstructure:"level"` + Format string `mapstructure:"time-format"` +} + +func (c *MsLoggingConfig) Path() string { + return c.LogPath +} + +func (c *MsLoggingConfig) MaxSizeInMb() uint { + return c.MaxSize +} + +func (c *MsLoggingConfig) MaxNoOfBackups() uint { + return c.MaxBackups +} + +func (c *MsLoggingConfig) MaxAgeInDays() uint { + return c.MaxAge +} + +func (c *MsLoggingConfig) Level() string { + return c.LogLevel +} + +func (c *MsLoggingConfig) TimeFormat() string { + return c.Format +} diff --git a/src/internal/helpers/mock-config-data.go b/src/internal/helpers/mock-config-data.go index b5c92f3..49034ec 100644 --- a/src/internal/helpers/mock-config-data.go +++ b/src/internal/helpers/mock-config-data.go @@ -1,11 +1,8 @@ package helpers import ( - "fmt" - "time" - "github.com/snivilised/cobrass/src/clif" - "github.com/snivilised/pixa/src/app/proxy" + "github.com/snivilised/pixa/src/cfg" ) // need to re-think this mock data as this is currently sub-optimal @@ -28,188 +25,13 @@ var ( BackyardWorldsPlanet9Scan01Last4 []string - ProfilesConfigData proxy.ProfilesConfigMap - SchemesConfigData proxy.SchemesConfig - SamplerConfigData proxy.SamplerConfig - AdvancedConfigData proxy.AdvancedConfig - LoggingConfigData proxy.LoggingConfig -) - -type testProfilesConfig struct { - profiles proxy.ProfilesConfigMap -} - -func (cfg testProfilesConfig) Profile(name string) (clif.ChangedFlagsMap, bool) { - profile, found := cfg.profiles[name] - - return profile, found -} - -type ( - testSchemes map[string]proxy.SchemeConfig - testSchemesConfig struct { - schemes testSchemes - } - - testSchemeConfig struct { - profiles []string - } + ProfilesConfigData cfg.ProfilesConfigMap + SchemesConfigData cfg.SchemesConfig + SamplerConfigData cfg.SamplerConfig + AdvancedConfigData cfg.AdvancedConfig + LoggingConfigData cfg.LoggingConfig ) -func (cfg *testSchemeConfig) Profiles() []string { - return cfg.profiles -} - -func (cfg *testSchemesConfig) Validate(name string, profiles proxy.ProfilesConfig) error { - if name == "" { - return nil - } - - var ( - found bool - scheme proxy.SchemeConfig - ) - - if scheme, found = cfg.schemes[name]; !found { - return fmt.Errorf("scheme: '%v' not found in config", name) - } - - for _, p := range scheme.Profiles() { - if _, found := profiles.Profile(p); !found { - return fmt.Errorf("profile(referenced by scheme: '%v'): '%v' not found in config", - name, p, - ) - } - } - - return nil -} - -func (cfg *testSchemesConfig) Scheme(name string) (proxy.SchemeConfig, bool) { - config, found := cfg.schemes[name] - - return config, found -} - -type testSamplerConfig struct { - Files uint - Folders uint -} - -func (cfg *testSamplerConfig) NoFiles() uint { - return cfg.Files -} - -func (cfg *testSamplerConfig) NoFolders() uint { - return cfg.Folders -} - -type testLabelsConfig struct { - Adhoc string - Journal string - Legacy string - Trash string -} - -type testExtensionsConfig struct { - FileSuffixes string - TransformsCSV string - Remap map[string]string -} - -func (cfg *testExtensionsConfig) Suffixes() string { - return cfg.FileSuffixes -} - -func (cfg *testExtensionsConfig) Transforms() string { - return cfg.TransformsCSV -} - -func (cfg *testExtensionsConfig) Map() map[string]string { - return cfg.Remap -} - -type testAdvancedConfig struct { - Abort bool - Timeout string - NoProgramRetries uint - LabelsCFG testLabelsConfig - ExtensionsCFG testExtensionsConfig -} - -func (cfg *testAdvancedConfig) AbortOnError() bool { - return cfg.Abort -} - -func (cfg *testAdvancedConfig) ProgramTimeout() (duration time.Duration, err error) { - return time.ParseDuration(cfg.Timeout) -} - -func (cfg *testAdvancedConfig) NoRetries() uint { - return cfg.NoProgramRetries -} - -func (cfg *testAdvancedConfig) AdhocLabel() string { - return cfg.LabelsCFG.Adhoc -} - -func (cfg *testAdvancedConfig) JournalLabel() string { - return cfg.LabelsCFG.Journal -} - -func (cfg *testAdvancedConfig) LegacyLabel() string { - return cfg.LabelsCFG.Legacy -} - -func (cfg *testAdvancedConfig) TrashLabel() string { - return cfg.LabelsCFG.Trash -} - -func (cfg *testAdvancedConfig) Extensions() proxy.ExtensionsConfig { - return &cfg.ExtensionsCFG -} - -func (cfg *testAdvancedConfig) Suffixes() string { - return cfg.ExtensionsCFG.FileSuffixes -} - -func (cfg *testAdvancedConfig) Transforms() string { - return cfg.ExtensionsCFG.TransformsCSV -} - -type testLoggingConfig struct { - LogPath string - MaxSize uint - MaxBackups uint - MaxAge uint - LogLevel string - Format string -} - -func (cfg *testLoggingConfig) Path() string { - return cfg.LogPath -} - -func (cfg *testLoggingConfig) MaxSizeInMb() uint { - return cfg.MaxSize -} - -func (cfg *testLoggingConfig) MaxNoOfBackups() uint { - return cfg.MaxBackups -} - -func (cfg *testLoggingConfig) MaxAgeInDays() uint { - return cfg.MaxAge -} - -func (cfg *testLoggingConfig) Level() string { - return cfg.LogLevel -} - -func (cfg *testLoggingConfig) TimeFormat() string { - return cfg.Format -} - func init() { // ✅ Keep this up to date with "nasa-scientist-index.xml" // @@ -243,7 +65,7 @@ func init() { "06_Backyard-Worlds-Planet-9_s01.jpg", } - ProfilesConfigData = proxy.ProfilesConfigMap{ + ProfilesConfigData = cfg.ProfilesConfigMap{ "blur": clif.ChangedFlagsMap{ "strip": "true", "interlace": "plane", @@ -263,39 +85,37 @@ func init() { }, } - SchemesConfigData = &testSchemesConfig{ - schemes: testSchemes{ - "blur-sf": &testSchemeConfig{ - profiles: []string{"blur", "sf"}, - }, - "adaptive-sf": &testSchemeConfig{ - profiles: []string{"adaptive", "sf"}, - }, - "adaptive-blur": &testSchemeConfig{ - profiles: []string{"adaptive", "blur"}, - }, - "singleton": &testSchemeConfig{ - profiles: []string{"adaptive"}, - }, + SchemesConfigData = &cfg.MsSchemesConfig{ + "blur-sf": &cfg.MsSchemeConfig{ + ProfilesData: []string{"blur", "sf"}, + }, + "adaptive-sf": &cfg.MsSchemeConfig{ + ProfilesData: []string{"adaptive", "sf"}, + }, + "adaptive-blur": &cfg.MsSchemeConfig{ + ProfilesData: []string{"adaptive", "blur"}, + }, + "singleton": &cfg.MsSchemeConfig{ + ProfilesData: []string{"adaptive"}, }, } - SamplerConfigData = &testSamplerConfig{ + SamplerConfigData = &cfg.MsSamplerConfig{ Files: noSampleFiles, Folders: noSampleFolders, } - AdvancedConfigData = &testAdvancedConfig{ + AdvancedConfigData = &cfg.MsAdvancedConfig{ Abort: false, Timeout: "10s", NoProgramRetries: noRetries, - LabelsCFG: testLabelsConfig{ + LabelsCFG: cfg.MsLabelsConfig{ Adhoc: "ADHOC", Journal: ".journal.txt", Legacy: ".LEGACY", Trash: "TRASH", }, - ExtensionsCFG: testExtensionsConfig{ + ExtensionsCFG: cfg.MsExtensionsConfig{ FileSuffixes: "jpg,jpeg,png", TransformsCSV: "lower", Remap: map[string]string{ @@ -304,7 +124,7 @@ func init() { }, } - LoggingConfigData = &testLoggingConfig{ + LoggingConfigData = &cfg.MsLoggingConfig{ LogPath: "", MaxSize: maxLogSizeInMb, MaxBackups: maxLogBackups, diff --git a/src/internal/helpers/test-utils.go b/src/internal/helpers/test-utils.go index 36f0c06..005f7f9 100644 --- a/src/internal/helpers/test-utils.go +++ b/src/internal/helpers/test-utils.go @@ -16,7 +16,7 @@ import ( ci18n "github.com/snivilised/cobrass/src/assistant/i18n" cmocks "github.com/snivilised/cobrass/src/assistant/mocks" "github.com/snivilised/pixa/src/app/mocks" - "github.com/snivilised/pixa/src/app/proxy" + "github.com/snivilised/pixa/src/cfg" "github.com/snivilised/pixa/src/i18n" "github.com/snivilised/pixa/src/internal/matchers" @@ -199,14 +199,14 @@ func DoMockReadInConfig(config *cmocks.MockViperConfig) { } func DoMockProfilesConfigsWith( - data proxy.ProfilesConfigMap, + data cfg.ProfilesConfigMap, config configuration.ViperConfig, reader *mocks.MockProfilesConfigReader, ) { reader.EXPECT().Read(config).DoAndReturn( - func(viper configuration.ViperConfig) (proxy.ProfilesConfig, error) { - stub := &testProfilesConfig{ - profiles: data, + func(viper configuration.ViperConfig) (cfg.ProfilesConfig, error) { + stub := &cfg.MsProfilesConfig{ + Profiles: data, } return stub, nil @@ -215,12 +215,12 @@ func DoMockProfilesConfigsWith( } func DoMockSchemesConfigWith( - data proxy.SchemesConfig, + data cfg.SchemesConfig, config configuration.ViperConfig, reader *mocks.MockSchemesConfigReader, ) { reader.EXPECT().Read(config).DoAndReturn( - func(viper configuration.ViperConfig) (proxy.SchemesConfig, error) { + func(viper configuration.ViperConfig) (cfg.SchemesConfig, error) { stub := data return stub, nil @@ -229,12 +229,12 @@ func DoMockSchemesConfigWith( } func DoMockSamplerConfigWith( - data proxy.SamplerConfig, + data cfg.SamplerConfig, config configuration.ViperConfig, reader *mocks.MockSamplerConfigReader, ) { reader.EXPECT().Read(config).DoAndReturn( - func(viper configuration.ViperConfig) (proxy.SamplerConfig, error) { + func(viper configuration.ViperConfig) (cfg.SamplerConfig, error) { stub := data return stub, nil @@ -243,12 +243,12 @@ func DoMockSamplerConfigWith( } func DoMockAdvancedConfigWith( - data proxy.AdvancedConfig, + data cfg.AdvancedConfig, config configuration.ViperConfig, reader *mocks.MockAdvancedConfigReader, ) { reader.EXPECT().Read(config).DoAndReturn( - func(viper configuration.ViperConfig) (proxy.AdvancedConfig, error) { + func(viper configuration.ViperConfig) (cfg.AdvancedConfig, error) { stub := data return stub, nil @@ -257,12 +257,12 @@ func DoMockAdvancedConfigWith( } func DoMockLoggingConfigWith( - data proxy.LoggingConfig, + data cfg.LoggingConfig, config configuration.ViperConfig, reader *mocks.MockLoggingConfigReader, ) { reader.EXPECT().Read(config).DoAndReturn( - func(viper configuration.ViperConfig) (proxy.LoggingConfig, error) { + func(viper configuration.ViperConfig) (cfg.LoggingConfig, error) { stub := data return stub, nil