Skip to content

Commit

Permalink
ref(proxy): move config into command pkg (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Jan 4, 2024
1 parent ae69373 commit 08142fa
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 160 deletions.
26 changes: 23 additions & 3 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"fmt"
"os"
"path/filepath"

"github.com/cubiest/jibberjabber"
"github.com/samber/lo"
Expand All @@ -18,9 +19,28 @@ import (
"github.com/snivilised/extendio/xfs/utils"
"github.com/snivilised/pixa/src/app/proxy"
"github.com/snivilised/pixa/src/i18n"
"github.com/snivilised/pixa/src/internal/helpers"
)

func ResolvePath(path string) string {
if path == "" {
return path
}

result := path

if result[0] == '~' {
if h, err := os.UserHomeDir(); err == nil {
result = filepath.Join(h, result[1:])
}
} else {
if absolute, absErr := filepath.Abs(path); absErr == nil {
result = absolute
}
}

return result
}

type LocaleDetector interface {
Scan() language.Tag
}
Expand All @@ -43,7 +63,7 @@ func validatePositionalArgs(cmd *cobra.Command, args []string) error {
return err
}

directory := helpers.ResolvePath(args[0])
directory := ResolvePath(args[0])

if !utils.Exists(directory) {
return xi18n.NewPathNotFoundError("shrink directory", directory)
Expand Down Expand Up @@ -137,7 +157,7 @@ func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command {
fmt.Printf(" ===> 🌷🌷🌷 Root Command...\n")

inputs := b.getRootInputs()
inputs.ParamSet.Native.Directory = helpers.ResolvePath(args[0])
inputs.ParamSet.Native.Directory = ResolvePath(args[0])

if inputs.WorkerPoolFam.Native.CPU {
inputs.WorkerPoolFam.Native.NoWorkers = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package proxy
package command

import (
"github.com/snivilised/cobrass/src/clif"
"github.com/snivilised/pixa/src/app/proxy"
)

const (
Expand All @@ -10,10 +11,25 @@ const (
defaultNoProgramRetries = 2
)

type (
defaultSchemes map[string]proxy.SchemeConfig // should be the proxy interface
defaultSchemesConfig struct {
schemes defaultSchemes
}

defaultSchemeConfig struct {
profiles []string
}
)

func (cfg defaultSchemeConfig) Profiles() []string {
return cfg.profiles
}

var (
DefaultProfilesConfig *MsProfilesConfig
DefaultSamplerConfig *MsSamplerConfig
DefaultSchemesConfig *MsSchemesConfig
DefaultSchemesConfig *defaultSchemesConfig
DefaultAdvancedConfig *MsAdvancedConfig
)

Expand All @@ -22,7 +38,7 @@ func init() {
// values that don't mean anything. Update to real useable defaults
//
DefaultProfilesConfig = &MsProfilesConfig{
Profiles: ProfilesConfigMap{
Profiles: proxy.ProfilesConfigMap{
"blur": clif.ChangedFlagsMap{
"strip": "true",
"interlace": "plane",
Expand All @@ -43,15 +59,18 @@ func init() {
},
}

DefaultSchemesConfig = &MsSchemesConfig{
"blur-sf": MsSchemeConfig{
Profiles: []string{"blur", "sf"},
},
"adaptive-sf": MsSchemeConfig{
Profiles: []string{"adaptive", "sf"},
},
"adaptive-blur": MsSchemeConfig{
Profiles: []string{"adaptive", "blur"},
// tbd: repatriate MsSchemesConfig
DefaultSchemesConfig = &defaultSchemesConfig{
schemes: defaultSchemes{
"blur-sf": &defaultSchemeConfig{
profiles: []string{"blur", "sf"},
},
"adaptive-sf": &defaultSchemeConfig{
profiles: []string{"adaptive", "sf"},
},
"adaptive-blur": &defaultSchemeConfig{
profiles: []string{"adaptive", "blur"},
},
},
}

Expand Down
8 changes: 4 additions & 4 deletions src/app/command/config-readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (r *MsProfilesConfigReader) Read(viper configuration.ViperConfig) (proxy.Pr
// the config, but extendio is not aware of config, so it can't
// check. Instead, we can check here.
//
profilesCFG := &proxy.MsProfilesConfig{
profilesCFG := &MsProfilesConfig{
Profiles: make(proxy.ProfilesConfigMap),
}

Expand All @@ -43,7 +43,7 @@ type MsSchemesConfigReader struct{}

func (r *MsSchemesConfigReader) Read(viper configuration.ViperConfig) (proxy.SchemesConfig, error) {
var (
schemesCFG proxy.MsSchemesConfig
schemesCFG MsSchemesConfig
)

err := viper.UnmarshalKey("schemes", &schemesCFG)
Expand All @@ -55,7 +55,7 @@ type MsSamplerConfigReader struct{}

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

err := viper.UnmarshalKey("sampler", &samplerCFG)
Expand All @@ -67,7 +67,7 @@ type MsAdvancedConfigReader struct{}

func (r *MsAdvancedConfigReader) Read(viper configuration.ViperConfig) (proxy.AdvancedConfig, error) {
var (
advancedCFG proxy.MsAdvancedConfig
advancedCFG MsAdvancedConfig
)

err := viper.UnmarshalKey("advanced", &advancedCFG)
Expand Down
113 changes: 113 additions & 0 deletions src/app/command/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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 MsAdvancedConfig struct {
Abort bool `mapstructure:"abort-on-error"`
Timeout string `mapstructure:"program-timeout"`
NoProgramRetries uint `mapstructure:"no-program-retries"`
Labels MsLabelsConfig `mapstructure:"labels"`
}

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.Labels.Adhoc
}

func (cfg *MsAdvancedConfig) JournalLabel() string {
return cfg.Labels.Journal
}

func (cfg *MsAdvancedConfig) LegacyLabel() string {
return cfg.Labels.Legacy
}

func (cfg *MsAdvancedConfig) TrashLabel() string {
return cfg.Labels.Trash
}
9 changes: 9 additions & 0 deletions src/app/command/shrink-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ 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/internal/helpers"
"go.uber.org/mock/gomock"

"github.com/snivilised/extendio/xfs/storage"
)

var (
_ proxy.ProfilesConfig = &command.MsProfilesConfig{}
_ proxy.SamplerConfig = &command.MsSamplerConfig{}
_ proxy.ProfilesConfigReader = &command.MsProfilesConfigReader{}
_ proxy.SamplerConfigReader = &command.MsSamplerConfigReader{}
_ proxy.AdvancedConfigReader = &command.MsAdvancedConfigReader{}
)

const (
BackyardWorldsPlanet9Scan01 = "nasa/exo/Backyard Worlds - Planet 9/sessions/scan-01"
)
Expand Down
41 changes: 39 additions & 2 deletions src/app/mocks/mocks-config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 08142fa

Please sign in to comment.