From b53a9fdbd11fac4c6cc78c7da704b4551be7c8cc Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Mon, 3 Jun 2024 17:01:45 -0400 Subject: [PATCH] Replace URL arg with --url flag or KORREL8R_URL environment. --- cmd/korrel8rcli/main.go | 33 ++++++++++++++++++++++++++------- cmd/korrel8rcli/main_test.go | 2 +- cmd/korrel8rcli/operations.go | 11 ++++------- cmd/korrel8rcli/web.go | 6 +++--- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/cmd/korrel8rcli/main.go b/cmd/korrel8rcli/main.go index 21e4ec9..001d925 100644 --- a/cmd/korrel8rcli/main.go +++ b/cmd/korrel8rcli/main.go @@ -3,6 +3,8 @@ package main import ( + "errors" + "fmt" "log" "net/url" "path/filepath" @@ -23,7 +25,8 @@ var ( } // Global Flags - output = EnumFlag("yaml", "json-pretty", "json") + output = EnumFlag("yaml", "json-pretty", "json") + korrel8rURL = rootCmd.PersistentFlags().StringP("url", "u", "", "URL of remote korrel8r service (you can also set the KORREL8R_URL environment variable)") ) func main() { @@ -33,17 +36,33 @@ func main() { check(rootCmd.Execute()) } -func check(err error) { - if err != nil { - log.Fatalln(err) - } +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print version.", + Run: func(cmd *cobra.Command, args []string) { fmt.Println(rootCmd.Version) }, } -func newClient(urlStr string) *client.RESTAPI { - u, err := url.Parse(urlStr) +func init() { + rootCmd.AddCommand(versionCmd) +} + +func newClient() *client.RESTAPI { + if *korrel8rURL == "" { + *korrel8rURL = os.Getenv("KORREL8R_URL") + } + if *korrel8rURL == "" { + check(errors.New("Either command line flag --url or environment variable KORREL8R_URL must be set. ")) + } + u, err := url.Parse(*korrel8rURL) check(err) if u.Path == "" { u.Path = client.DefaultBasePath } return client.New(httptransport.New(u.Host, u.Path, []string{u.Scheme}), nil) } + +func check(err error) { + if err != nil { + log.Fatalln(err) + } +} diff --git a/cmd/korrel8rcli/main_test.go b/cmd/korrel8rcli/main_test.go index 019072e..e6de3a4 100644 --- a/cmd/korrel8rcli/main_test.go +++ b/cmd/korrel8rcli/main_test.go @@ -24,7 +24,7 @@ func Test_domains(t *testing.T) { ) // Wait for server to start require.Eventually(t, func() bool { - out, err = korrel8rcli(t, "domains", u.String()) + out, err = korrel8rcli(t, "domains", "-u", u.String()) if err != nil { t.Log("retry: ", err) } diff --git a/cmd/korrel8rcli/operations.go b/cmd/korrel8rcli/operations.go index f990e40..39305eb 100644 --- a/cmd/korrel8rcli/operations.go +++ b/cmd/korrel8rcli/operations.go @@ -30,11 +30,10 @@ func makeStart() *models.Start { } var domainsCmd = &cobra.Command{ - Use: "domains URL", + Use: "domains", Short: "Get a list of domains and store configuration", - Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - c := newClient(args[0]) + c := newClient() ok, err := c.Operations.GetDomains(&operations.GetDomainsParams{}) check(err) NewPrinter(output.String(), os.Stdout)(ok.Payload) @@ -48,9 +47,8 @@ func init() { var neighboursCmd = &cobra.Command{ Use: "neighbours URL [FLAGS]", Short: "Get graph of nearest neighbours", - Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - c := newClient(args[0]) + c := newClient() ok, err := c.Operations.PostGraphsNeighbours(&operations.PostGraphsNeighboursParams{ Request: &models.Neighbours{ Depth: depth, @@ -66,9 +64,8 @@ var neighboursCmd = &cobra.Command{ var goalsCmd = &cobra.Command{ Use: "goals URL [FLAGS]", Short: "Get graph of nearest goals", - Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - c := newClient(args[0]) + c := newClient() ok, err := c.Operations.PostGraphsGoals(&operations.PostGraphsGoalsParams{ Request: &models.Goals{ Goals: goals, diff --git a/cmd/korrel8rcli/web.go b/cmd/korrel8rcli/web.go index df01065..3d2d8a1 100644 --- a/cmd/korrel8rcli/web.go +++ b/cmd/korrel8rcli/web.go @@ -16,14 +16,14 @@ var webCmd = &cobra.Command{ Short: "Connect to REMOTE-URL and run an HTTP server listening on LISTEN-ADDR (default :8080)", Args: cobra.RangeArgs(1, 2), RunE: func(_ *cobra.Command, args []string) error { - remoteURL := args[0] gin.DefaultWriter = log.Writer() gin.SetMode(gin.ReleaseMode) gin.DisableConsoleColor() router := gin.New() router.Use(gin.Recovery()) router.Use(gin.Logger()) - b, err := browser.New(newClient(remoteURL), router) + c := newClient() + b, err := browser.New(c, router) if err != nil { return err } @@ -32,7 +32,7 @@ var webCmd = &cobra.Command{ Addr: *addr, Handler: router, } - log.Printf("Listening on %v, connected to %v\n", *addr, remoteURL) + log.Println("Listening on ", *addr, " connected to ", *korrel8rURL) return s.ListenAndServe() }, }