From 9f162388da012ec76eb7b1034b407912e86e82ee Mon Sep 17 00:00:00 2001 From: Rohit Durvasula Date: Mon, 5 May 2025 12:04:51 -0700 Subject: [PATCH] feat: Make printing metadata to screen optional behind cmd flags. --- main.go | 19 ++++++++++++++----- transport/transport.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 610eef3..5a6415c 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ import ( ) var ( - version = "v0.0.6" + version = "v0.0.7" ) var versionCmd = &cobra.Command{ @@ -29,7 +29,7 @@ var versionCmd = &cobra.Command{ func main() { var data, method, apiKeyPath, header string - var versionFlag bool + var versionFlag, statusFlag, debugFlag bool var id, secret string cmd := &cobra.Command{ @@ -45,7 +45,10 @@ func main() { return fmt.Errorf("URL is required unless using -v") } - opts := []transport.Option{} + opts := []transport.Option{ + transport.WithDebug(debugFlag), + } + if apiKeyPath != "" { opts = append(opts, transport.WithAPIKeyLoaderOption(transport.WithPath(apiKeyPath))) } @@ -94,8 +97,12 @@ func main() { return err } - // Print HTTP status code and response body - fmt.Println(resp.Status) + // Print HTTP status code only if the status flag is set + if statusFlag { + fmt.Println(resp.Status) + } + + // Always print the response body fmt.Println(string(body)) return nil }, @@ -108,6 +115,8 @@ func main() { cmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Print the version number and exit") cmd.Flags().StringVarP(&id, "id", "i", "", "API Key ID (only works with Ed25519 keys)") cmd.Flags().StringVarP(&secret, "secret", "s", "", "API Key Secret (only works with Ed25519 keys)") + cmd.Flags().BoolVarP(&statusFlag, "status", "S", false, "Print the HTTP status code in addition to the response body") + cmd.Flags().BoolVarP(&debugFlag, "debug", "D", false, "Enable debug mode") cmd.AddCommand(versionCmd) diff --git a/transport/transport.go b/transport/transport.go index 0871320..39eada3 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -21,12 +21,14 @@ type transport struct { originalTransport http.RoundTripper authenticator *auth.Authenticator serviceName string + debug bool } type Option func(o *options) type options struct { apiKey *auth.APIKey + debug bool apiKeyOptions []auth.LoadAPIKeyOption } @@ -43,6 +45,12 @@ func WithAPIKey(apiKey *auth.APIKey) Option { } } +func WithDebug(debug bool) Option { + return func(o *options) { + o.debug = debug + } +} + func New(service string, originalTransport http.RoundTripper, opts ...Option) (http.RoundTripper, error) { o := &options{ apiKeyOptions: make([]auth.LoadAPIKeyOption, 0), @@ -65,6 +73,7 @@ func New(service string, originalTransport http.RoundTripper, opts ...Option) (h originalTransport: originalTransport, authenticator: authenticator, serviceName: service, + debug: o.debug, }, nil } @@ -76,6 +85,11 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) { if err != nil { return nil, err } + + if t.debug { + fmt.Println(jwt) + } + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", jwt)) return t.originalTransport.RoundTrip(req) }