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"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var replCmd = &cobra.Command{
|
|
|
|
Use: "repl",
|
|
|
|
Short: "Interactive REPL (read-eval-print-loop)",
|
|
|
|
Args: cobra.NoArgs,
|
2019-01-22 18:19:26 +00:00
|
|
|
RunE: wrapCheckSetup(replF),
|
2018-05-24 17:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var replFlags struct {
|
2018-09-12 21:10:09 +00:00
|
|
|
OrgID string
|
2018-10-18 20:55:31 +00:00
|
|
|
Org string
|
2018-05-24 17:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2019-01-14 18:54:53 +00:00
|
|
|
replCmd.PersistentFlags().StringVar(&replFlags.OrgID, "org-id", "", "The ID of organization to query")
|
2018-05-24 17:46:40 +00:00
|
|
|
viper.BindEnv("ORG_ID")
|
|
|
|
if h := viper.GetString("ORG_ID"); h != "" {
|
|
|
|
replFlags.OrgID = h
|
|
|
|
}
|
2018-10-18 20:55:31 +00:00
|
|
|
|
2019-01-14 18:54:53 +00:00
|
|
|
replCmd.PersistentFlags().StringVarP(&replFlags.Org, "org", "o", "", "The name of the organization")
|
2018-10-18 20:55:31 +00:00
|
|
|
viper.BindEnv("ORG")
|
|
|
|
if h := viper.GetString("ORG"); h != "" {
|
|
|
|
replFlags.Org = h
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-10-18 20:55:31 +00:00
|
|
|
if replFlags.OrgID == "" && replFlags.Org == "" {
|
2019-01-22 18:19:26 +00:00
|
|
|
return fmt.Errorf("must specify exactly one of org or org-id")
|
2018-05-24 17:46:40 +00:00
|
|
|
}
|
2018-10-18 20:55:31 +00:00
|
|
|
|
|
|
|
if replFlags.OrgID != "" && replFlags.Org != "" {
|
2019-01-22 18:19:26 +00:00
|
|
|
return fmt.Errorf("must specify exactly one of org or org-id")
|
2018-10-18 20:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var orgID platform.ID
|
|
|
|
if replFlags.OrgID != "" {
|
|
|
|
err := orgID.DecodeFromString(replFlags.OrgID)
|
|
|
|
if err != nil {
|
2019-01-22 18:19:26 +00:00
|
|
|
return fmt.Errorf("invalid org id: %v", err)
|
2018-10-18 20:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if replFlags.Org != "" {
|
|
|
|
ctx := context.Background()
|
|
|
|
var err error
|
|
|
|
orgID, err = findOrgID(ctx, replFlags.Org)
|
|
|
|
if err != nil {
|
2019-01-22 18:19:26 +00:00
|
|
|
return fmt.Errorf("unable to find organization: %v", err)
|
2018-10-18 20:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 01:13:13 +00:00
|
|
|
flux.FinalizeBuiltIns()
|
|
|
|
|
2018-09-12 21:10:09 +00:00
|
|
|
r, err := getFluxREPL(flags.host, flags.token, 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
|
|
|
|
2018-10-18 20:55:31 +00:00
|
|
|
func findOrgID(ctx context.Context, org string) (platform.ID, error) {
|
|
|
|
svc := &http.OrganizationService{
|
|
|
|
Addr: flags.host,
|
|
|
|
Token: flags.token,
|
|
|
|
}
|
|
|
|
|
|
|
|
o, err := svc.FindOrganization(ctx, platform.OrganizationFilter{
|
|
|
|
Name: &org,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return platform.InvalidID(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.ID, nil
|
|
|
|
}
|
|
|
|
|
2018-09-12 21:10:09 +00:00
|
|
|
func getFluxREPL(addr, token string, orgID platform.ID) (*repl.REPL, error) {
|
|
|
|
qs := &http.FluxQueryService{
|
2018-10-04 19:11:45 +00:00
|
|
|
Addr: addr,
|
2018-09-12 21:10:09 +00:00
|
|
|
Token: token,
|
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
|
|
|
}
|
2019-08-23 18:26:29 +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
|
|
|
}
|