From 4f3ad86c923bc5d32ebd34294c44ee4c350fa7e9 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Thu, 3 Feb 2022 23:36:55 -0800 Subject: [PATCH 01/12] Move main out of cmd/cf-lc-plugin folder --- .gitignore | 26 ++++++++++++++++--- cmd/cf-lc-plugin/.gitignore | 1 - .../cf_log_cache_plugin_suite_test.go | 13 ---------- cmd/cf-lc-plugin/main.go => main.go | 0 scripts/build.sh | 6 ++--- scripts/install.sh | 4 +-- scripts/integration-tests.sh | 4 +-- scripts/uninstall.sh | 2 +- 8 files changed, 29 insertions(+), 27 deletions(-) delete mode 100644 cmd/cf-lc-plugin/.gitignore delete mode 100644 cmd/cf-lc-plugin/cf_log_cache_plugin_suite_test.go rename cmd/cf-lc-plugin/main.go => main.go (100%) diff --git a/.gitignore b/.gitignore index 0bb65833..3136a424 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,22 @@ -log-cache-cli -cf-lc-plugin -*.test -main +# Builds +bin + +# IntelliJ +.idea/ + +# macOS +.DS_Store + +# Vim files +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] +Session.vim +Sessionx.vim +.netrwhist +*~ +tags +[._]*.un~ diff --git a/cmd/cf-lc-plugin/.gitignore b/cmd/cf-lc-plugin/.gitignore deleted file mode 100644 index f7f7c15b..00000000 --- a/cmd/cf-lc-plugin/.gitignore +++ /dev/null @@ -1 +0,0 @@ -cf-lc-plugin diff --git a/cmd/cf-lc-plugin/cf_log_cache_plugin_suite_test.go b/cmd/cf-lc-plugin/cf_log_cache_plugin_suite_test.go deleted file mode 100644 index 3d50f2c3..00000000 --- a/cmd/cf-lc-plugin/cf_log_cache_plugin_suite_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package main_test - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - "testing" -) - -func TestLogCacheCli(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "CF Log Cache Plugin Suite") -} diff --git a/cmd/cf-lc-plugin/main.go b/main.go similarity index 100% rename from cmd/cf-lc-plugin/main.go rename to main.go diff --git a/scripts/build.sh b/scripts/build.sh index 7489fc2f..de4a94ab 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash + set -e version="{\"Major\":0,\"Minor\":0,\"Build\":\"0+dev.0\"}" @@ -8,8 +9,5 @@ WORKSPACE="$SCRIPTS_PATH/.." pushd $WORKSPACE go get ./... -popd - -pushd "$WORKSPACE/cmd/cf-lc-plugin" - go build -ldflags "-X main.version=$version" -o $WORKSPACE/build_artifacts/log-cache-cf-plugin-dev + go build -ldflags "-X main.version=$version" -o $WORKSPACE/bin/log-cache-cf-plugin-dev popd diff --git a/scripts/install.sh b/scripts/install.sh index 4d02f4c5..b9914bad 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -8,6 +8,6 @@ WORKSPACE="$SCRIPTS_PATH/.." $SCRIPTS_PATH/build.sh # Install the log-cache plugin to the CF CLI and force overwrite -cf install-plugin $WORKSPACE/build_artifacts/log-cache-cf-plugin-dev -f +cf install-plugin $WORKSPACE/bin/log-cache-cf-plugin-dev -f -rm -rf $WORKSPACE/build_artifacts +rm -rf $WORKSPACE/bin diff --git a/scripts/integration-tests.sh b/scripts/integration-tests.sh index 975e420e..db1a9f54 100755 --- a/scripts/integration-tests.sh +++ b/scripts/integration-tests.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -euo pipefail @@ -26,7 +26,7 @@ function setup { cd $(dirname $0) - NO_STANDALONE_LC=true ./install.sh + ./install.sh } function cleanup_test_app { diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh index 56338b5c..76376434 100755 --- a/scripts/uninstall.sh +++ b/scripts/uninstall.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash read -r -p "Are you sure you want to remove the log-cache CLI? [y/N]: " remove_cli if [[ ! "$remove_cli" =~ ^[Yy]$ ]]; then From d4498cebbb7ef49402a26d8d00ffcda3632e57bb Mon Sep 17 00:00:00 2001 From: Carson Long Date: Fri, 25 Feb 2022 14:24:10 -0800 Subject: [PATCH 02/12] Change pkg/command/cf path to internal/command path We don't expect any of these commands to be used externally to this cf CLI plugin, so there's no point in exporting them. Moving them behind internal makes them only available to our main package. --- internal/command/command.go | 23 +++++++++++++++++++ .../command}/command_suite_test.go | 4 ++-- .../cf => internal/command}/formatter.go | 2 +- {pkg/command/cf => internal/command}/meta.go | 2 +- .../cf => internal/command}/meta_test.go | 4 ++-- {pkg/command/cf => internal/command}/query.go | 2 +- .../cf => internal/command}/query_test.go | 4 ++-- {pkg/command/cf => internal/command}/tail.go | 16 +------------ .../cf => internal/command}/tail_test.go | 4 ++-- main.go | 2 +- 10 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 internal/command/command.go rename {pkg/command/cf => internal/command}/command_suite_test.go (98%) rename {pkg/command/cf => internal/command}/formatter.go (99%) rename {pkg/command/cf => internal/command}/meta.go (99%) rename {pkg/command/cf => internal/command}/meta_test.go (99%) rename {pkg/command/cf => internal/command}/query.go (99%) rename {pkg/command/cf => internal/command}/query_test.go (98%) rename {pkg/command/cf => internal/command}/tail.go (95%) rename {pkg/command/cf => internal/command}/tail_test.go (99%) diff --git a/internal/command/command.go b/internal/command/command.go new file mode 100644 index 00000000..be9f7c5e --- /dev/null +++ b/internal/command/command.go @@ -0,0 +1,23 @@ +package command + +import ( + "context" + "io" + "net/http" + + "code.cloudfoundry.org/cli/plugin" +) + +// Command is the interface to implement plugin commands +type Command func(ctx context.Context, cli plugin.CliConnection, args []string, c HTTPClient, log Logger, w io.Writer) + +// Logger is used for outputting log-cache results and errors +type Logger interface { + Fatalf(format string, args ...interface{}) + Printf(format string, args ...interface{}) +} + +// HTTPClient is the client used for HTTP requests +type HTTPClient interface { + Do(req *http.Request) (*http.Response, error) +} diff --git a/pkg/command/cf/command_suite_test.go b/internal/command/command_suite_test.go similarity index 98% rename from pkg/command/cf/command_suite_test.go rename to internal/command/command_suite_test.go index 891e7caa..a6c8d4d9 100644 --- a/pkg/command/cf/command_suite_test.go +++ b/internal/command/command_suite_test.go @@ -1,4 +1,4 @@ -package cf_test +package command_test import ( "errors" @@ -9,7 +9,7 @@ import ( "sync" "code.cloudfoundry.org/cli/plugin" - "code.cloudfoundry.org/cli/plugin/models" + plugin_models "code.cloudfoundry.org/cli/plugin/models" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/pkg/command/cf/formatter.go b/internal/command/formatter.go similarity index 99% rename from pkg/command/cf/formatter.go rename to internal/command/formatter.go index 6761d964..faae922b 100644 --- a/pkg/command/cf/formatter.go +++ b/internal/command/formatter.go @@ -1,4 +1,4 @@ -package cf +package command import ( "bytes" diff --git a/pkg/command/cf/meta.go b/internal/command/meta.go similarity index 99% rename from pkg/command/cf/meta.go rename to internal/command/meta.go index beac722d..02dc3bc8 100644 --- a/pkg/command/cf/meta.go +++ b/internal/command/meta.go @@ -1,4 +1,4 @@ -package cf +package command import ( "context" diff --git a/pkg/command/cf/meta_test.go b/internal/command/meta_test.go similarity index 99% rename from pkg/command/cf/meta_test.go rename to internal/command/meta_test.go index 3367198f..0dad7521 100644 --- a/pkg/command/cf/meta_test.go +++ b/internal/command/meta_test.go @@ -1,4 +1,4 @@ -package cf_test +package command_test import ( "bytes" @@ -9,7 +9,7 @@ import ( "os" "strings" - "code.cloudfoundry.org/log-cache-cli/v4/pkg/command/cf" + cf "code.cloudfoundry.org/log-cache-cli/v4/internal/command" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" diff --git a/pkg/command/cf/query.go b/internal/command/query.go similarity index 99% rename from pkg/command/cf/query.go rename to internal/command/query.go index 93cccb6d..bf75010c 100644 --- a/pkg/command/cf/query.go +++ b/internal/command/query.go @@ -1,4 +1,4 @@ -package cf +package command import ( "context" diff --git a/pkg/command/cf/query_test.go b/internal/command/query_test.go similarity index 98% rename from pkg/command/cf/query_test.go rename to internal/command/query_test.go index ce030798..f8355d0c 100644 --- a/pkg/command/cf/query_test.go +++ b/internal/command/query_test.go @@ -1,11 +1,11 @@ -package cf_test +package command_test import ( "context" "fmt" "net/url" - "code.cloudfoundry.org/log-cache-cli/v4/pkg/command/cf" + cf "code.cloudfoundry.org/log-cache-cli/v4/internal/command" . "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo/extensions/table" . "github.com/onsi/gomega" diff --git a/pkg/command/cf/tail.go b/internal/command/tail.go similarity index 95% rename from pkg/command/cf/tail.go rename to internal/command/tail.go index 212845a6..a6b75b2d 100644 --- a/pkg/command/cf/tail.go +++ b/internal/command/tail.go @@ -1,4 +1,4 @@ -package cf +package command import ( "context" @@ -25,20 +25,6 @@ const ( timeFormat = "2006-01-02T15:04:05.00-0700" ) -// Command is the interface to implement plugin commands -type Command func(ctx context.Context, cli plugin.CliConnection, args []string, c HTTPClient, log Logger, w io.Writer) - -// Logger is used for outputting log-cache results and errors -type Logger interface { - Fatalf(format string, args ...interface{}) - Printf(format string, args ...interface{}) -} - -// HTTPClient is the client used for HTTP requests -type HTTPClient interface { - Do(req *http.Request) (*http.Response, error) -} - type TailOption func(*tailOptions) func WithTailNoHeaders() TailOption { diff --git a/pkg/command/cf/tail_test.go b/internal/command/tail_test.go similarity index 99% rename from pkg/command/cf/tail_test.go rename to internal/command/tail_test.go index 3d664f47..c29a8962 100644 --- a/pkg/command/cf/tail_test.go +++ b/internal/command/tail_test.go @@ -1,4 +1,4 @@ -package cf_test +package command_test import ( "context" @@ -10,7 +10,7 @@ import ( "strconv" "time" - "code.cloudfoundry.org/log-cache-cli/v4/pkg/command/cf" + cf "code.cloudfoundry.org/log-cache-cli/v4/internal/command" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) diff --git a/main.go b/main.go index c575dea5..f79415cc 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( "os" "code.cloudfoundry.org/cli/plugin" - "code.cloudfoundry.org/log-cache-cli/v4/pkg/command/cf" + cf "code.cloudfoundry.org/log-cache-cli/v4/internal/command" "golang.org/x/crypto/ssh/terminal" ) From 27036b84a94ccec5b9d1474669e7887c31fc8974 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Fri, 25 Feb 2022 15:04:53 -0800 Subject: [PATCH 03/12] Simplify Run() and remove unused funcs from main Don't need to check for arg size, if there wasn't at least one arg then it wouldn't be redirected to our plugin. By using a switch we can also ignore the case where the plugin is being uninstalled. --- main.go | 76 +++++++++++++++------------------------------------------ 1 file changed, 20 insertions(+), 56 deletions(-) diff --git a/main.go b/main.go index f79415cc..bc87543e 100644 --- a/main.go +++ b/main.go @@ -4,13 +4,12 @@ import ( "context" "crypto/tls" "encoding/json" - "io" "log" "net/http" "os" "code.cloudfoundry.org/cli/plugin" - cf "code.cloudfoundry.org/log-cache-cli/v4/internal/command" + "code.cloudfoundry.org/log-cache-cli/v4/internal/command" "golang.org/x/crypto/ssh/terminal" ) @@ -21,62 +20,36 @@ var version string type LogCacheCLI struct{} -var commands = make(map[string]cf.Command) - func (c *LogCacheCLI) Run(conn plugin.CliConnection, args []string) { - if len(args) == 1 && args[0] == "CLI-MESSAGE-UNINSTALL" { - // someone's uninstalling the plugin, but we don't need to clean up - return - } - - if len(args) < 1 { - log.Fatalf("Expected at least 1 argument, but got %d.", len(args)) - } - isTerminal := terminal.IsTerminal(int(os.Stdout.Fd())) - commands["query"] = func(ctx context.Context, cli plugin.CliConnection, args []string, c cf.HTTPClient, log cf.Logger, tableWriter io.Writer) { - var opts []cf.QueryOption - cf.Query(ctx, cli, args, c, log, tableWriter, opts...) - } - - commands["tail"] = func(ctx context.Context, cli plugin.CliConnection, args []string, c cf.HTTPClient, log cf.Logger, tableWriter io.Writer) { - var opts []cf.TailOption - if !isTerminal { - opts = append(opts, cf.WithTailNoHeaders()) - } - cf.Tail(ctx, cli, args, c, log, tableWriter, opts...) - } - - commands["log-meta"] = func(ctx context.Context, cli plugin.CliConnection, args []string, c cf.HTTPClient, log cf.Logger, tableWriter io.Writer) { - var opts []cf.MetaOption - if !isTerminal { - opts = append(opts, cf.WithMetaNoHeaders()) - } - cf.Meta( - ctx, - cli, - args, - c, - log, - tableWriter, - opts..., - ) - } - skipSSL, err := conn.IsSSLDisabled() if err != nil { - log.Fatalf("%s", err) + log.Fatal(err) } http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{ InsecureSkipVerify: skipSSL, } - op, ok := commands[args[0]] - if !ok { - log.Fatalf("Unknown Log Cache command: %s", args[0]) + l := log.New(os.Stderr, "", 0) + + switch args[0] { + case "query": + var opts []command.QueryOption + command.Query(context.Background(), conn, args[1:], http.DefaultClient, l, os.Stdout, opts...) + case "tail": + var opts []command.TailOption + if !isTerminal { + opts = append(opts, command.WithTailNoHeaders()) + } + command.Tail(context.Background(), conn, args[1:], http.DefaultClient, l, os.Stdout, opts...) + case "log-meta": + var opts []command.MetaOption + if !isTerminal { + opts = append(opts, command.WithMetaNoHeaders()) + } + command.Meta(context.Background(), conn, args[1:], http.DefaultClient, l, os.Stdout, opts...) } - op(context.Background(), conn, args[1:], http.DefaultClient, log.New(os.Stderr, "", 0), os.Stdout) } func (c *LogCacheCLI) GetMetadata() plugin.PluginMetadata { @@ -152,12 +125,3 @@ ENVIRONMENT VARIABLES: func main() { plugin.Start(&LogCacheCLI{}) } - -type linesWriter struct { - lines []string -} - -func (w *linesWriter) Write(data []byte) (int, error) { - w.lines = append(w.lines, string(data)) - return len(data), nil -} From 471bc8b95d98de858d08c33cb15018b6dddcd6be Mon Sep 17 00:00:00 2001 From: Carson Long Date: Fri, 25 Feb 2022 15:32:22 -0800 Subject: [PATCH 04/12] Remove some unused code and correct some style mistakes --- internal/command/command_suite_test.go | 7 ------ internal/command/formatter.go | 4 +-- internal/command/meta.go | 34 +++++--------------------- internal/command/meta_test.go | 7 ------ internal/command/tail.go | 15 ------------ internal/command/tail_test.go | 19 ++++++++++++++ 6 files changed, 27 insertions(+), 59 deletions(-) diff --git a/internal/command/command_suite_test.go b/internal/command/command_suite_test.go index a6c8d4d9..f68f890f 100644 --- a/internal/command/command_suite_test.go +++ b/internal/command/command_suite_test.go @@ -190,10 +190,3 @@ func (s *stubCliConnection) AccessToken() (string, error) { s.accessTokenCount++ return s.accessToken, s.accessTokenErr } - -func (s *stubCliConnection) getAccessTokenCount() int { - s.Lock() - defer s.Unlock() - - return s.accessTokenCount -} diff --git a/internal/command/formatter.go b/internal/command/formatter.go index faae922b..b85def69 100644 --- a/internal/command/formatter.go +++ b/internal/command/formatter.go @@ -122,7 +122,7 @@ func (f prettyFormatter) sourceHeader(sourceID, _, _, user string) (string, bool } func (f prettyFormatter) formatEnvelope(e *loggregator_v2.Envelope) (string, bool) { - return fmt.Sprintf("%s", envelopeWrapper{sourceID: f.sourceID, Envelope: e, newLine: f.newLine}), true + return envelopeWrapper{sourceID: f.sourceID, Envelope: e, newLine: f.newLine}.String(), true } type jsonFormatter struct { @@ -251,7 +251,7 @@ func (e envelopeWrapper) String() string { values = append(values, fmt.Sprintf("%s:%f %s", k, v.Value, v.Unit)) } - sort.Sort(sort.StringSlice(values)) + sort.Strings(values) return fmt.Sprintf("%sGAUGE %s", e.header(ts), diff --git a/internal/command/meta.go b/internal/command/meta.go index 02dc3bc8..deb2d76b 100644 --- a/internal/command/meta.go +++ b/internal/command/meta.go @@ -8,7 +8,6 @@ import ( "os" "regexp" "sort" - "strconv" "strings" "text/tabwriter" "time" @@ -274,19 +273,21 @@ func tableFormat(opts optionsFlags, row displayRow) (string, []interface{}) { func writeRetrievingMetaHeader(opts optionsFlags, tableWriter io.Writer, username string) { if opts.withHeaders { - fmt.Fprintf(tableWriter, fmt.Sprintf( + fmt.Fprintf( + tableWriter, "Retrieving log cache metadata as %s...\n\n", username, - )) + ) } } func writeAppsAndServicesHeader(opts optionsFlags, tableWriter io.Writer, username string) { if opts.withHeaders { - fmt.Fprintf(tableWriter, fmt.Sprintf( + fmt.Fprintf( + tableWriter, "Retrieving app and service names as %s...\n\n", username, - )) + ) } } @@ -375,18 +376,6 @@ func getOptions(args []string, log Logger, mopts ...MetaOption) optionsFlags { return opts } -func displayRate(rate int) string { - var output string - - if rate >= MaximumBatchSize { - output = fmt.Sprintf(">%d", MaximumBatchSize-1) - } else { - output = strconv.Itoa(rate) - } - - return output -} - func sortRows(opts optionsFlags, rows []displayRow) { switch opts.SortBy { case string(sortBySourceID): @@ -530,17 +519,6 @@ func maxDuration(a, b time.Duration) time.Duration { return a } -func truncate(count int, entries map[string]*logcache_v1.MetaInfo) map[string]*logcache_v1.MetaInfo { - truncated := make(map[string]*logcache_v1.MetaInfo) - for k, v := range entries { - if len(truncated) >= count { - break - } - truncated[k] = v - } - return truncated -} - func logCacheEndpoint(cli plugin.CliConnection) (string, error) { logCacheAddr := os.Getenv("LOG_CACHE_ADDR") diff --git a/internal/command/meta_test.go b/internal/command/meta_test.go index 0dad7521..6669253c 100644 --- a/internal/command/meta_test.go +++ b/internal/command/meta_test.go @@ -1240,13 +1240,6 @@ var _ = Describe("Meta", func() { }) }) -func generateBatch(count int) []string { - x := strings.Repeat("{},", count-1) - x += "{}" - - return []string{fmt.Sprintf(`{"batch": [%s]}`, x)} -} - func metaResponseInfo(sourceIDs ...string) string { var metaInfos []string metaInfos = append(metaInfos, fmt.Sprintf(`"%s": { diff --git a/internal/command/tail.go b/internal/command/tail.go index a6b75b2d..050cf5a3 100644 --- a/internal/command/tail.go +++ b/internal/command/tail.go @@ -474,21 +474,6 @@ func checkFeatureVersioning(client *logcache.Client, ctx context.Context, log Lo } } -type backoff struct { - logcache.AlwaysDoneBackoff - - logger Logger -} - -func newBackoff(log Logger) backoff { - return backoff{logger: log} -} - -func (b backoff) OnErr(err error) bool { - b.logger.Fatalf("%s", err) - return b.AlwaysDoneBackoff.OnErr(err) -} - type tokenHTTPClient struct { c HTTPClient tokenFunc func() string diff --git a/internal/command/tail_test.go b/internal/command/tail_test.go index c29a8962..d1f41e1b 100644 --- a/internal/command/tail_test.go +++ b/internal/command/tail_test.go @@ -78,6 +78,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).To(HaveLen(1)) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) end, err := strconv.ParseInt(requestURL.Query().Get("end_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(end).To(BeNumerically("~", time.Now().UnixNano(), 10000000)) @@ -113,6 +114,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).To(HaveLen(1)) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) end, err := strconv.ParseInt(requestURL.Query().Get("end_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(end).To(BeNumerically("~", time.Now().UnixNano(), 10000000)) @@ -147,6 +149,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).To(HaveLen(1)) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) end, err := strconv.ParseInt(requestURL.Query().Get("end_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(end).To(BeNumerically("~", time.Now().UnixNano(), 10000000)) @@ -179,6 +182,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).To(HaveLen(1)) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) end, err := strconv.ParseInt(requestURL.Query().Get("end_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(end).To(BeNumerically("~", time.Now().UnixNano(), 10000000)) @@ -214,6 +218,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).To(HaveLen(1)) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) end, err := strconv.ParseInt(requestURL.Query().Get("end_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(end).To(BeNumerically("~", time.Now().UnixNano(), 10000000)) @@ -390,6 +395,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).ToNot(BeEmpty()) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) start, err := strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) @@ -403,6 +409,7 @@ var _ = Describe("LogCache", func() { Expect(envelopeType).To(Equal("ANY")) requestURL, err = url.Parse(httpClient.requestURLs[1]) + Expect(err).ToNot(HaveOccurred()) start, err = strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(start).To(Equal(startTime.Add(-28*time.Second).UnixNano() + 1)) @@ -452,6 +459,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).ToNot(BeEmpty()) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) start, err := strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) @@ -465,6 +473,7 @@ var _ = Describe("LogCache", func() { Expect(envelopeType).To(Equal("ANY")) requestURL, err = url.Parse(httpClient.requestURLs[1]) + Expect(err).ToNot(HaveOccurred()) start, err = strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(start).To(Equal(startTime.Add(-28*time.Second).UnixNano() + 1)) @@ -514,6 +523,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).ToNot(BeEmpty()) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) start, err := strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) @@ -527,6 +537,7 @@ var _ = Describe("LogCache", func() { Expect(envelopeType).To(Equal("ANY")) requestURL, err = url.Parse(httpClient.requestURLs[1]) + Expect(err).ToNot(HaveOccurred()) start, err = strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(start).To(Equal(startTime.Add(-28*time.Second).UnixNano() + 1)) @@ -602,6 +613,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).ToNot(BeEmpty()) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) start, err := strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) @@ -615,6 +627,7 @@ var _ = Describe("LogCache", func() { Expect(envelopeType).To(Equal("ANY")) requestURL, err = url.Parse(httpClient.requestURLs[1]) + Expect(err).ToNot(HaveOccurred()) start, err = strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(start).To(Equal(startTime.Add(-28*time.Second).UnixNano() + 1)) @@ -673,6 +686,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).ToNot(BeEmpty()) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) start, err := strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) @@ -686,6 +700,7 @@ var _ = Describe("LogCache", func() { Expect(envelopeType).To(Equal("ANY")) requestURL, err = url.Parse(httpClient.requestURLs[1]) + Expect(err).ToNot(HaveOccurred()) start, err = strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(start).To(Equal(startTime.Add(-28*time.Second).UnixNano() + 1)) @@ -744,6 +759,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).ToNot(BeEmpty()) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) start, err := strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) @@ -757,6 +773,7 @@ var _ = Describe("LogCache", func() { Expect(envelopeType).To(Equal("ANY")) requestURL, err = url.Parse(httpClient.requestURLs[1]) + Expect(err).ToNot(HaveOccurred()) start, err = strconv.ParseInt(requestURL.Query().Get("start_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(start).To(Equal(startTime.Add(-28*time.Second).UnixNano() + 1)) @@ -890,6 +907,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).To(HaveLen(1)) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) end, err := strconv.ParseInt(requestURL.Query().Get("end_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(end).To(BeNumerically("~", time.Now().UnixNano(), 10000000)) @@ -1582,6 +1600,7 @@ var _ = Describe("LogCache", func() { Expect(httpClient.requestURLs).To(HaveLen(1)) requestURL, err := url.Parse(httpClient.requestURLs[0]) + Expect(err).ToNot(HaveOccurred()) end, err := strconv.ParseInt(requestURL.Query().Get("end_time"), 10, 64) Expect(err).ToNot(HaveOccurred()) Expect(end).To(BeNumerically("~", time.Now().UnixNano(), 10000000)) From f3e4c67e2f546620823956b1e3b57b9c37761775 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Fri, 25 Feb 2022 16:56:33 -0800 Subject: [PATCH 05/12] Move shared function to command.go for readability --- internal/command/command.go | 15 +++++++++++++++ internal/command/tail.go | 16 ---------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/internal/command/command.go b/internal/command/command.go index be9f7c5e..10d0064f 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -21,3 +21,18 @@ type Logger interface { type HTTPClient interface { Do(req *http.Request) (*http.Response, error) } + +type tokenHTTPClient struct { + c HTTPClient + tokenFunc func() string +} + +func (c *tokenHTTPClient) Do(req *http.Request) (*http.Response, error) { + accessToken := c.tokenFunc() + if len(accessToken) > 0 { + req.Header.Set("Authorization", accessToken) + } + + return c.c.Do(req) + +} diff --git a/internal/command/tail.go b/internal/command/tail.go index 050cf5a3..ffb30694 100644 --- a/internal/command/tail.go +++ b/internal/command/tail.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "net/http" "os" "regexp" "strings" @@ -473,18 +472,3 @@ func checkFeatureVersioning(client *logcache.Client, ctx context.Context, log Lo } } } - -type tokenHTTPClient struct { - c HTTPClient - tokenFunc func() string -} - -func (c *tokenHTTPClient) Do(req *http.Request) (*http.Response, error) { - accessToken := c.tokenFunc() - if len(accessToken) > 0 { - req.Header.Set("Authorization", accessToken) - } - - return c.c.Do(req) - -} From 6f2def64e672f3952195d312146d8c32ee9cd7e7 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Fri, 25 Feb 2022 16:57:51 -0800 Subject: [PATCH 06/12] Change metadata to be closer to cf CLI usage --- internal/command/query.go | 10 ++-------- internal/command/query_test.go | 2 +- main.go | 9 ++++----- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/internal/command/query.go b/internal/command/query.go index bf75010c..f253e2b2 100644 --- a/internal/command/query.go +++ b/internal/command/query.go @@ -16,8 +16,6 @@ import ( flags "github.com/jessevdk/go-flags" ) -type QueryOption func(*queryOptions) - func Query( ctx context.Context, cli plugin.CliConnection, @@ -25,11 +23,11 @@ func Query( c HTTPClient, log Logger, w io.Writer, - opts ...QueryOption, ) { if len(args) < 1 { - log.Fatalf("Must specify a PromQL query") + log.Fatalf("Incorrect Usage: the required argument `PROMQL_QUERY` was not provided") } + query := args[0] queryOptions, err := newQueryOptions(cli, args, log) @@ -37,10 +35,6 @@ func Query( log.Fatalf("%s", err) } - for _, opt := range opts { - opt(&queryOptions) - } - lw := lineWriter{w: w} if strings.ToLower(os.Getenv("LOG_CACHE_SKIP_AUTH")) != "true" { diff --git a/internal/command/query_test.go b/internal/command/query_test.go index f8355d0c..01e30e49 100644 --- a/internal/command/query_test.go +++ b/internal/command/query_test.go @@ -61,7 +61,7 @@ var _ = Describe("LogCache", func() { tc.query() }).To(Panic()) - Expect(tc.logger.fatalfMessage).To(HavePrefix(`Must specify a PromQL query`)) + Expect(tc.logger.fatalfMessage).To(HavePrefix("Incorrect Usage: the required argument `PROMQL_QUERY` was not provided")) Expect(tc.httpClient.requestURLs).To(HaveLen(0)) }) }) diff --git a/main.go b/main.go index bc87543e..a9cf9989 100644 --- a/main.go +++ b/main.go @@ -35,8 +35,7 @@ func (c *LogCacheCLI) Run(conn plugin.CliConnection, args []string) { switch args[0] { case "query": - var opts []command.QueryOption - command.Query(context.Background(), conn, args[1:], http.DefaultClient, l, os.Stdout, opts...) + command.Query(context.Background(), conn, args[1:], http.DefaultClient, l, os.Stdout) case "tail": var opts []command.TailOption if !isTerminal { @@ -66,7 +65,7 @@ func (c *LogCacheCLI) GetMetadata() plugin.PluginMetadata { Name: "tail", HelpText: "Output logs for a source-id/app", UsageDetails: plugin.Usage{ - Usage: `tail [options] + Usage: `tail [options] SOURCE_ID ENVIRONMENT VARIABLES: LOG_CACHE_ADDR Overrides the default location of log-cache. @@ -105,13 +104,13 @@ ENVIRONMENT VARIABLES: Name: "query", HelpText: "Issues a PromQL query against Log Cache", UsageDetails: plugin.Usage{ - Usage: `query [options] + Usage: `query PROMQL_QUERY [options] ENVIRONMENT VARIABLES: LOG_CACHE_ADDR Overrides the default location of log-cache. LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`, Options: map[string]string{ - "-time": "Effective time for query execution of an instant query. Cannont be used with --start, --end, or --step. Can be a unix timestamp or RFC3339.", + "-time": "Effective time for query execution of an instant query. Cannot be used with --start, --end, or --step. Can be a unix timestamp or RFC3339.", "-start": "Start time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339.", "-end": "End time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339.", "-step": "Step interval for a range query. Cannot be used with --time.", From 79ed9e3805567cb99e941329afd912b5413a5c3e Mon Sep 17 00:00:00 2001 From: Carson Long Date: Fri, 25 Feb 2022 16:58:29 -0800 Subject: [PATCH 07/12] Change test description to match files --- internal/command/query_test.go | 2 +- internal/command/tail_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/command/query_test.go b/internal/command/query_test.go index 01e30e49..b8b499ed 100644 --- a/internal/command/query_test.go +++ b/internal/command/query_test.go @@ -11,7 +11,7 @@ import ( . "github.com/onsi/gomega" ) -var _ = Describe("LogCache", func() { +var _ = Describe("Query", func() { Describe("error handling for queries", func() { It("reports an error for a failed request", func() { tc := setup("", 503) diff --git a/internal/command/tail_test.go b/internal/command/tail_test.go index d1f41e1b..6b4829bb 100644 --- a/internal/command/tail_test.go +++ b/internal/command/tail_test.go @@ -15,7 +15,7 @@ import ( . "github.com/onsi/gomega" ) -var _ = Describe("LogCache", func() { +var _ = Describe("Tail", func() { var ( logger *stubLogger writer *stubWriter From 02fb80066204c1d40dbea3d50c79f3762b59206a Mon Sep 17 00:00:00 2001 From: Carson Long Date: Fri, 25 Feb 2022 17:21:21 -0800 Subject: [PATCH 08/12] Remove Command.Command type --- internal/command/command.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/internal/command/command.go b/internal/command/command.go index 10d0064f..a773d47e 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -1,16 +1,9 @@ package command import ( - "context" - "io" "net/http" - - "code.cloudfoundry.org/cli/plugin" ) -// Command is the interface to implement plugin commands -type Command func(ctx context.Context, cli plugin.CliConnection, args []string, c HTTPClient, log Logger, w io.Writer) - // Logger is used for outputting log-cache results and errors type Logger interface { Fatalf(format string, args ...interface{}) From c76b3b0a79ff3021c339a57cea52cf25f32b9457 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Fri, 25 Feb 2022 17:23:13 -0800 Subject: [PATCH 09/12] Update README to match metadata changes --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8cb2bc42..17573329 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ NAME: tail - Output logs for a source-id/app USAGE: - tail [options] + tail [options] SOURCE_ID ENVIRONMENT VARIABLES: LOG_CACHE_ADDR Overrides the default location of log-cache. @@ -94,14 +94,14 @@ NAME: query - Issues a PromQL query against Log Cache USAGE: - query [options] + query PROMQL_QUERY [options] ENVIRONMENT VARIABLES: LOG_CACHE_ADDR Overrides the default location of log-cache. LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication. OPTIONS: - --end End time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339. + --end End time for a range query. Cannot be used with --time. Can be a unix timestamp or RFC3339. --start Start time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339. --step Step interval for a range query. Cannot be used with --time. --time Effective time for query execution of an instant query. Cannont be used with --start, --end, or --step. Can be a unix timestamp or RFC3339. From 5fb1d14f75e49036ab251dffd33abb053421fe6b Mon Sep 17 00:00:00 2001 From: Carson Long Date: Mon, 28 Feb 2022 15:44:01 -0800 Subject: [PATCH 10/12] Remove unused context param from meta --- internal/command/meta.go | 5 ++--- internal/command/meta_test.go | 35 ----------------------------------- main.go | 2 +- 3 files changed, 3 insertions(+), 39 deletions(-) diff --git a/internal/command/meta.go b/internal/command/meta.go index deb2d76b..7a1c4757 100644 --- a/internal/command/meta.go +++ b/internal/command/meta.go @@ -105,7 +105,6 @@ func WithMetaNoiseSleepDuration(d time.Duration) MetaOption { // Meta returns the metadata from Log Cache func Meta( - ctx context.Context, cli plugin.CliConnection, args []string, c HTTPClient, @@ -124,7 +123,7 @@ func Meta( var originalMeta map[string]*logcache_v1.MetaInfo var currentMeta map[string]*logcache_v1.MetaInfo writeRetrievingMetaHeader(opts, tw, username) - currentMeta, err = client.Meta(ctx) + currentMeta, err = client.Meta(context.Background()) if err != nil { log.Fatalf("Failed to read Meta information: %s", err) } @@ -134,7 +133,7 @@ func Meta( writeWaiting(opts, tw, username) time.Sleep(opts.metaNoiseSleepDuration) writeRetrievingMetaHeader(opts, tw, username) - currentMeta, err = client.Meta(ctx) + currentMeta, err = client.Meta(context.Background()) if err != nil { log.Fatalf("Failed to read Meta information: %s", err) } diff --git a/internal/command/meta_test.go b/internal/command/meta_test.go index 6669253c..13a6d5e9 100644 --- a/internal/command/meta_test.go +++ b/internal/command/meta_test.go @@ -2,7 +2,6 @@ package command_test import ( "bytes" - "context" "errors" "fmt" "net/url" @@ -57,7 +56,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, []string{"--noise", "--sort-by", "rate"}, httpClient, @@ -115,7 +113,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, []string{"--sort-by", "source-type"}, httpClient, @@ -162,7 +159,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, []string{"--sort-by", "count"}, httpClient, @@ -210,7 +206,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, []string{"--sort-by", "expired"}, httpClient, @@ -258,7 +253,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, []string{"--sort-by", "cache-duration"}, httpClient, @@ -291,7 +285,6 @@ var _ = Describe("Meta", func() { It("fatally logs when --sort-by is not valid", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, []string{"--sort-by", "invalid"}, httpClient, @@ -306,7 +299,6 @@ var _ = Describe("Meta", func() { It("fatally logs when --source-type other than 'platform' is used with --guid", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, []string{"--guid", "--source-type", "not-platform"}, httpClient, @@ -321,7 +313,6 @@ var _ = Describe("Meta", func() { It("fatally logs when --sort-by source is used with --guid", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, []string{"--guid", "--sort-by", "source"}, httpClient, @@ -336,7 +327,6 @@ var _ = Describe("Meta", func() { It("fatally logs when --sort-by source-type is used with --guid", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, []string{"--guid", "--sort-by", "source-type"}, httpClient, @@ -351,7 +341,6 @@ var _ = Describe("Meta", func() { It("fatally logs when --sort-by source-type is used with --guid", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, []string{"--guid", "--sort-by", "source-type"}, httpClient, @@ -366,7 +355,6 @@ var _ = Describe("Meta", func() { It("fatally logs when --sort-by rate is used without --noise", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, []string{"--sort-by", "rate"}, httpClient, @@ -395,7 +383,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, []string{"--guid"}, httpClient, @@ -436,7 +423,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, []string{"--guid"}, httpClient, @@ -465,7 +451,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -522,7 +507,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, []string{"--noise"}, httpClient, @@ -575,7 +559,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -641,7 +624,6 @@ var _ = Describe("Meta", func() { args := []string{"--source-type", "application"} cf.Meta( - context.Background(), cliConn, args, httpClient, @@ -687,7 +669,6 @@ var _ = Describe("Meta", func() { args := []string{"--source-type", "service"} cf.Meta( - context.Background(), cliConn, args, httpClient, @@ -733,7 +714,6 @@ var _ = Describe("Meta", func() { args := []string{"--source-type", "PLATFORM"} cf.Meta( - context.Background(), cliConn, args, httpClient, @@ -778,7 +758,6 @@ var _ = Describe("Meta", func() { args := []string{"--source-type", "all"} cf.Meta( - context.Background(), cliConn, args, httpClient, @@ -824,7 +803,6 @@ var _ = Describe("Meta", func() { args := []string{"--source-type", "unknown"} cf.Meta( - context.Background(), cliConn, args, httpClient, @@ -868,7 +846,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -913,7 +890,6 @@ var _ = Describe("Meta", func() { args := []string{"--guid"} cf.Meta( - context.Background(), cliConn, args, httpClient, @@ -954,7 +930,6 @@ var _ = Describe("Meta", func() { args := []string{"--source-type", "PLATFORM", "--guid"} cf.Meta( - context.Background(), cliConn, args, httpClient, @@ -1001,7 +976,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -1060,7 +1034,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -1091,7 +1064,6 @@ var _ = Describe("Meta", func() { cliConn.cliCommandErr = nil cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -1105,7 +1077,6 @@ var _ = Describe("Meta", func() { It("fatally logs when it receives too many arguments", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, []string{"extra-arg"}, httpClient, @@ -1121,7 +1092,6 @@ var _ = Describe("Meta", func() { args := []string{"--source-type", "invalid"} Expect(func() { cf.Meta( - context.Background(), cliConn, args, httpClient, @@ -1138,7 +1108,6 @@ var _ = Describe("Meta", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -1160,7 +1129,6 @@ var _ = Describe("Meta", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -1188,7 +1156,6 @@ var _ = Describe("Meta", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -1210,7 +1177,6 @@ var _ = Describe("Meta", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, nil, httpClient, @@ -1227,7 +1193,6 @@ var _ = Describe("Meta", func() { Expect(func() { cf.Meta( - context.Background(), cliConn, nil, httpClient, diff --git a/main.go b/main.go index a9cf9989..534959ca 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ func (c *LogCacheCLI) Run(conn plugin.CliConnection, args []string) { if !isTerminal { opts = append(opts, command.WithMetaNoHeaders()) } - command.Meta(context.Background(), conn, args[1:], http.DefaultClient, l, os.Stdout, opts...) + command.Meta(conn, args[1:], http.DefaultClient, l, os.Stdout, opts...) } } From c14ad57b9450a4872ab244192b3837c30c03ff99 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Mon, 28 Feb 2022 17:10:10 -0800 Subject: [PATCH 11/12] Remove unused context param from query --- internal/command/query.go | 1 - internal/command/query_test.go | 2 -- main.go | 2 +- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/command/query.go b/internal/command/query.go index f253e2b2..6c4d2347 100644 --- a/internal/command/query.go +++ b/internal/command/query.go @@ -17,7 +17,6 @@ import ( ) func Query( - ctx context.Context, cli plugin.CliConnection, args []string, c HTTPClient, diff --git a/internal/command/query_test.go b/internal/command/query_test.go index b8b499ed..7f40666b 100644 --- a/internal/command/query_test.go +++ b/internal/command/query_test.go @@ -1,7 +1,6 @@ package command_test import ( - "context" "fmt" "net/url" @@ -267,7 +266,6 @@ func setup(responseBody string, responseCode int) *testContext { func (tc *testContext) query(args ...string) { cf.Query( - context.Background(), tc.cliConnection, args, tc.httpClient, diff --git a/main.go b/main.go index 534959ca..294c8596 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ func (c *LogCacheCLI) Run(conn plugin.CliConnection, args []string) { switch args[0] { case "query": - command.Query(context.Background(), conn, args[1:], http.DefaultClient, l, os.Stdout) + command.Query(conn, args[1:], http.DefaultClient, l, os.Stdout) case "tail": var opts []command.TailOption if !isTerminal { From ddbe2bc4447ffafb10a9af44374425a98357689e Mon Sep 17 00:00:00 2001 From: Carson Long Date: Tue, 1 Mar 2022 09:38:56 -0800 Subject: [PATCH 12/12] Refactor commands to use a shared function to create a log cache client --- internal/command/command.go | 34 ++++++++++++++ internal/command/meta.go | 44 +----------------- internal/command/query.go | 36 +-------------- internal/command/tail.go | 87 +++++++++++------------------------ internal/command/tail_test.go | 37 +-------------- 5 files changed, 63 insertions(+), 175 deletions(-) diff --git a/internal/command/command.go b/internal/command/command.go index a773d47e..e7bc3012 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -2,6 +2,11 @@ package command import ( "net/http" + "os" + "strings" + + "code.cloudfoundry.org/cli/plugin" + logcache "code.cloudfoundry.org/go-log-cache" ) // Logger is used for outputting log-cache results and errors @@ -29,3 +34,32 @@ func (c *tokenHTTPClient) Do(req *http.Request) (*http.Response, error) { return c.c.Do(req) } + +func newLogCacheClient(c HTTPClient, log Logger, cli plugin.CliConnection) *logcache.Client { + addr := os.Getenv("LOG_CACHE_ADDR") + if addr == "" { + addrAPI, err := cli.ApiEndpoint() + if err != nil { + log.Fatalf("Could not determine Log Cache endpoint: %s", err) + } + addr = strings.Replace(addrAPI, "api", "log-cache", 1) + } + + if strings.ToLower(os.Getenv("LOG_CACHE_SKIP_AUTH")) != "true" { + c = &tokenHTTPClient{ + c: c, + tokenFunc: func() string { + token, err := cli.AccessToken() + if err != nil { + log.Fatalf("Unable to get Access Token: %s", err) + } + return token + }, + } + } + + return logcache.NewClient( + addr, + logcache.WithHTTPClient(c), + ) +} diff --git a/internal/command/meta.go b/internal/command/meta.go index 7a1c4757..32250c06 100644 --- a/internal/command/meta.go +++ b/internal/command/meta.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "os" "regexp" "sort" "strings" @@ -13,7 +12,6 @@ import ( "time" "code.cloudfoundry.org/cli/plugin" - logcache "code.cloudfoundry.org/go-log-cache" logcache_v1 "code.cloudfoundry.org/go-log-cache/rpc/logcache_v1" flags "github.com/jessevdk/go-flags" ) @@ -113,7 +111,7 @@ func Meta( mopts ...MetaOption, ) { opts := getOptions(args, log, mopts...) - client := createLogCacheClient(c, log, cli) + client := newLogCacheClient(c, log, cli) tw := tabwriter.NewWriter(tableWriter, 0, 2, 2, ' ', 0) username, err := cli.Username() if err != nil { @@ -225,31 +223,6 @@ type displayRow struct { Delta int64 } -func createLogCacheClient(c HTTPClient, log Logger, cli plugin.CliConnection) *logcache.Client { - logCacheEndpoint, err := logCacheEndpoint(cli) - if err != nil { - log.Fatalf("Could not determine Log Cache endpoint: %s", err) - } - - if strings.ToLower(os.Getenv("LOG_CACHE_SKIP_AUTH")) != "true" { - c = &tokenHTTPClient{ - c: c, - tokenFunc: func() string { - token, err := cli.AccessToken() - if err != nil { - log.Fatalf("Unable to get Access Token: %s", err) - } - return token - }, - } - } - - return logcache.NewClient( - logCacheEndpoint, - logcache.WithHTTPClient(c), - ) -} - func tableFormat(opts optionsFlags, row displayRow) (string, []interface{}) { tableFormat := "%d\t%d\t%s\n" items := []interface{}{interface{}(row.Count), interface{}(row.Expired), interface{}(row.CacheDuration)} @@ -518,21 +491,6 @@ func maxDuration(a, b time.Duration) time.Duration { return a } -func logCacheEndpoint(cli plugin.CliConnection) (string, error) { - logCacheAddr := os.Getenv("LOG_CACHE_ADDR") - - if logCacheAddr != "" { - return logCacheAddr, nil - } - - apiEndpoint, err := cli.ApiEndpoint() - if err != nil { - return "", err - } - - return strings.Replace(apiEndpoint, "api", "log-cache", 1), nil -} - func invalidSourceType(st string) bool { validSourceTypes := []sourceType{ sourceTypePlatform, diff --git a/internal/command/query.go b/internal/command/query.go index 6c4d2347..95d0313e 100644 --- a/internal/command/query.go +++ b/internal/command/query.go @@ -6,9 +6,7 @@ import ( "errors" "fmt" "io" - "os" "strconv" - "strings" "time" "code.cloudfoundry.org/cli/plugin" @@ -36,39 +34,7 @@ func Query( lw := lineWriter{w: w} - if strings.ToLower(os.Getenv("LOG_CACHE_SKIP_AUTH")) != "true" { - c = &tokenHTTPClient{ - c: c, - tokenFunc: func() string { - token, err := cli.AccessToken() - if err != nil { - log.Fatalf("Unable to get Access Token: %s", err) - } - return token - }, - } - } - - logCacheAddr := os.Getenv("LOG_CACHE_ADDR") - if logCacheAddr == "" { - hasAPI, err := cli.HasAPIEndpoint() - if err != nil { - log.Fatalf("%s", err) - } - - if !hasAPI { - log.Fatalf("No API endpoint targeted.") - } - - tokenURL, err := cli.ApiEndpoint() - if err != nil { - log.Fatalf("%s", err) - } - - logCacheAddr = strings.Replace(tokenURL, "api", "log-cache", 1) - } - - client := logcache.NewClient(logCacheAddr, logcache.WithHTTPClient(c)) + client := newLogCacheClient(c, log, cli) var res *logcache.PromQLQueryResult diff --git a/internal/command/tail.go b/internal/command/tail.go index ffb30694..e56ad2b3 100644 --- a/internal/command/tail.go +++ b/internal/command/tail.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "os" "regexp" "strings" "text/template" @@ -62,54 +61,37 @@ func Tail( } }() - logCacheAddr := os.Getenv("LOG_CACHE_ADDR") - if logCacheAddr == "" { - hasAPI, err := cli.HasAPIEndpoint() - if err != nil { - log.Fatalf("%s", err) - } + client := newLogCacheClient(c, log, cli) - if !hasAPI { - log.Fatalf("No API endpoint targeted.") - } - - tokenURL, err := cli.ApiEndpoint() - if err != nil { - log.Fatalf("%s", err) - } - - user, err := cli.Username() - if err != nil { - log.Fatalf("%s", err) - } - - org, err := cli.GetCurrentOrg() - if err != nil { - log.Fatalf("%s", err) - } + user, err := cli.Username() + if err != nil { + log.Fatalf("%s", err) + } - space, err := cli.GetCurrentSpace() - if err != nil { - log.Fatalf("%s", err) - } + org, err := cli.GetCurrentOrg() + if err != nil { + log.Fatalf("%s", err) + } - logCacheAddr = strings.Replace(tokenURL, "api", "log-cache", 1) + space, err := cli.GetCurrentSpace() + if err != nil { + log.Fatalf("%s", err) + } - headerPrinter := formatter.appHeader - if o.isService { - headerPrinter = formatter.serviceHeader - } - if sourceID == "" { - // not an app or service, use generic header - headerPrinter = formatter.sourceHeader - } + headerPrinter := formatter.appHeader + if o.isService { + headerPrinter = formatter.serviceHeader + } + if sourceID == "" { + // not an app or service, use generic header + headerPrinter = formatter.sourceHeader + } - if !o.noHeaders { - header, ok := headerPrinter(o.providedName, org.Name, space.Name, user) - if ok { - lw.Write(header) - lw.Write("") - } + if !o.noHeaders { + header, ok := headerPrinter(o.providedName, org.Name, space.Name, user) + if ok { + lw.Write(header) + lw.Write("") } } @@ -121,23 +103,6 @@ func Tail( return formatter.formatEnvelope(e) } - tokenClient := &tokenHTTPClient{ - c: c, - tokenFunc: func() string { return "" }, - } - - if strings.ToLower(os.Getenv("LOG_CACHE_SKIP_AUTH")) != "true" { - tokenClient.tokenFunc = func() string { - token, err := cli.AccessToken() - if err != nil { - log.Fatalf("Unable to get Access Token: %s", err) - } - return token - } - } - - client := logcache.NewClient(logCacheAddr, logcache.WithHTTPClient(tokenClient)) - checkFeatureVersioning(client, ctx, log, o.nameFilter) if sourceID == "" { diff --git a/internal/command/tail_test.go b/internal/command/tail_test.go index 6b4829bb..03cc786b 100644 --- a/internal/command/tail_test.go +++ b/internal/command/tail_test.go @@ -1408,42 +1408,7 @@ var _ = Describe("Tail", func() { ) }).To(Panic()) - Expect(logger.fatalfMessage).To(Equal("some-error")) - }) - - It("fatally logs if there is no API endpoint", func() { - cliConn.hasAPIEndpoint = false - - Expect(func() { - cf.Tail( - context.Background(), - cliConn, - []string{"app-name"}, - httpClient, - logger, - writer, - ) - }).To(Panic()) - - Expect(logger.fatalfMessage).To(Equal("No API endpoint targeted.")) - }) - - It("fatally logs if there is an error while checking for API endpoint", func() { - cliConn.hasAPIEndpoint = true - cliConn.hasAPIEndpointErr = errors.New("some-error") - - Expect(func() { - cf.Tail( - context.Background(), - cliConn, - []string{"app-name"}, - httpClient, - logger, - writer, - ) - }).To(Panic()) - - Expect(logger.fatalfMessage).To(Equal("some-error")) + Expect(logger.fatalfMessage).To(Equal("Could not determine Log Cache endpoint: some-error")) }) It("fatally logs if the request returns an error", func() {