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 28, 2024
1 parent 14f2625 commit c8f3ded
Show file tree
Hide file tree
Showing 17 changed files with 428 additions and 197 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
Loading

0 comments on commit c8f3ded

Please sign in to comment.