2018-05-24 17:46:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2018-09-06 18:09:52 +00:00
|
|
|
"github.com/influxdata/flux/repl"
|
2018-05-31 17:47:33 +00:00
|
|
|
"github.com/influxdata/platform"
|
2018-09-12 21:10:09 +00:00
|
|
|
"github.com/influxdata/platform/http"
|
|
|
|
"github.com/influxdata/platform/query"
|
2018-05-31 17:47:33 +00:00
|
|
|
_ "github.com/influxdata/platform/query/builtin"
|
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,
|
|
|
|
Run: replF,
|
|
|
|
}
|
|
|
|
|
|
|
|
var replFlags struct {
|
2018-09-12 21:10:09 +00:00
|
|
|
OrgID string
|
2018-05-24 17:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
replCmd.PersistentFlags().StringVar(&replFlags.OrgID, "org-id", "", "Organization ID")
|
|
|
|
viper.BindEnv("ORG_ID")
|
|
|
|
if h := viper.GetString("ORG_ID"); h != "" {
|
|
|
|
replFlags.OrgID = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func replF(cmd *cobra.Command, args []string) {
|
2018-09-12 21:10:09 +00:00
|
|
|
var orgID platform.ID
|
|
|
|
err := orgID.DecodeFromString(replFlags.OrgID)
|
2018-05-24 17:46:40 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
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 {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Run()
|
|
|
|
}
|
2018-05-31 17:47:33 +00:00
|
|
|
|
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
|
|
|
}
|
2018-09-12 21:10:09 +00:00
|
|
|
return repl.New(q), nil
|
2018-05-31 17:47:33 +00:00
|
|
|
}
|