Skip to content

Commit

Permalink
feat(proxy): skip agent invoke if sample file already exists (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Feb 26, 2024
1 parent 14f2625 commit 743ea71
Show file tree
Hide file tree
Showing 15 changed files with 327 additions and 156 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"repotoken",
"samber",
"sidewalk",
"SMPL",
"snivilised",
"staticcheck",
"structcheck",
Expand Down
15 changes: 8 additions & 7 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ func validatePositionalArgs(cmd *cobra.Command, args []string) error {
// without resorting to the use of Go's init() mechanism and minimal
// use of package global variables.
type Bootstrap struct {
Container *assistant.CobraContainer
OptionsInfo ConfigureOptionsInfo
Configs *common.Configs
Vfs storage.VirtualFS
Logger *slog.Logger
Presentation common.PresentationOptions
Observers common.Observers
Container *assistant.CobraContainer
OptionsInfo ConfigureOptionsInfo
Configs *common.Configs
Vfs storage.VirtualFS
Logger *slog.Logger
Presentation common.PresentationOptions
Observers common.Observers
Notifications common.LifecycleNotifications
}

type ConfigureOptionsInfo struct {
Expand Down
9 changes: 5 additions & 4 deletions src/app/command/shrink-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob

_, appErr = proxy.EnterShrink(
&proxy.ShrinkParams{
Inputs: inputs,
Viper: b.OptionsInfo.Config.Viper,
Logger: b.Logger,
Vfs: b.Vfs,
Inputs: inputs,
Viper: b.OptionsInfo.Config.Viper,
Logger: b.Logger,
Vfs: b.Vfs,
Notifications: &b.Notifications,
},
)
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/app/proxy/common/filing-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type (
PathFinder interface {
Transfer(info *PathInfo) (folder, file string)
Result(info *PathInfo) (folder, file string)
FolderSupplement(profile string) string
FileSupplement(profile, withSampling string) string
SampleFileSupplement(withSampling string) string
TransparentInput() bool
JournalFullPath(item *nav.TraverseItem) string
Statics() *StaticInfo
Expand All @@ -39,6 +42,8 @@ type (

FileManager interface {
Finder() PathFinder
FileExists(pathAt string) bool
DirectoryExists(pathAt string) bool
Create(path string, overwrite bool) error
Setup(pi *PathInfo) (destination string, err error)
Tidy(pi *PathInfo) error
Expand Down
15 changes: 15 additions & 0 deletions src/app/proxy/common/notifications-defs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package common

type CallbackOnBegin interface {
Notify(finder PathFinder, scheme, profile string)
}

type CallbackOnBeginFunc func(finder PathFinder, scheme, profile string)

func (f CallbackOnBeginFunc) Notify(finder PathFinder, scheme, profile string) {
f(finder, scheme, profile)
}

type LifecycleNotifications struct {
OnBegin CallbackOnBeginFunc
}
36 changes: 36 additions & 0 deletions src/app/proxy/common/static-info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,42 @@ type StaticInfo struct {
Sample string
}

func NewStaticInfoFromConfig(advanced AdvancedConfig) *StaticInfo {
stats := &StaticInfo{
Adhoc: advanced.AdhocLabel(),
Legacy: advanced.LegacyLabel(),
Trash: advanced.TrashLabel(),
Fake: advanced.FakeLabel(),
Supplement: advanced.SupplementLabel(),
Sample: advanced.SampleLabel(),
}

stats.initJournal(advanced.JournalLabel())

return stats
}

func (i *StaticInfo) initJournal(journalLabel string) {
if !strings.HasSuffix(journalLabel, Definitions.Filing.JournalExt) {
journalLabel += Definitions.Filing.JournalExt
}

if !strings.HasPrefix(journalLabel, Definitions.Filing.Discriminator) {
journalLabel = Definitions.Filing.Discriminator + journalLabel
}

withoutExt := strings.TrimSuffix(journalLabel, Definitions.Filing.JournalExt)
core := strings.TrimPrefix(withoutExt, Definitions.Filing.Discriminator)

i.Journal = JournalMetaInfo{
Core: core,
Actual: journalLabel,
WithoutExt: withoutExt,
Extension: Definitions.Filing.JournalExt,
Discriminator: Definitions.Filing.Discriminator,
}
}

func (i *StaticInfo) JournalLocation(name, parent string) string {
file := name + i.Journal.Actual
journalFile := filepath.Join(parent, file)
Expand Down
18 changes: 14 additions & 4 deletions src/app/proxy/enter-shrink.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func (e *ShrinkEntry) PrincipalOptionsFn(o *nav.TraverseOptions) {

o.Notify.OnBegin = func(_ *nav.NavigationState) {
e.Log.Info("===> 🛡️ beginning traversal ...")

if e.Notifications.OnBegin != nil {
manager := e.FileManager
e.Notifications.OnBegin(manager.Finder(),
e.Inputs.Root.ProfileFam.Native.Scheme,
e.Inputs.Root.ProfileFam.Native.Profile,
)
}
}

o.Callback = e.EntryBase.Interaction.Decorate(&nav.LabelledTraverseCallback{
Expand Down Expand Up @@ -155,10 +163,11 @@ func (e *ShrinkEntry) run() (result *nav.TraverseResult, err error) {
}

type ShrinkParams struct {
Inputs *common.ShrinkCommandInputs
Viper configuration.ViperConfig
Logger *slog.Logger
Vfs storage.VirtualFS
Inputs *common.ShrinkCommandInputs
Viper configuration.ViperConfig
Logger *slog.Logger
Vfs storage.VirtualFS
Notifications *common.LifecycleNotifications
}

func EnterShrink(
Expand Down Expand Up @@ -242,6 +251,7 @@ func EnterShrink(
},
params.Inputs.Root.Configs,
),
Notifications: params.Notifications,
},
Inputs: params.Inputs,
}
Expand Down
28 changes: 13 additions & 15 deletions src/app/proxy/entry-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ type EntryBase struct {
// navigation such as Options)
// with the rest going into cobrass.clif
//
Inputs *common.RootCommandInputs
Agent common.ExecutionAgent
Interaction common.UserInteraction
Viper configuration.ViperConfig
Options *nav.TraverseOptions
Registry *orc.ControllerRegistry
Log *slog.Logger
Vfs storage.VirtualFS
FileManager common.FileManager
FilterSetup *filterSetup
Inputs *common.RootCommandInputs
Agent common.ExecutionAgent
Interaction common.UserInteraction
Viper configuration.ViperConfig
Options *nav.TraverseOptions
Registry *orc.ControllerRegistry
Log *slog.Logger
Vfs storage.VirtualFS
FileManager common.FileManager
FilterSetup *filterSetup
Notifications *common.LifecycleNotifications
}

func (e *EntryBase) ConfigureOptions(o *nav.TraverseOptions) {
Expand All @@ -51,21 +52,18 @@ func (e *EntryBase) ConfigureOptions(o *nav.TraverseOptions) {
statics := e.FileManager.Finder().Statics()
jWithoutExt := statics.Journal.WithoutExt
trash := statics.TrashTag()
sample := fmt.Sprintf("$%v$", statics.Sample) // PathFinder.FileSupplement

return lo.Filter(contents, func(item fs.DirEntry, index int) bool {
name := item.Name()

return !strings.HasPrefix(name, ".") &&
!strings.Contains(name, jWithoutExt) &&
!strings.Contains(name, trash) &&
!strings.Contains(name, statics.Sample)
!strings.Contains(name, sample)
}), nil
}

o.Hooks.Extend = func(navi *nav.NavigationInfo, entries *nav.DirectoryContents) {
nav.DefaultExtendHookFn(navi, entries)
}

if o.Store.FilterDefs == nil {
switch {
case e.Inputs.FoldersFam.Native.FoldersGlob != "":
Expand Down
14 changes: 11 additions & 3 deletions src/app/proxy/filing/file-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func (fm *FileManager) Finder() common.PathFinder {
return fm.finder
}

func (fm *FileManager) FileExists(pathAt string) bool {
return fm.Vfs.FileExists(pathAt)
}

func (fm *FileManager) DirectoryExists(pathAt string) bool {
return fm.Vfs.DirectoryExists(pathAt)
}

func (fm *FileManager) Create(path string, overwrite bool) error {
if fm.Vfs.FileExists(path) && !overwrite {
return errors.Wrapf(os.ErrExist, "could not create file at path: '%v'", path)
Expand All @@ -57,12 +65,12 @@ func (fm *FileManager) Create(path string, overwrite bool) error {
func (fm *FileManager) Setup(pi *common.PathInfo) (destination string, err error) {
if !fm.finder.TransparentInput() {
// Any result file must not clash with the input file, so the input
// file must stay in place
// file must stay in place.
// todo: if --trash is specified, then the input must be moved there
//
return pi.Item.Path, nil
}

// 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...
Expand Down
69 changes: 28 additions & 41 deletions src/app/proxy/filing/path-finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@ func NewFinder(
advanced := info.Advanced
extensions := advanced.Extensions()
finder := &PathFinder{
Sch: info.Scheme,
Stats: &common.StaticInfo{
Adhoc: advanced.AdhocLabel(),
Legacy: advanced.LegacyLabel(),
Trash: advanced.TrashLabel(),
Fake: advanced.FakeLabel(),
Supplement: advanced.SupplementLabel(),
Sample: advanced.SampleLabel(),
},
Sch: info.Scheme,
Stats: common.NewStaticInfoFromConfig(advanced),
Ext: &ExtensionTransformation{
Transformers: strings.Split(extensions.Transforms(), ","),
Remap: extensions.Map(),
Expand Down Expand Up @@ -182,27 +175,6 @@ func (f *PathFinder) init(info *NewFinderInfo) {
// with the --output flag, then the input is no longer transparent, as the user has
// to go to the output location to see the result.
f.transparentInput = info.OutputPath == "" && info.Arity == 1

journal := info.Advanced.JournalLabel()

if !strings.HasSuffix(journal, common.Definitions.Filing.JournalExt) {
journal += common.Definitions.Filing.JournalExt
}

if !strings.HasPrefix(journal, common.Definitions.Filing.Discriminator) {
journal = common.Definitions.Filing.Discriminator + journal
}

withoutExt := strings.TrimSuffix(journal, common.Definitions.Filing.JournalExt)
core := strings.TrimPrefix(withoutExt, common.Definitions.Filing.Discriminator)

f.Stats.Journal = common.JournalMetaInfo{
Core: core,
Actual: journal,
WithoutExt: withoutExt,
Extension: common.Definitions.Filing.JournalExt,
Discriminator: common.Definitions.Filing.Discriminator,
}
}

func (f *PathFinder) JournalFullPath(item *nav.TraverseItem) string {
Expand Down Expand Up @@ -255,7 +227,7 @@ func (f *PathFinder) Transfer(info *common.PathInfo) (folder, file string) {
"${{TRANSFER-DESTINATION}}": to,
"${{ITEM-SUB-PATH}}": info.Item.Extension.SubPath,
"${{DEJA-VU}}": f.Stats.TrashTag(),
"${{SUPPLEMENT}}": f.folderProfileSupplement(info.Profile),
"${{SUPPLEMENT}}": f.FolderSupplement(info.Profile),
}, segments...)
}()

Expand All @@ -270,7 +242,7 @@ func (f *PathFinder) Transfer(info *common.PathInfo) (folder, file string) {

if info.IsCuddling {
supp := fmt.Sprintf("%v.%v", f.Stats.TrashTag(),
f.fileSupplement(info.Profile, ""),
f.FileSupplement(info.Profile, ""),
)

return SupplementFilename(
Expand Down Expand Up @@ -352,7 +324,7 @@ func (f *PathFinder) Result(info *common.PathInfo) (folder, file string) {
return pfTemplates.evaluate(pfFieldValues{
"${{OUTPUT-ROOT}}": to,
"${{ITEM-SUB-PATH}}": info.Item.Extension.SubPath,
"${{SUPPLEMENT}}": f.folderProfileSupplement(info.Profile),
"${{SUPPLEMENT}}": f.FolderSupplement(info.Profile),
}, segments...)
},
)
Expand All @@ -370,7 +342,14 @@ func (f *PathFinder) Result(info *common.PathInfo) (folder, file string) {
withSampling = f.Stats.Sample
}

supp := f.fileSupplement(info.Profile, withSampling)
supp := lo.TernaryF(info.IsSampling,
func() string {
return f.SampleFileSupplement(withSampling)
},
func() string {
return f.FileSupplement(info.Profile, withSampling)
},
)

return SupplementFilename(
info.Item.Extension.Name, supp, f.Stats,
Expand All @@ -383,7 +362,7 @@ func (f *PathFinder) Result(info *common.PathInfo) (folder, file string) {
return folder, f.mutateExtension(file)
}

func (f *PathFinder) folderProfileSupplement(profile string) string {
func (f *PathFinder) FolderSupplement(profile string) string {
return lo.TernaryF(f.Sch == "" && profile == "",
func() string {
adhocLabel := f.Stats.Adhoc
Expand All @@ -395,23 +374,23 @@ func (f *PathFinder) folderProfileSupplement(profile string) string {
)
}

func (f *PathFinder) fileSupplement(profile, withSampling string) string {
func FileSupplement(scheme, profile, adhoc, withSampling string) string {
var (
result string
)

switch {
case f.Sch != "" && profile != "":
result = fmt.Sprintf("%v.%v", f.Sch, profile)
case scheme != "" && profile != "":
result = fmt.Sprintf("%v.%v", scheme, profile)

case f.Sch != "":
result = f.Sch
case scheme != "":
result = scheme

case profile != "":
result = profile

default:
result = f.Stats.Adhoc
result = adhoc
}

if withSampling != "" {
Expand All @@ -421,6 +400,14 @@ func (f *PathFinder) fileSupplement(profile, withSampling string) string {
return result
}

func (f *PathFinder) FileSupplement(profile, withSampling string) string {
return FileSupplement(f.Sch, profile, f.Stats.Adhoc, withSampling) // todo: is adhoc ok here?
}

func (f *PathFinder) SampleFileSupplement(withSampling string) string {
return fmt.Sprintf("$%v$", withSampling)
}

func (f *PathFinder) TransparentInput() bool {
return f.transparentInput
}
Expand Down
Loading

0 comments on commit 743ea71

Please sign in to comment.