From 11f869fb37fb44d2cf59c9e245c6f3821c933993 Mon Sep 17 00:00:00 2001 From: Chris Goller Date: Thu, 18 Oct 2018 15:55:31 -0500 Subject: [PATCH] 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 --- cmd/influx/bucket.go | 2 +- cmd/influx/repl.go | 59 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/cmd/influx/bucket.go b/cmd/influx/bucket.go index 18d60fe546..b17386ee0c 100644 --- a/cmd/influx/bucket.go +++ b/cmd/influx/bucket.go @@ -52,7 +52,7 @@ func init() { func bucketCreateF(cmd *cobra.Command, args []string) { if bucketCreateFlags.org != "" && bucketCreateFlags.orgID != "" { fmt.Println("must specify exactly one of org or org-id") - cmd.Usage() + _ = cmd.Usage() os.Exit(1) } diff --git a/cmd/influx/repl.go b/cmd/influx/repl.go index 48121ce4e0..5aed1792ba 100644 --- a/cmd/influx/repl.go +++ b/cmd/influx/repl.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "os" @@ -22,23 +23,55 @@ var replCmd = &cobra.Command{ var replFlags struct { OrgID string + Org string } 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") if h := viper.GetString("ORG_ID"); 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) { - var orgID platform.ID - err := orgID.DecodeFromString(replFlags.OrgID) - if err != nil { - fmt.Fprintln(os.Stderr, err) + if replFlags.OrgID == "" && replFlags.Org == "" { + fmt.Fprintln(os.Stderr, "must specify exactly one of org or org-id") + _ = cmd.Usage() 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) if err != nil { fmt.Fprintln(os.Stderr, err) @@ -48,6 +81,22 @@ func replF(cmd *cobra.Command, args []string) { 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) { qs := &http.FluxQueryService{ Addr: addr,