Skip to content

Commit

Permalink
feat(proxy): implement look-ahead (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Oct 26, 2023
1 parent 3f34c6f commit 646e84e
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 125 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.28.1
github.com/pkg/errors v0.9.1
github.com/samber/lo v1.38.1
github.com/snivilised/extendio v0.3.0
github.com/snivilised/lorax v0.4.1
Expand All @@ -22,7 +23,6 @@ require (
github.com/google/uuid v1.3.1 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
26 changes: 13 additions & 13 deletions src/app/command/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,25 @@ type ConfigInfo struct {
// without resorting to the use of Go's init() mechanism and minimal
// use of package global variables.
type Bootstrap struct {
Container *assistant.CobraContainer
options ConfigureOptions
Container *assistant.CobraContainer
optionsInfo ConfigureOptionsInfo
}

type ConfigureOptions struct {
type ConfigureOptionsInfo struct {
Detector LocaleDetector
Executor proxy.Executor
Config ConfigInfo
}

type ConfigureOptionFn func(*ConfigureOptions)
type ConfigureOptionFn func(*ConfigureOptionsInfo)

// Root builds the command tree and returns the root command, ready
// to be executed.
func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

b.options = ConfigureOptions{
b.optionsInfo = ConfigureOptionsInfo{
Detector: &Jabber{},
Executor: &ProgramExecutor{
Name: "magick",
Expand All @@ -89,21 +89,21 @@ func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command {
},
}

if _, err := b.options.Executor.Look(); err != nil {
b.options.Executor = &DummyExecutor{
Name: b.options.Executor.ProgName(),
if _, err := b.optionsInfo.Executor.Look(); err != nil {
b.optionsInfo.Executor = &DummyExecutor{
Name: b.optionsInfo.Executor.ProgName(),
}
}

for _, fo := range options {
fo(&b.options)
fo(&b.optionsInfo)
}

b.configure()

// JUST TEMPORARY: make the executor the dummy for safety
//
b.options.Executor = &DummyExecutor{
b.optionsInfo.Executor = &DummyExecutor{
Name: "magick",
}

Expand All @@ -126,7 +126,7 @@ func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command {

// ---> execute root core
//
return proxy.EnterRoot(inputs, b.options.Executor, b.options.Config.Viper)
return proxy.EnterRoot(inputs, b.optionsInfo.Executor, b.optionsInfo.Config.Viper)
},
},
)
Expand All @@ -139,8 +139,8 @@ func (b *Bootstrap) Root(options ...ConfigureOptionFn) *cobra.Command {
}

func (b *Bootstrap) configure() {
vc := b.options.Config.Viper
ci := b.options.Config
vc := b.optionsInfo.Config.Viper
ci := b.optionsInfo.Config

vc.SetConfigName(ci.Name)
vc.SetConfigType(ci.ConfigType)
Expand Down
2 changes: 1 addition & 1 deletion src/app/command/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var _ = Describe("Bootstrap", Ordered, func() {
Context("given: root defined with magick sub-command", func() {
It("🧪 should: setup command without error", func() {
bootstrap := command.Bootstrap{}
rootCmd := bootstrap.Root(func(co *command.ConfigureOptions) {
rootCmd := bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &DetectorStub{}
co.Executor = &ExecutorStub{
Name: "magick",
Expand Down
2 changes: 1 addition & 1 deletion src/app/command/magick-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var _ = Describe("MagickCmd", Ordered, func() {
bootstrap := command.Bootstrap{}
tester := helpers.CommandTester{
Args: []string{"mag"},
Root: bootstrap.Root(func(co *command.ConfigureOptions) {
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &DetectorStub{}
}),
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/command/root-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var _ = Describe("RootCmd", Ordered, func() {
bootstrap := command.Bootstrap{}
tester = helpers.CommandTester{
Args: []string{"./"},
Root: bootstrap.Root(func(co *command.ConfigureOptions) {
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &DetectorStub{}
co.Executor = &ExecutorStub{
Name: "magick",
Expand All @@ -49,7 +49,7 @@ var _ = Describe("RootCmd", Ordered, func() {
bootstrap := command.Bootstrap{}
tester = helpers.CommandTester{
Args: entry.commandLine,
Root: bootstrap.Root(func(co *command.ConfigureOptions) {
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &DetectorStub{}
co.Executor = &ExecutorStub{
Name: "magick",
Expand Down
4 changes: 2 additions & 2 deletions src/app/command/shrink-cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func (b *Bootstrap) buildShrinkCommand(container *assistant.CobraContainer) *cob

appErr = proxy.EnterShrink(
inputs,
b.options.Executor,
b.options.Config.Viper,
b.optionsInfo.Executor,
b.optionsInfo.Config.Viper,
)
} else {
return xvErr
Expand Down
2 changes: 1 addition & 1 deletion src/app/command/shrink-cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func expectValidShrinkCmdInvocation(entry *shrinkTE) {

tester := helpers.CommandTester{
Args: append(options, entry.args...),
Root: bootstrap.Root(func(co *command.ConfigureOptions) {
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &DetectorStub{}
co.Executor = &ExecutorStub{
Name: "magick",
Expand Down
2 changes: 1 addition & 1 deletion src/app/proxy/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func expectValidShrinkCmdInvocation(entry *configTE) {

tester := helpers.CommandTester{
Args: append(options, entry.args...),
Root: bootstrap.Root(func(co *command.ConfigureOptions) {
Root: bootstrap.Root(func(co *command.ConfigureOptionsInfo) {
co.Detector = &helpers.DetectorStub{}
co.Executor = &helpers.ExecutorStub{
Name: prog,
Expand Down
84 changes: 37 additions & 47 deletions src/app/proxy/enter-root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ type RootEntry struct {
files []string
}

func (e *RootEntry) principalFn(item *nav.TraverseItem) error {
depth := item.Extension.Depth
indicator := lo.Ternary(len(item.Children) > 0, "☀️", "🌊")

for _, entry := range item.Children {
fullPath := filepath.Join(item.Path, entry.Name())
e.files = append(e.files, fullPath)
}

fmt.Printf(
"---> %v ROOT-CALLBACK: (depth:%v, files:%v) '%v'\n",
indicator,
depth, len(item.Children),
item.Path,
)

return e.Program.Execute("--version")
}

func (e *RootEntry) ConfigureOptions(o *nav.TraverseOptions) {
o.Notify.OnBegin = func(_ *nav.NavigationState) {
fmt.Printf("===> 🛡️ beginning traversal ...\n")
Expand All @@ -46,68 +65,39 @@ func (e *RootEntry) ConfigureOptions(o *nav.TraverseOptions) {
}
o.Callback = &nav.LabelledTraverseCallback{
Label: "Root Entry Callback",
Fn: func(item *nav.TraverseItem) error {
depth := item.Extension.Depth
indicator := lo.Ternary(len(item.Children) > 0, "☀️", "🌊")

for _, entry := range item.Children {
fullPath := filepath.Join(item.Path, entry.Name())
e.files = append(e.files, fullPath)
}

fmt.Printf(
"---> %v ROOT-CALLBACK: (depth:%v, files:%v) '%v'\n",
indicator,
depth, len(item.Children),
item.Path,
)

return e.Program.Execute("--version")
},
Fn: e.principalFn,
}
o.Store.Subscription = nav.SubscribeFoldersWithFiles
o.Store.DoExtend = true
e.EntryBase.ConfigureOptions(o)
}

func (e *RootEntry) run() error {
files := []string{}
runnerWith := composeWith(e.Inputs.ParamSet)
resumption := &nav.Resumption{
RestorePath: "/json-path-to-come-from-a-flag-option/restore.json",
Restorer: func(o *nav.TraverseOptions, active *nav.ActiveState) {
o.Callback = &nav.LabelledTraverseCallback{
Label: "Root Entry Callback",
Fn: func(item *nav.TraverseItem) error {
depth := item.Extension.Depth
indicator := lo.Ternary(len(item.Children) > 0, "☀️", "🌊")

for _, entry := range item.Children {
fullPath := filepath.Join(item.Path, entry.Name())
files = append(files, fullPath)
}

fmt.Printf(
"---> %v ROOT-CALLBACK: (depth:%v, files:%v) '%v'\n",
indicator,
depth, len(item.Children),
item.Path,
)

return nil
},
}
},
Strategy: nav.ResumeStrategySpawnEn, // to come from an arg
}

var nilResumption *nav.Resumption // root does not need to support resume

after := func(result *nav.TraverseResult, err error) {
for _, file := range e.files {
fmt.Printf(" ===> 🔆 candidate file: '%v'\n", file)
}
}

return e.navigate(GetTraverseOptionsFunc(e), runnerWith, resumption, after)
principal := func(o *nav.TraverseOptions) {
e.ConfigureOptions(o)
o.Callback = &nav.LabelledTraverseCallback{
Label: "Root Entry Callback",
Fn: e.principalFn,
}
}

return e.navigate(
principal,
runnerWith,
nilResumption,
after,
summariseAfter,
)
}

func composeWith(rps *assistant.ParamSet[RootParameterSet]) nav.CreateNewRunnerWith {
Expand Down
Loading

0 comments on commit 646e84e

Please sign in to comment.