Skip to content

Commit

Permalink
feat(proxy): add basic path-finder transparent mode (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Dec 20, 2023
1 parent ae51059 commit 3028997
Show file tree
Hide file tree
Showing 21 changed files with 1,240 additions and 225 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ dist/
.task/

src/i18n/out/en-US/active.en-GB.json
test/data/storage/scientist/
test/data/research/
test/data/research/scientist/

.DS_Store
thumbs.db
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
"--fast"
],
"cSpell.words": [
"beezledub",
"bodyclose",
"chardata",
"clif",
"cmds",
"cmock",
"cmocks",
"cobrass",
"cubiest",
"deadcode",
Expand All @@ -19,6 +22,7 @@
"errcheck",
"exportloopref",
"extendio",
"faydeaudeau",
"fieldalignment",
"GOARCH",
"goconst",
Expand Down
18 changes: 1 addition & 17 deletions src/app/command/magick-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

ci18n "github.com/snivilised/cobrass/src/assistant/i18n"
xi18n "github.com/snivilised/extendio/i18n"
"github.com/snivilised/extendio/xfs/storage"
"github.com/snivilised/pixa/src/app/command"
"github.com/snivilised/pixa/src/i18n"
"github.com/snivilised/pixa/src/internal/helpers"
"github.com/snivilised/pixa/src/internal/matchers"
)
Expand All @@ -31,22 +29,8 @@ var _ = Describe("MagickCmd", Ordered, func() {

BeforeEach(func() {
xi18n.ResetTx()
err := xi18n.Use(func(uo *xi18n.UseOptions) {
uo.From = xi18n.LoadFrom{
Path: l10nPath,
Sources: xi18n.TranslationFiles{
i18n.PixaSourceID: xi18n.TranslationSource{
Name: "pixa",
},

ci18n.CobrassSourceID: xi18n.TranslationSource{
Name: "cobrass",
},
},
}
})

if err != nil {
if err := helpers.UseI18n(l10nPath); err != nil {
Fail(err.Error())
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/app/command/root-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (b *Bootstrap) buildRootCommand(container *assistant.CobraContainer) {
profileFam := assistant.NewParamSet[store.ProfileParameterSet](rootCommand)
profileFam.Native.BindAll(profileFam, rootCommand.PersistentFlags())

rootCommand.Args = validatePositionalArgs
// ??? rootCommand.Args = validatePositionalArgs

container.MustRegisterParamSet(RootPsName, paramSet)
container.MustRegisterParamSet(PreviewFamName, previewFam)
Expand Down
9 changes: 6 additions & 3 deletions src/app/command/root-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ var _ = Describe("RootCmd", Ordered, func() {

configPath = filepath.Join(repo, "test", "data", "configuration")
Expect(matchers.AsDirectory(configPath)).To(matchers.ExistInFS(nfs))

if err := helpers.UseI18n(l10nPath); err != nil {
Fail(err.Error())
}
})

BeforeEach(func() {
bootstrap := command.Bootstrap{
Vfs: nfs,
}
tester = helpers.CommandTester{
Args: []string{"./"},
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &DetectorStub{}
co.Program = &ExecutorStub{
Expand All @@ -65,14 +68,14 @@ var _ = Describe("RootCmd", Ordered, func() {
return fmt.Sprintf("🧪 given: '%v', should: execute", entry.given)
},

Entry(
XEntry(
nil, &rootTE{
given: "just a positional",
commandLine: []string{"./"},
},
),

Entry(
XEntry(
nil, &rootTE{
given: "a family defined switch (--dry-run)",
commandLine: []string{"./", "--dry-run"},
Expand Down
5 changes: 4 additions & 1 deletion src/app/command/shrink-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
shrinkPS.Native.ThirdPartySet.KnownBy,
)

// changed is incorrect; it only contains the third party args,
// all the native args are being omitted

shrinkPS.Native.ThirdPartySet.LongChangedCL = changed

fmt.Printf("%v %v Running shrink, with options: '%v', args: '%v'\n",
Expand Down Expand Up @@ -351,7 +354,7 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob
// the bootstrap, then access it from the func, instead of using
// validatePositionalArgs
//
shrinkCommand.Args = validatePositionalArgs
// shrinkCommand.Args = validatePositionalArgs

return shrinkCommand
}
Expand Down
20 changes: 1 addition & 19 deletions src/app/command/shrink-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

ci18n "github.com/snivilised/cobrass/src/assistant/i18n"
"github.com/snivilised/pixa/src/app/command"
"github.com/snivilised/pixa/src/i18n"
"github.com/snivilised/pixa/src/internal/helpers"
"github.com/snivilised/pixa/src/internal/matchers"

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

Expand Down Expand Up @@ -77,22 +74,7 @@ var _ = Describe("ShrinkCmd", Ordered, func() {
})

BeforeEach(func() {
err := xi18n.Use(func(uo *xi18n.UseOptions) {
uo.From = xi18n.LoadFrom{
Path: l10nPath,
Sources: xi18n.TranslationFiles{
i18n.PixaSourceID: xi18n.TranslationSource{
Name: "dummy-cobrass",
},

ci18n.CobrassSourceID: xi18n.TranslationSource{
Name: "dummy-cobrass",
},
},
}
})

if err != nil {
if err := helpers.UseI18n(l10nPath); err != nil {
Fail(err.Error())
}
})
Expand Down
6 changes: 4 additions & 2 deletions src/app/proxy/enter-shrink.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ func (e *ShrinkEntry) PrincipalOptionsFn(o *nav.TraverseOptions) {

func (e *ShrinkEntry) createFinder() *PathFinder {
finder := &PathFinder{
Scheme: e.Inputs.Root.ProfileFam.Native.Scheme,
Profile: e.Inputs.Root.ProfileFam.Native.Profile,
behaviours: strategies{
output: inlineOutputStrategy{},
deletion: inlineDeletionStrategy{},
output: &inlineOutputStrategy{},
deletion: &inlineDeletionStrategy{},
},
}

Expand Down
17 changes: 17 additions & 0 deletions src/app/proxy/entry-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package proxy
import (
"context"
"fmt"
"io/fs"
"os"
"time"

"github.com/samber/lo"
Expand Down Expand Up @@ -46,6 +48,21 @@ type EntryBase struct {

func (e *EntryBase) ConfigureOptions(o *nav.TraverseOptions) {
e.Options = o
o.Hooks.QueryStatus = func(path string) (os.FileInfo, error) {
fi, err := e.Vfs.Lstat(path)

return fi, err
}
o.Hooks.ReadDirectory = func(dirname string) ([]fs.DirEntry, error) {
contents, err := e.Vfs.ReadDir(dirname)
if err != nil {
return nil, err
}

return lo.Filter(contents, func(item fs.DirEntry, index int) bool {
return item.Name() != ".DS_Store"
}), nil
}

// TODO: to apply the folder filters in combination with these
// file filters, we need to define a custom compound
Expand Down
10 changes: 5 additions & 5 deletions src/app/proxy/execution-step.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// Step
type Step interface {
Run() error
Run(*SharedRunnerInfo) error
}

// Sequence
Expand All @@ -19,16 +19,16 @@ type Sequence []Step
type magickStep struct {
shared *SharedRunnerInfo
thirdPartyCL clif.ThirdPartyCommandLine
scheme string
profile string
sourcePath string
outputPath string
journalPath string
}

// Run
func (s *magickStep) Run() error {
func (s *magickStep) Run(*SharedRunnerInfo) error {
positional := []string{s.sourcePath}

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

return err
return s.shared.program.Execute(clif.Expand(positional, s.thirdPartyCL, s.outputPath)...)
}
45 changes: 38 additions & 7 deletions src/app/proxy/file-manager.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
package proxy

import (
"fmt"
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/snivilised/extendio/xfs/nav"
"github.com/snivilised/extendio/xfs/storage"
)

const (
beezledub = os.FileMode(0o666)
)

// FileManager knows how to translate requests into invocations on the file
// system and nothing else.
type FileManager struct {
vfs storage.VirtualFS
finder *PathFinder
}

func (fm *FileManager) setup(source string) error {
// prepare: move existing file out of the way

// Setup prepares for operation by moving existing file out of the way,
// if applicable.
func (fm *FileManager) Setup(item *nav.TraverseItem) error {
// 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)
from := &destinationInfo{
item: item,
origin: item.Parent.Path,
transparent: true, // might come from a flag
}

if folder, file := fm.finder.Destination(from); folder != "" {
if err := fm.vfs.MkdirAll(folder, beezledub); err != nil {
return errors.Wrapf(err, "could not create parent setup for '%v'", item.Path)
}

destination := filepath.Join(folder, file)

if !fm.vfs.FileExists(item.Path) {
return fmt.Errorf("source file: '%v' does not exist", item.Path)
}

if fm.vfs.FileExists(destination) {
return fmt.Errorf("destination file: '%v' already exists", destination)
}

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

return nil
Expand All @@ -40,7 +71,7 @@ func (fm *FileManager) delete(target string) error {
return nil
}

func (fm *FileManager) tidy() error {
func (fm *FileManager) Tidy() error {
// invoke deletions
// delete journal file
//
Expand Down
Loading

0 comments on commit 3028997

Please sign in to comment.