Skip to content

Commit

Permalink
feat(proxy): implement dry run mode (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Feb 1, 2024
1 parent 486d37c commit 8b6ef83
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (b *Bootstrap) logger() *slog.Logger {
logPath = utils.ResolvePath(logPath)
logPath, _ = utils.EnsurePathAt(logPath, defaultLogFilename, perm, b.Vfs)

ws := zapcore.AddSync(&lumberjack.Logger{
sync := zapcore.AddSync(&lumberjack.Logger{
Filename: logPath,
MaxSize: int(b.LoggingCFG.MaxSizeInMb()),
MaxBackups: int(b.LoggingCFG.MaxNoOfBackups()),
Expand All @@ -285,7 +285,7 @@ func (b *Bootstrap) logger() *slog.Logger {
config.EncodeTime = zapcore.TimeEncoderOfLayout(b.LoggingCFG.TimeFormat())
core := zapcore.NewCore(
zapcore.NewJSONEncoder(config),
ws,
sync,
b.level(b.LoggingCFG.Level()),
)

Expand Down
14 changes: 14 additions & 0 deletions src/app/mocks/mocks-config.go

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

1 change: 1 addition & 0 deletions src/app/proxy/common/static-info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type StaticInfo struct {
Meta JournalMetaInfo
Legacy string
Trash string
Fake string
}

func (i *StaticInfo) JournalLocation(name, parent string) string {
Expand Down
17 changes: 16 additions & 1 deletion src/app/proxy/enter-shrink.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,26 @@ func EnterShrink(
if agent, err = ipc.New(
params.Configs.Advanced,
params.Inputs.ParamSet.Native.KnownBy,
params.Vfs,
params.Inputs.Root.PreviewFam.Native.DryRun,
); err != nil {
if errors.Is(err, ipc.ErrUsingDummyExecutor) {
if errors.Is(err, ipc.ErrUseDummyExecutor) {
// todo: notify ui via bubbletea
//
fmt.Printf("===> 💥💥💥 REVERTING TO DUMMY EXECUTOR !!!!\n")

agent = ipc.Pacify(
params.Configs.Advanced,
params.Inputs.ParamSet.Native.KnownBy,
params.Vfs,
ipc.PacifyWithDummy,
)
} else if errors.Is(err, ipc.ErrUnsupportedExecutor) {
fmt.Printf("===> 💥💥💥 Undefined EXECUTOR: '%v' !!!!\n",
params.Configs.Advanced.Executable().Symbol(),
)

return err
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/app/proxy/filing/dry-run-finder-decorator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package filing

import (
"path/filepath"

"github.com/snivilised/extendio/xfs/nav"
"github.com/snivilised/pixa/src/app/proxy/common"
)

func ComposeFake(name, label string) string {
return FilenameWithoutExtension(name) + label + filepath.Ext(name)
}

type dryRunPathFinderDecorator struct {
decorated *PathFinder
}

func (d *dryRunPathFinderDecorator) Transfer(info *common.PathInfo) (folder, file string) {
if d.decorated.TransparentInput() {
folder = info.Origin
file = info.Item.Extension.Name
} else {
folder, file = d.decorated.Transfer(info)
}

return folder, file
}

func (d *dryRunPathFinderDecorator) Result(info *common.PathInfo) (folder, file string) {
if d.decorated.TransparentInput() {
folder = info.Origin
file = ComposeFake(info.Item.Extension.Name, d.decorated.Statics().Fake)
} else {
folder, file = d.decorated.Result(info)
}

return folder, file
}

func (d *dryRunPathFinderDecorator) TransparentInput() bool {
return d.decorated.TransparentInput()
}

func (d *dryRunPathFinderDecorator) JournalFullPath(item *nav.TraverseItem) string {
return d.decorated.JournalFullPath(item)
}

func (d *dryRunPathFinderDecorator) Statics() *common.StaticInfo {
return d.decorated.Statics()
}

func (d *dryRunPathFinderDecorator) Scheme() string {
return d.decorated.Scheme()
}
7 changes: 7 additions & 0 deletions src/app/proxy/filing/path-finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func NewFinder(
Adhoc: advancedCFG.AdhocLabel(),
Legacy: advancedCFG.LegacyLabel(),
Trash: advancedCFG.TrashLabel(),
Fake: advancedCFG.FakeLabel(),
},
Ext: &ExtensionTransformation{
Transformers: strings.Split(extensions.Transforms(), ","),
Expand Down Expand Up @@ -67,6 +68,12 @@ func NewFinder(
Tag: common.JournalTag,
}

if inputs.Root.PreviewFam.Native.DryRun {
return &dryRunPathFinderDecorator{
decorated: finder,
}
}

return finder
}

Expand Down
64 changes: 48 additions & 16 deletions src/app/proxy/ipc/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package ipc

import (
"github.com/snivilised/cobrass/src/clif"
"github.com/snivilised/extendio/xfs/storage"
"github.com/snivilised/pixa/src/app/proxy/common"
"github.com/snivilised/pixa/src/cfg"
)

const (
PacifyWithDummy = true
PacifyWithFake = false
)

type baseAgent struct {
knownBy clif.KnownByCollection
program common.Executor
Expand All @@ -14,22 +20,18 @@ type baseAgent struct {
func New(
advanced cfg.AdvancedConfig,
knownBy clif.KnownByCollection,
vfs storage.VirtualFS,
dryRun bool,
) (common.ExecutionAgent, error) {
var (
agent common.ExecutionAgent
// dummy uses the same agent as magick
//
dummy = &magickAgent{
baseAgent{
knownBy: knownBy,
program: &DummyExecutor{
Name: advanced.Executable().Symbol(),
},
},
}
err error
err error
)

if dryRun {
return Pacify(advanced, knownBy, vfs, PacifyWithFake), nil
}

switch advanced.Executable().Symbol() {
case "magick":
agent = &magickAgent{
Expand All @@ -42,16 +44,46 @@ func New(
}

if !agent.IsInstalled() {
err = ErrUsingDummyExecutor

agent = dummy
err = ErrUseDummyExecutor
}

case "dummy":
err = ErrUsingDummyExecutor
agent = Pacify(advanced, knownBy, vfs, PacifyWithDummy)

agent = dummy
case "fake":
agent = Pacify(advanced, knownBy, vfs, PacifyWithFake)

default:
err = ErrUnsupportedExecutor
}

return agent, err
}

func Pacify(
advanced cfg.AdvancedConfig,
knownBy clif.KnownByCollection,
vfs storage.VirtualFS,
dummy bool,
) common.ExecutionAgent {
if dummy {
return &magickAgent{
baseAgent{
knownBy: knownBy,
program: &ProgramExecutor{
Name: advanced.Executable().Symbol(),
},
},
}
}

return &fakeAgent{
baseAgent: baseAgent{
knownBy: knownBy,
program: &ProgramExecutor{
Name: advanced.Executable().Symbol(),
},
},
vfs: vfs,
}
}
49 changes: 49 additions & 0 deletions src/app/proxy/ipc/execution-agent-fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ipc

import (
"fmt"
"os"

"github.com/snivilised/cobrass/src/clif"
"github.com/snivilised/extendio/xfs/storage"
"github.com/snivilised/pixa/src/cfg"
)

type fakeAgent struct {
baseAgent
vfs storage.VirtualFS
advanced cfg.AdvancedConfig
}

func (a *fakeAgent) IsInstalled() bool {
_, err := a.program.Look()

return err == nil
}

func (a *fakeAgent) Invoke(thirdPartyCL clif.ThirdPartyCommandLine, source, destination string) error {
var (
err error
fake *os.File
)

before := []string{source}

if a.vfs.FileExists(destination) {
return os.ErrExist
}

if fake, err = a.vfs.Create(destination); err != nil {
return err
}

// for this to work, the dry run decorator needs to be in place ...
//
fmt.Printf("---> 🚀 created fake destination at '%v'\n", destination)

defer fake.Close()

return a.program.Execute(
clif.Expand(before, thirdPartyCL, destination)...,
)
}
3 changes: 2 additions & 1 deletion src/app/proxy/ipc/internal-ipc-errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import "errors"
// internally and are of no significance to the user directly, which
// means they also don't need to be i18n error messages.

var ErrUsingDummyExecutor = errors.New("using dummy executor")
var ErrUseDummyExecutor = errors.New("using dummy executor")
var ErrUnsupportedExecutor = errors.New("unsupported executor")
Loading

0 comments on commit 8b6ef83

Please sign in to comment.