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 b1a15bd
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 93 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
54 changes: 35 additions & 19 deletions src/app/proxy/enter-root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ type RootEntry struct {
files []string
}

func (e *RootEntry) LookAheadFn() nav.TraverseCallback {
return nil
}

func (e *RootEntry) PrincipalFn() nav.TraverseCallback {
return e.principalFn
}

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,24 +73,7 @@ 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
Expand Down Expand Up @@ -107,7 +117,13 @@ func (e *RootEntry) run() error {
}
}

return e.navigate(GetTraverseOptionsFunc(e), runnerWith, resumption, after)
return e.navigate(
GetTraverseOptionsFuncLegacy(e, "Root Entry Callback"),
runnerWith,
resumption,
after,
summariseAfter,
)
}

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

0 comments on commit b1a15bd

Please sign in to comment.