Skip to content

Commit

Permalink
feat(proxy): add vfs and implement strategies (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 16, 2023
1 parent d208d8a commit ae51059
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 48 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"varcheck",
"watchv",
"wgan",
"Wrapf",
"xutils"
]
}
2 changes: 2 additions & 0 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/snivilised/cobrass/src/assistant/configuration"
ci18n "github.com/snivilised/cobrass/src/assistant/i18n"
xi18n "github.com/snivilised/extendio/i18n"
"github.com/snivilised/extendio/xfs/storage"
"github.com/snivilised/extendio/xfs/utils"
"github.com/snivilised/pixa/src/app/proxy"
"github.com/snivilised/pixa/src/i18n"
Expand Down Expand Up @@ -67,6 +68,7 @@ type Bootstrap struct {
OptionsInfo ConfigureOptionsInfo
ProfilesCFG proxy.ProfilesConfig
SamplerCFG proxy.SamplerConfig
Vfs storage.VirtualFS
}

type ConfigureOptionsInfo struct {
Expand Down
4 changes: 3 additions & 1 deletion src/app/command/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ var _ = Describe("Bootstrap", Ordered, func() {

Context("given: root defined with magick sub-command", func() {
It("🧪 should: setup command without error", func() {
bootstrap := command.Bootstrap{}
bootstrap := command.Bootstrap{
Vfs: nfs,
}
rootCmd := bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &DetectorStub{}
co.Program = &ExecutorStub{
Expand Down
4 changes: 3 additions & 1 deletion src/app/command/magick-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ var _ = Describe("MagickCmd", Ordered, func() {

When("specified flags are valid", func() {
It("🧪 should: execute without error", func() {
bootstrap := command.Bootstrap{}
bootstrap := command.Bootstrap{
Vfs: nfs,
}
tester := helpers.CommandTester{
Args: []string{"mag"},
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
Expand Down
4 changes: 3 additions & 1 deletion src/app/command/root-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ var _ = Describe("RootCmd", Ordered, func() {
})

BeforeEach(func() {
bootstrap := command.Bootstrap{}
bootstrap := command.Bootstrap{
Vfs: nfs,
}
tester = helpers.CommandTester{
Args: []string{"./"},
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
Expand Down
1 change: 1 addition & 0 deletions src/app/command/shrink-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
b.OptionsInfo.Config.Viper,
b.ProfilesCFG,
b.SamplerCFG,
b.Vfs,
)
} else {
return xvErr
Expand Down
16 changes: 9 additions & 7 deletions src/app/command/shrink-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type shrinkTE struct {
directory string
}

func expectValidShrinkCmdInvocation(entry *shrinkTE) {
bootstrap := command.Bootstrap{}
func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *shrinkTE) {
bootstrap := command.Bootstrap{
Vfs: vfs,
}

// we also prepend the directory name to the command line
//
Expand Down Expand Up @@ -102,7 +104,7 @@ var _ = Describe("ShrinkCmd", Ordered, func() {
//
entry.directory = l10nPath
entry.configPath = configPath
expectValidShrinkCmdInvocation(entry)
expectValidShrinkCmdInvocation(nfs, entry)
},
func(entry *shrinkTE) string {
return fmt.Sprintf("🧪 ===> given: '%v'", entry.message)
Expand Down Expand Up @@ -203,7 +205,7 @@ var _ = Describe("ShrinkCmd", Ordered, func() {
},
}

expectValidShrinkCmdInvocation(entry)
expectValidShrinkCmdInvocation(nfs, entry)
})

It("🧪 should: execute successfully", func() {
Expand All @@ -218,7 +220,7 @@ var _ = Describe("ShrinkCmd", Ordered, func() {
},
}

expectValidShrinkCmdInvocation(entry)
expectValidShrinkCmdInvocation(nfs, entry)
})
})

Expand All @@ -235,7 +237,7 @@ var _ = Describe("ShrinkCmd", Ordered, func() {
},
}

expectValidShrinkCmdInvocation(entry)
expectValidShrinkCmdInvocation(nfs, entry)
})

It("🧪 should: execute successfully", func() {
Expand All @@ -250,7 +252,7 @@ var _ = Describe("ShrinkCmd", Ordered, func() {
},
}

expectValidShrinkCmdInvocation(entry)
expectValidShrinkCmdInvocation(nfs, entry)
})
})
})
8 changes: 5 additions & 3 deletions src/app/proxy/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"github.com/snivilised/pixa/src/internal/matchers"
)

func expectValidShrinkCmdInvocation(entry *configTE) {
bootstrap := command.Bootstrap{}
func expectValidShrinkCmdInvocation(vfs storage.VirtualFS, entry *configTE) {
bootstrap := command.Bootstrap{
Vfs: vfs,
}

options := []string{
entry.comm, entry.file,
Expand Down Expand Up @@ -115,7 +117,7 @@ var _ = Describe("Config", Ordered, func() {
_ = actual

Expect(1).To(Equal(1))
expectValidShrinkCmdInvocation(entry)
expectValidShrinkCmdInvocation(nfs, entry)
} else {
actual := entry.actual(entry)
entry.assert(entry, actual)
Expand Down
28 changes: 23 additions & 5 deletions src/app/proxy/enter-shrink.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/samber/lo"
"github.com/snivilised/cobrass/src/assistant/configuration"
"github.com/snivilised/extendio/xfs/nav"
"github.com/snivilised/extendio/xfs/storage"
)

type ShrinkEntry struct {
Expand Down Expand Up @@ -100,12 +101,22 @@ func (e *ShrinkEntry) PrincipalOptionsFn(o *nav.TraverseOptions) {
}

func (e *ShrinkEntry) createFinder() *PathFinder {
return &PathFinder{
Behaviours: strategies{
output: inlineOutputStrategy{}, // ejectOutputStrategy{}
deletion: inlineDeletionStrategy{}, // ejectDeletionStrategy{}
finder := &PathFinder{
behaviours: strategies{
output: inlineOutputStrategy{},
deletion: inlineDeletionStrategy{},
},
}

if e.Inputs.ParamSet.Native.OutputPath != "" {
finder.behaviours.output = &ejectOutputStrategy{}
}

if e.Inputs.ParamSet.Native.TrashPath != "" {
finder.behaviours.deletion = &ejectOutputStrategy{}
}

return finder
}

func (e *ShrinkEntry) ConfigureOptions(o *nav.TraverseOptions) {
Expand All @@ -121,14 +132,19 @@ func (e *ShrinkEntry) ConfigureOptions(o *nav.TraverseOptions) {

e.EntryBase.ConfigureOptions(o)

finder := e.createFinder()
e.Registry = NewRunnerRegistry(&SharedRunnerInfo{
Type: RunnerTypeSamplerEn, // TODO: to come from an arg !!!
Options: e.Options,
program: e.Program,
profiles: e.ProfilesCFG,
sampler: e.SamplerCFG,
Inputs: e.Inputs,
finder: e.createFinder(),
finder: finder,
fileManager: &FileManager{
vfs: e.Vfs,
finder: finder,
},
})
}

Expand Down Expand Up @@ -216,6 +232,7 @@ func EnterShrink(
config configuration.ViperConfig,
profilesCFG ProfilesConfig,
samplerCFG SamplerConfig,
vfs storage.VirtualFS,
) error {
fmt.Printf("---> 🔊🔊 Directory: '%v'\n", inputs.Root.ParamSet.Native.Directory)

Expand All @@ -226,6 +243,7 @@ func EnterShrink(
Config: config,
ProfilesCFG: profilesCFG,
SamplerCFG: samplerCFG,
Vfs: vfs,
},
Inputs: inputs,
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/proxy/entry-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/samber/lo"
"github.com/snivilised/cobrass/src/assistant/configuration"
"github.com/snivilised/extendio/xfs/nav"
"github.com/snivilised/extendio/xfs/storage"
"github.com/snivilised/lorax/boost"
)

Expand Down Expand Up @@ -40,6 +41,7 @@ type EntryBase struct {
Registry *RunnerRegistry
ProfilesCFG ProfilesConfig
SamplerCFG SamplerConfig
Vfs storage.VirtualFS
}

func (e *EntryBase) ConfigureOptions(o *nav.TraverseOptions) {
Expand Down
10 changes: 2 additions & 8 deletions src/app/proxy/execution-step.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type Sequence []Step
// and output file names; this is the responsibility of the runner, which uses
// the path-finder to accomplish that task.
type magickStep struct {
fileManager *FileManager
program Executor
shared *SharedRunnerInfo
thirdPartyCL clif.ThirdPartyCommandLine
sourcePath string
outputPath string
Expand All @@ -29,12 +28,7 @@ type magickStep struct {
func (s *magickStep) Run() error {
positional := []string{s.sourcePath}

// prepare: move existing file out of the way

err := s.program.Execute(clif.Expand(positional, s.thirdPartyCL)...)

// invoke deletions
// delete journal file
err := s.shared.program.Execute(clif.Expand(positional, s.thirdPartyCL)...)

return err
}
47 changes: 46 additions & 1 deletion src/app/proxy/file-manager.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
package proxy

type FileManager struct{}
import (
"github.com/pkg/errors"
"github.com/snivilised/extendio/xfs/storage"
)

type FileManager struct {
vfs storage.VirtualFS
finder *PathFinder
}

func (fm *FileManager) setup(source string) error {

Check failure on line 13 in src/app/proxy/file-manager.go

View workflow job for this annotation

GitHub Actions / lint

block should not start with a whitespace (wsl)
// prepare: move existing file out of the way

// https://pkg.go.dev/os#Rename LinkError may result
//
// this might not be right. it may be that we want to leave the
// original alone and create other outputs; in this scenario
// we don't want to rename/move the source...
//
destination := fm.finder.Destination(source)

if err := fm.vfs.Rename(source, destination); err != nil {
return errors.Wrapf(err, "could not complete setup for '%v'", source)
}

return nil
}

func (fm *FileManager) move(from, to string) error {
_, _ = from, to

return nil
}

func (fm *FileManager) delete(target string) error {
_ = target

return nil
}

func (fm *FileManager) tidy() error {
// invoke deletions
// delete journal file
//
return nil
}
14 changes: 13 additions & 1 deletion src/app/proxy/path-finder.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package proxy

import (
"path/filepath"
)

// INLINE-MODE: EJECT | INLINE (should we call this a strategy?
// they do the same thing but create a different output structure => OutputStrategy)
//
Expand Down Expand Up @@ -81,7 +85,15 @@ type PathFinder struct {
// I think this depends on the mode (tidy/preserve)
Trash string

Behaviours strategies
behaviours strategies
}

func (f *PathFinder) Destination(source string) string {
// may be we also return a bool that indicates weather a rename
// should be implemented or not. this depends on the appropriate
// strategy. Or if we dont need to rename, we return an empty string;
// this is the preferred solution.
return filepath.Join(f.Output, source)
}

/*
Expand Down
15 changes: 8 additions & 7 deletions src/app/proxy/proxy-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ const (
)

type SharedRunnerInfo struct {
Type RunnerTypeEnum
Options *nav.TraverseOptions
program Executor
profiles ProfilesConfig
sampler SamplerConfig
Inputs *ShrinkCommandInputs
finder *PathFinder
Type RunnerTypeEnum
Options *nav.TraverseOptions
program Executor
profiles ProfilesConfig
sampler SamplerConfig
Inputs *ShrinkCommandInputs
finder *PathFinder
fileManager *FileManager
}

// ItemController
Expand Down
Loading

0 comments on commit ae51059

Please sign in to comment.