2018-05-24 17:46:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-10-18 20:55:31 +00:00
|
|
|
"context"
|
2018-05-24 17:46:40 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-09-19 15:01:17 +00:00
|
|
|
"github.com/influxdata/flux"
|
2018-09-06 18:09:52 +00:00
|
|
|
"github.com/influxdata/flux/repl"
|
2019-11-06 01:13:13 +00:00
|
|
|
_ "github.com/influxdata/flux/stdlib"
|
2019-01-08 00:37:16 +00:00
|
|
|
platform "github.com/influxdata/influxdb"
|
|
|
|
"github.com/influxdata/influxdb/http"
|
|
|
|
"github.com/influxdata/influxdb/query"
|
2019-11-06 01:13:13 +00:00
|
|
|
_ "github.com/influxdata/influxdb/query/stdlib"
|
2018-05-24 17:46:40 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var replFlags struct {
|
2020-01-10 00:34:30 +00:00
|
|
|
org organization
|
2018-05-24 17:46:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 00:34:30 +00:00
|
|
|
func cmdREPL() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "repl",
|
|
|
|
Short: "Interactive Flux REPL (read-eval-print-loop)",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: wrapCheckSetup(replF),
|
2018-05-24 17:46:40 +00:00
|
|
|
}
|
2020-01-10 00:34:30 +00:00
|
|
|
replFlags.org.register(cmd, false)
|
2018-10-18 20:55:31 +00:00
|
|
|
|
2020-01-10 00:34:30 +00:00
|
|
|
return cmd
|
2018-05-24 17:46:40 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 18:19:26 +00:00
|
|
|
func replF(cmd *cobra.Command, args []string) error {
|
2018-10-25 19:28:33 +00:00
|
|
|
if flags.local {
|
2019-01-22 18:19:26 +00:00
|
|
|
return fmt.Errorf("local flag not supported for repl command")
|
2018-10-25 19:28:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 00:34:30 +00:00
|
|
|
if err := replFlags.org.validOrgFlags(); err != nil {
|
|
|
|
return err
|
2018-10-18 20:55:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 00:34:30 +00:00
|
|
|
orgSVC, err := newOrganizationService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-18 20:55:31 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 00:34:30 +00:00
|
|
|
orgID, err := replFlags.org.getID(orgSVC)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-18 20:55:31 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 01:13:13 +00:00
|
|
|
flux.FinalizeBuiltIns()
|
|
|
|
|
2019-11-14 23:28:43 +00:00
|
|
|
r, err := getFluxREPL(flags.host, flags.token, flags.skipVerify, orgID)
|
2018-05-24 17:46:40 +00:00
|
|
|
if err != nil {
|
2019-01-22 18:19:26 +00:00
|
|
|
return err
|
2018-05-24 17:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r.Run()
|
2019-01-22 18:19:26 +00:00
|
|
|
return nil
|
2018-05-24 17:46:40 +00:00
|
|
|
}
|
2018-05-31 17:47:33 +00:00
|
|
|
|
2019-11-14 23:28:43 +00:00
|
|
|
func getFluxREPL(addr, token string, skipVerify bool, orgID platform.ID) (*repl.REPL, error) {
|
2018-09-12 21:10:09 +00:00
|
|
|
qs := &http.FluxQueryService{
|
2019-11-14 23:28:43 +00:00
|
|
|
Addr: addr,
|
|
|
|
Token: token,
|
|
|
|
InsecureSkipVerify: skipVerify,
|
2018-05-31 17:47:33 +00:00
|
|
|
}
|
2018-09-12 21:10:09 +00:00
|
|
|
q := &query.REPLQuerier{
|
|
|
|
OrganizationID: orgID,
|
|
|
|
QueryService: qs,
|
2018-05-31 17:47:33 +00:00
|
|
|
}
|
2020-01-10 00:34:30 +00:00
|
|
|
// background context is OK here, and DefaultDependencies are noop deps. Also safe
|
|
|
|
// since we send all queries to the server side.
|
2019-09-19 15:01:17 +00:00
|
|
|
return repl.New(context.Background(), flux.NewDefaultDependencies(), q), nil
|
2018-05-31 17:47:33 +00:00
|
|
|
}
|