From 175f8e816729b942cb986300b7f31d14fa4dfb40 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 30 Sep 2022 15:12:06 -0400 Subject: [PATCH] moved zrok client creation into zrokdir, to better support the new zrokloop utility (#40) --- cmd/zrok/disable.go | 2 +- cmd/zrok/enable.go | 2 +- cmd/zrok/http_backend.go | 2 +- cmd/zrok/invite.go | 3 ++- cmd/zrok/main.go | 17 ----------------- cmd/zrokloop/main.go | 27 +++++++++++++++++++++++++++ zrokdir/client.go | 21 +++++++++++++++++++++ 7 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 cmd/zrokloop/main.go create mode 100644 zrokdir/client.go diff --git a/cmd/zrok/disable.go b/cmd/zrok/disable.go index 2c19115f..d13e86e9 100644 --- a/cmd/zrok/disable.go +++ b/cmd/zrok/disable.go @@ -36,7 +36,7 @@ func (cmd *disableCommand) run(_ *cobra.Command, args []string) { } panic(err) } - zrok, err := newZrokClient(env.ApiEndpoint) + zrok, err := zrokdir.ZrokClient(env.ApiEndpoint) if err != nil { if !panicInstead { showError("could not create zrok service client", err) diff --git a/cmd/zrok/enable.go b/cmd/zrok/enable.go index 9acaa531..2016b5ec 100644 --- a/cmd/zrok/enable.go +++ b/cmd/zrok/enable.go @@ -56,7 +56,7 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { cmd.description = fmt.Sprintf("%v@%v", user.Username, hostName) } - zrok, err := newZrokClient(apiEndpoint) + zrok, err := zrokdir.ZrokClient(apiEndpoint) if err != nil { panic(err) } diff --git a/cmd/zrok/http_backend.go b/cmd/zrok/http_backend.go index 6ecc9111..da1e6705 100644 --- a/cmd/zrok/http_backend.go +++ b/cmd/zrok/http_backend.go @@ -80,7 +80,7 @@ func (self *httpBackendCommand) run(_ *cobra.Command, args []string) { EndpointAddress: args[0], } - zrok, err := newZrokClient(env.ApiEndpoint) + zrok, err := zrokdir.ZrokClient(env.ApiEndpoint) if err != nil { ui.Close() if !panicInstead { diff --git a/cmd/zrok/invite.go b/cmd/zrok/invite.go index 17fd1222..d64c8d8a 100644 --- a/cmd/zrok/invite.go +++ b/cmd/zrok/invite.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/openziti-test-kitchen/zrok/rest_client_zrok/identity" "github.com/openziti-test-kitchen/zrok/rest_model_zrok" + "github.com/openziti-test-kitchen/zrok/zrokdir" "github.com/openziti/foundation/v2/term" "github.com/spf13/cobra" ) @@ -40,7 +41,7 @@ func (cmd *inviteCommand) run(_ *cobra.Command, _ []string) { showError("entered emails do not match... aborting!", nil) } - zrok, err := newZrokClient(apiEndpoint) + zrok, err := zrokdir.ZrokClient(apiEndpoint) if err != nil { if !panicInstead { showError("error creating zrok api client", err) diff --git a/cmd/zrok/main.go b/cmd/zrok/main.go index 4f7a748c..d5016e31 100644 --- a/cmd/zrok/main.go +++ b/cmd/zrok/main.go @@ -1,15 +1,9 @@ package main import ( - "github.com/go-openapi/runtime" - httptransport "github.com/go-openapi/runtime/client" - "github.com/go-openapi/strfmt" "github.com/michaelquigley/pfxlog" - "github.com/openziti-test-kitchen/zrok/rest_client_zrok" - "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "net/url" "os" "path/filepath" "strings" @@ -50,14 +44,3 @@ func main() { panic(err) } } - -func newZrokClient(endpoint string) (*rest_client_zrok.Zrok, error) { - apiUrl, err := url.Parse(endpoint) - if err != nil { - return nil, errors.Wrapf(err, "error parsing api endpoint '%v'", apiEndpoint) - } - transport := httptransport.New(apiUrl.Host, "/api/v1", []string{apiUrl.Scheme}) - transport.Producers["application/zrok.v1+json"] = runtime.JSONProducer() - transport.Consumers["application/zrok.v1+json"] = runtime.JSONConsumer() - return rest_client_zrok.New(transport, strfmt.Default), nil -} diff --git a/cmd/zrokloop/main.go b/cmd/zrokloop/main.go new file mode 100644 index 00000000..f39ab8e9 --- /dev/null +++ b/cmd/zrokloop/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "os" + "path/filepath" + "strings" +) + +var rootCmd = &cobra.Command{ + Use: strings.TrimSuffix(filepath.Base(os.Args[0]), filepath.Ext(os.Args[0])), + Short: "zrok loopback harness", + PersistentPreRun: func(_ *cobra.Command, _ []string) { + if verbose { + logrus.SetLevel(logrus.DebugLevel) + } + }, +} +var verbose bool +var apiEndpoint string + +func main() { + if err := rootCmd.Execute(); err != nil { + panic(err) + } +} diff --git a/zrokdir/client.go b/zrokdir/client.go new file mode 100644 index 00000000..ae037295 --- /dev/null +++ b/zrokdir/client.go @@ -0,0 +1,21 @@ +package zrokdir + +import ( + "github.com/go-openapi/runtime" + httptransport "github.com/go-openapi/runtime/client" + "github.com/go-openapi/strfmt" + "github.com/openziti-test-kitchen/zrok/rest_client_zrok" + "github.com/pkg/errors" + "net/url" +) + +func ZrokClient(endpoint string) (*rest_client_zrok.Zrok, error) { + apiUrl, err := url.Parse(endpoint) + if err != nil { + return nil, errors.Wrapf(err, "error parsing api endpoint '%v'", endpoint) + } + transport := httptransport.New(apiUrl.Host, "/api/v1", []string{apiUrl.Scheme}) + transport.Producers["application/zrok.v1+json"] = runtime.JSONProducer() + transport.Consumers["application/zrok.v1+json"] = runtime.JSONConsumer() + return rest_client_zrok.New(transport, strfmt.Default), nil +}