Skip to content

Commit

Permalink
fix(repl): replace extra boolean parameter for suggestions with repl …
Browse files Browse the repository at this point in the history
…options (#4915)
  • Loading branch information
jsternberg authored Jun 23, 2022
1 parent 473402e commit ad28223
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
8 changes: 7 additions & 1 deletion cmd/flux/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/influxdata/flux/execute/executetest"
"github.com/influxdata/flux/fluxinit"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/repl"
"github.com/opentracing/opentracing-go"
"github.com/spf13/cobra"
jaegercfg "github.com/uber/jaeger-client-go/config"
Expand Down Expand Up @@ -63,8 +64,13 @@ func runE(cmd *cobra.Command, args []string) error {
}
ctx = feature.Dependency{Flagger: flagger}.Inject(ctx)

var opts []repl.Option
if flags.EnableSuggestions {
opts = append(opts, repl.EnableSuggestions())
}

if len(args) == 0 {
return replE(ctx, flags.EnableSuggestions)
return replE(ctx, opts...)
}
return executeE(ctx, script, flags.Format)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/flux/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/influxdata/flux/repl"
)

func replE(ctx context.Context, enableSuggestions bool) error {
r := repl.New(ctx, enableSuggestions)
func replE(ctx context.Context, opts ...repl.Option) error {
r := repl.New(ctx, opts...)
r.Run()
return nil
}
4 changes: 2 additions & 2 deletions interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func TestInterpreter_MultiPhaseInterpretation(t *testing.T) {
ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
defer deps.Finish()

r := repl.New(ctx, false)
r := repl.New(ctx)
if _, err := r.Eval(prelude); err != nil {
t.Fatalf("unable to evaluate prelude: %s", err)
}
Expand Down Expand Up @@ -772,7 +772,7 @@ func TestInterpreter_MultipleEval(t *testing.T) {
ctx, deps := dependency.Inject(context.Background(), dependenciestest.Default())
defer deps.Finish()

r := repl.New(ctx, false)
r := repl.New(ctx)

if _, err := r.Eval(prelude); err != nil {
t.Fatalf("unable to evaluate prelude: %s", err)
Expand Down
35 changes: 27 additions & 8 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ type REPL struct {
enableSuggestions bool
}

func New(ctx context.Context, enableSuggestions bool) *REPL {
type Option interface {
applyOption(r *REPL)
}

func New(ctx context.Context, opts ...Option) *REPL {
scope := values.NewScope()
importer := runtime.StdLib()
for _, p := range runtime.PreludeList {
Expand All @@ -57,14 +61,17 @@ func New(ctx context.Context, enableSuggestions bool) *REPL {
panic(err)
}

return &REPL{
ctx: ctx,
scope: scope,
itrp: interpreter.NewInterpreter(nil, &lang.ExecOptsConfig{}),
analyzer: analyzer,
importer: importer,
enableSuggestions: enableSuggestions,
repl := &REPL{
ctx: ctx,
scope: scope,
itrp: interpreter.NewInterpreter(nil, &lang.ExecOptsConfig{}),
analyzer: analyzer,
importer: importer,
}
for _, opt := range opts {
opt.applyOption(repl)
}
return repl
}

func (r *REPL) Run() {
Expand Down Expand Up @@ -312,3 +319,15 @@ func LoadQuery(q string) (string, error) {

return q, nil
}

type option func(r *REPL)

func (o option) applyOption(r *REPL) {
o(r)
}

func EnableSuggestions() Option {
return option(func(r *REPL) {
r.enableSuggestions = true
})
}

0 comments on commit ad28223

Please sign in to comment.