Skip to content

Commit

Permalink
Replace URL arg with --url flag or KORREL8R_URL environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
alanconway committed Jun 3, 2024
1 parent 459e7bb commit b53a9fd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
33 changes: 26 additions & 7 deletions cmd/korrel8rcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package main

import (
"errors"
"fmt"
"log"
"net/url"
"path/filepath"
Expand All @@ -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() {
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion cmd/korrel8rcli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 4 additions & 7 deletions cmd/korrel8rcli/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions cmd/korrel8rcli/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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()
},
}
Expand Down

0 comments on commit b53a9fd

Please sign in to comment.