feat(cmd/influx): add org name option to repl (#1121)

* feat(cmd/influx): add org name option to repl

* fix(cmd/influx): repl org-id and org are mutually exclusive
pull/10616/head
Chris Goller 2018-10-18 15:55:31 -05:00 committed by GitHub
parent 9d2119e48c
commit 11f869fb37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 6 deletions

View File

@ -52,7 +52,7 @@ func init() {
func bucketCreateF(cmd *cobra.Command, args []string) { func bucketCreateF(cmd *cobra.Command, args []string) {
if bucketCreateFlags.org != "" && bucketCreateFlags.orgID != "" { if bucketCreateFlags.org != "" && bucketCreateFlags.orgID != "" {
fmt.Println("must specify exactly one of org or org-id") fmt.Println("must specify exactly one of org or org-id")
cmd.Usage() _ = cmd.Usage()
os.Exit(1) os.Exit(1)
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"os" "os"
@ -22,23 +23,55 @@ var replCmd = &cobra.Command{
var replFlags struct { var replFlags struct {
OrgID string OrgID string
Org string
} }
func init() { func init() {
replCmd.PersistentFlags().StringVar(&replFlags.OrgID, "org-id", "", "Organization ID") replCmd.PersistentFlags().StringVar(&replFlags.OrgID, "org-id", "", "ID of organization to query")
viper.BindEnv("ORG_ID") viper.BindEnv("ORG_ID")
if h := viper.GetString("ORG_ID"); h != "" { if h := viper.GetString("ORG_ID"); h != "" {
replFlags.OrgID = h replFlags.OrgID = h
} }
replCmd.PersistentFlags().StringVarP(&replFlags.Org, "org", "o", "", "name of the organization")
viper.BindEnv("ORG")
if h := viper.GetString("ORG"); h != "" {
replFlags.Org = h
}
} }
func replF(cmd *cobra.Command, args []string) { func replF(cmd *cobra.Command, args []string) {
var orgID platform.ID if replFlags.OrgID == "" && replFlags.Org == "" {
err := orgID.DecodeFromString(replFlags.OrgID) fmt.Fprintln(os.Stderr, "must specify exactly one of org or org-id")
if err != nil { _ = cmd.Usage()
fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
if replFlags.OrgID != "" && replFlags.Org != "" {
fmt.Fprintln(os.Stderr, "must specify exactly one of org or org-id")
_ = cmd.Usage()
os.Exit(1)
}
var orgID platform.ID
if replFlags.OrgID != "" {
err := orgID.DecodeFromString(replFlags.OrgID)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid org id: %v\n", err)
os.Exit(1)
}
}
if replFlags.Org != "" {
ctx := context.Background()
var err error
orgID, err = findOrgID(ctx, replFlags.Org)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to find organization: %v\n", err)
os.Exit(1)
}
}
r, err := getFluxREPL(flags.host, flags.token, orgID) r, err := getFluxREPL(flags.host, flags.token, orgID)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
@ -48,6 +81,22 @@ func replF(cmd *cobra.Command, args []string) {
r.Run() r.Run()
} }
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
}
func getFluxREPL(addr, token string, orgID platform.ID) (*repl.REPL, error) { func getFluxREPL(addr, token string, orgID platform.ID) (*repl.REPL, error) {
qs := &http.FluxQueryService{ qs := &http.FluxQueryService{
Addr: addr, Addr: addr,