From 8bfbd2c3280199af22fb19a4a30574d466dc045e Mon Sep 17 00:00:00 2001 From: Carson Long Date: Wed, 6 Jul 2022 20:45:06 -0600 Subject: [PATCH 1/5] Move http things to their own shared package --- internal/command/http.go | 23 ----------------------- internal/command/meta.go | 23 +++++++++++------------ internal/command/query.go | 21 ++++++++++----------- internal/command/tail.go | 15 ++++++--------- internal/util/http/http.go | 30 ++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 55 deletions(-) delete mode 100644 internal/command/http.go create mode 100644 internal/util/http/http.go diff --git a/internal/command/http.go b/internal/command/http.go deleted file mode 100644 index e22dea4c..00000000 --- a/internal/command/http.go +++ /dev/null @@ -1,23 +0,0 @@ -package command - -import "net/http" - -// HTTPClient is the client used for HTTP requests -type HTTPClient interface { - Do(req *http.Request) (*http.Response, error) -} - -// tokenHTTPClient is an interface representing an authenticated HTTP client -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/meta.go b/internal/command/meta.go index cbc8a589..ffb1a206 100644 --- a/internal/command/meta.go +++ b/internal/command/meta.go @@ -12,6 +12,8 @@ import ( "text/tabwriter" "time" + "code.cloudfoundry.org/log-cache-cli/v4/internal/util/http" + "code.cloudfoundry.org/cli/plugin" logcache "code.cloudfoundry.org/go-log-cache" logcache_v1 "code.cloudfoundry.org/go-log-cache/rpc/logcache_v1" @@ -96,7 +98,7 @@ func Meta( ctx context.Context, cli plugin.CliConnection, args []string, - c HTTPClient, + c http.Client, log Logger, tableWriter io.Writer, mopts ...MetaOption, @@ -214,23 +216,20 @@ type displayRow struct { Delta int64 } -func createLogCacheClient(c HTTPClient, log Logger, cli plugin.CliConnection) *logcache.Client { +func createLogCacheClient(c http.Client, 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 - }, - } + c = http.NewTokenClient(c, func() string { + token, err := cli.AccessToken() + if err != nil { + log.Fatalf("Unable to get Access Token: %s", err) + } + return token + }) } return logcache.NewClient( diff --git a/internal/command/query.go b/internal/command/query.go index bf75010c..9fde992d 100644 --- a/internal/command/query.go +++ b/internal/command/query.go @@ -11,6 +11,8 @@ import ( "strings" "time" + "code.cloudfoundry.org/log-cache-cli/v4/internal/util/http" + "code.cloudfoundry.org/cli/plugin" logcache "code.cloudfoundry.org/go-log-cache" flags "github.com/jessevdk/go-flags" @@ -22,7 +24,7 @@ func Query( ctx context.Context, cli plugin.CliConnection, args []string, - c HTTPClient, + c http.Client, log Logger, w io.Writer, opts ...QueryOption, @@ -44,16 +46,13 @@ 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 - }, - } + c = http.NewTokenClient(c, 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") diff --git a/internal/command/tail.go b/internal/command/tail.go index 67ceeaa4..81015ac6 100644 --- a/internal/command/tail.go +++ b/internal/command/tail.go @@ -12,6 +12,8 @@ import ( "time" "unicode/utf8" + "code.cloudfoundry.org/log-cache-cli/v4/internal/util/http" + "code.cloudfoundry.org/cli/plugin" logcache "code.cloudfoundry.org/go-log-cache" logcache_v1 "code.cloudfoundry.org/go-log-cache/rpc/logcache_v1" @@ -34,7 +36,7 @@ func Tail( ctx context.Context, cli plugin.CliConnection, args []string, - c HTTPClient, + c http.Client, log Logger, w io.Writer, opts ...TailOption, @@ -115,22 +117,17 @@ 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 { + c = http.NewTokenClient(c, 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)) + client := logcache.NewClient(logCacheAddr, logcache.WithHTTPClient(c)) checkFeatureVersioning(client, ctx, log, o.nameFilter) diff --git a/internal/util/http/http.go b/internal/util/http/http.go new file mode 100644 index 00000000..2f0e8fb5 --- /dev/null +++ b/internal/util/http/http.go @@ -0,0 +1,30 @@ +package http + +import "net/http" + +// Client is the client used for HTTP requests +type Client interface { + Do(req *http.Request) (*http.Response, error) +} + +// TokenClient is an interface representing an authenticated HTTP client +type TokenClient struct { + c Client + tokenFunc func() string +} + +func NewTokenClient(c Client, tf func() string) *TokenClient { + return &TokenClient{ + c: c, + tokenFunc: tf, + } +} + +func (c *TokenClient) 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 073cbeb7fa8a85f9bf5e02c040329dc241a2e2c8 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Thu, 7 Jul 2022 10:42:51 -0600 Subject: [PATCH 2/5] Add Go Doc comments to the http package --- internal/util/http/http.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/util/http/http.go b/internal/util/http/http.go index 2f0e8fb5..c20a7f5e 100644 --- a/internal/util/http/http.go +++ b/internal/util/http/http.go @@ -1,18 +1,23 @@ +// Package http provides HTTP client implementations. package http import "net/http" -// Client is the client used for HTTP requests +// A Client is implemented by the standard library's http.Client and +// TokenClient. type Client interface { Do(req *http.Request) (*http.Response, error) } -// TokenClient is an interface representing an authenticated HTTP client +// A TokenClient wraps an HTTP client to automatically set Authorization headers +// in requests using the provided function to generate tokens. type TokenClient struct { c Client tokenFunc func() string } +// NewTokenClient returns a TokenClient given a client and token-generating +// funtion. func NewTokenClient(c Client, tf func() string) *TokenClient { return &TokenClient{ c: c, @@ -20,6 +25,9 @@ func NewTokenClient(c Client, tf func() string) *TokenClient { } } +// Do makes an HTTP request using the underlying client. If the token function +// returns a non-empty string then it will be set as the Authorization header of +// the request. func (c *TokenClient) Do(req *http.Request) (*http.Response, error) { accessToken := c.tokenFunc() if len(accessToken) > 0 { From 7e086705629fc67b6739bddf9caec66d66e3f582 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Thu, 7 Jul 2022 11:23:45 -0600 Subject: [PATCH 3/5] Added testing for http.TokenClient --- .github/workflows/go.yml | 2 +- internal/util/http/http_test.go | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 internal/util/http/http_test.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9cc6063e..976b2f8e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -14,7 +14,7 @@ jobs: - uses: actions/setup-go@v3 with: go-version: 1.18 - - run: go run github.com/onsi/ginkgo/v2/ginkgo -p -r -race --randomize-all + - run: go test -race ./... vet: runs-on: ubuntu-latest steps: diff --git a/internal/util/http/http_test.go b/internal/util/http/http_test.go new file mode 100644 index 00000000..1f7fdf01 --- /dev/null +++ b/internal/util/http/http_test.go @@ -0,0 +1,41 @@ +package http + +import ( + "net/http" + "testing" +) + +func TestTokenClient(t *testing.T) { + mc := &mockClient{} + tc := NewTokenClient(mc, func() string { + return "test" + }) + + r, err := http.NewRequest("GET", "fakeurl", nil) + if err != nil { + t.Fatal(err) + } + + resp, err := tc.Do(r) + if err != nil { + t.Fatal(err) + } + + if resp.StatusCode != 200 { + t.Errorf("got %d, want %d", resp.StatusCode, 200) + } + + auth := mc.lastReq.Header.Get("Authorization") + if auth != "test" { + t.Errorf("got %s, want %s", auth, "test") + } +} + +type mockClient struct { + lastReq http.Request +} + +func (c *mockClient) Do(req *http.Request) (*http.Response, error) { + c.lastReq = *req + return &http.Response{StatusCode: 200}, nil +} From 9f9ed1c3c1e062e1b9d01fa2a35bd41bc4e38369 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Thu, 7 Jul 2022 11:27:06 -0600 Subject: [PATCH 4/5] tools.go should have package tools by convention The build tags will prevent it from being included anyway. --- tools.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools.go b/tools.go index e4195cf3..3bac4c0b 100644 --- a/tools.go +++ b/tools.go @@ -1,7 +1,7 @@ //go:build tools // +build tools -package main +package tools import ( _ "github.com/onsi/ginkgo/v2/ginkgo" From a37cbb62f3f1d4b654aa7bfa01608874657bc897 Mon Sep 17 00:00:00 2001 From: Carson Long Date: Thu, 7 Jul 2022 14:48:17 -0600 Subject: [PATCH 5/5] Move certain structs into a new platform package --- internal/command/command.go | 14 --- internal/command/doc.go | 3 + internal/command/meta.go | 146 ++++++++++----------------- internal/command/tail.go | 19 ++-- internal/util/platform/service.go | 14 +++ internal/util/platform/sortby.go | 17 ++++ internal/util/platform/source.go | 19 ++++ internal/util/platform/sourcetype.go | 16 +++ 8 files changed, 130 insertions(+), 118 deletions(-) delete mode 100644 internal/command/command.go create mode 100644 internal/command/doc.go create mode 100644 internal/util/platform/service.go create mode 100644 internal/util/platform/sortby.go create mode 100644 internal/util/platform/source.go create mode 100644 internal/util/platform/sourcetype.go diff --git a/internal/command/command.go b/internal/command/command.go deleted file mode 100644 index daf79bb3..00000000 --- a/internal/command/command.go +++ /dev/null @@ -1,14 +0,0 @@ -// Package command implements various cf CLI plugin commands for communicating -// with Log Cache. -package command - -type sourceType string - -const ( - _application sourceType = "application" - _service sourceType = "service" - _platform sourceType = "platform" - _all sourceType = "all" - _default sourceType = "default" - _unknown sourceType = "unknown" -) diff --git a/internal/command/doc.go b/internal/command/doc.go new file mode 100644 index 00000000..8d03c1e7 --- /dev/null +++ b/internal/command/doc.go @@ -0,0 +1,3 @@ +// Package command implements various cf CLI plugin commands for communicating +// with Log Cache. +package command diff --git a/internal/command/meta.go b/internal/command/meta.go index ffb1a206..d20d9283 100644 --- a/internal/command/meta.go +++ b/internal/command/meta.go @@ -13,6 +13,7 @@ import ( "time" "code.cloudfoundry.org/log-cache-cli/v4/internal/util/http" + "code.cloudfoundry.org/log-cache-cli/v4/internal/util/platform" "code.cloudfoundry.org/cli/plugin" logcache "code.cloudfoundry.org/go-log-cache" @@ -20,51 +21,6 @@ import ( flags "github.com/jessevdk/go-flags" ) -const ( - sortBySourceID sortBy = "source-id" - sortBySource sortBy = "source" - sortBySourceType sortBy = "source-type" - sortByCount sortBy = "count" - sortByExpired sortBy = "expired" - sortByCacheDuration sortBy = "cache-duration" - sortByRate sortBy = "rate" -) - -type sortBy string - -func (st sourceType) Equal(value string) bool { - return string(st) == value -} - -func (sb sortBy) Equal(value string) bool { - return string(sb) == value -} - -type source struct { - GUID string `json:"guid"` - Name string `json:"name"` - Type sourceType -} - -type sourceInfo struct { - Resources []source `json:"resources"` -} - -type serviceInstance struct { - Metadata struct { - GUID string `json:"guid"` - } `json:"metadata"` - Entity struct { - Name string `json:"name"` - } `json:"entity"` -} - -type servicesResponse struct { - Resources []serviceInstance `json:"resources"` -} - -type Tailer func(sourceID string) []string - type optionsFlags struct { SourceType string `long:"source-type"` EnableNoise bool `long:"noise"` @@ -130,7 +86,7 @@ func Meta( } } - resources := make(map[string]source) + resources := make(map[string]platform.Source) if !opts.ShowGUID { writeAppsAndServicesHeader(opts, tw, username) resources, err = getSourceInfo(currentMeta, cli) @@ -155,7 +111,7 @@ func Meta( } } -func toDisplayRows(resources map[string]source, currentMeta, originalMeta map[string]*logcache_v1.MetaInfo) []displayRow { +func toDisplayRows(resources map[string]platform.Source, currentMeta, originalMeta map[string]*logcache_v1.MetaInfo) []displayRow { var rows []displayRow for sourceID, m := range currentMeta { dR := displayRow{Source: sourceID, SourceID: sourceID, Count: m.Count, Expired: m.Expired, CacheDuration: cacheDuration(m)} @@ -164,9 +120,9 @@ func toDisplayRows(resources map[string]source, currentMeta, originalMeta map[st dR.Type = source.Type dR.Source = source.Name } else if appOrServiceRegex.MatchString(sourceID) { - dR.Type = _unknown + dR.Type = platform.UnknownType } else { - dR.Type = _platform + dR.Type = platform.PlatformType } if originalMeta[sourceID] != nil { diff := (m.Count + m.Expired) - (originalMeta[sourceID].Count + originalMeta[sourceID].Expired) @@ -181,21 +137,21 @@ func toDisplayRows(resources map[string]source, currentMeta, originalMeta map[st } func filterRows(opts optionsFlags, rows []displayRow) []displayRow { - if _all.Equal(opts.SourceType) { + if platform.AllType.Equal(opts.SourceType) { return rows } filteredRows := []displayRow{} for _, row := range rows { - if row.Type == _application && (_application.Equal(opts.SourceType) || _default.Equal(opts.SourceType)) { + if row.Type == platform.ApplicationType && (platform.ApplicationType.Equal(opts.SourceType) || platform.DefaultType.Equal(opts.SourceType)) { filteredRows = append(filteredRows, row) } - if row.Type == _platform && (_platform.Equal(opts.SourceType) || _default.Equal(opts.SourceType)) { + if row.Type == platform.PlatformType && (platform.PlatformType.Equal(opts.SourceType) || platform.DefaultType.Equal(opts.SourceType)) { filteredRows = append(filteredRows, row) } - if row.Type == _service && (_service.Equal(opts.SourceType) || _default.Equal(opts.SourceType)) { + if row.Type == platform.ServiceType && (platform.ServiceType.Equal(opts.SourceType) || platform.DefaultType.Equal(opts.SourceType)) { filteredRows = append(filteredRows, row) } - if row.Type == _unknown && (_unknown.Equal(opts.SourceType) || shouldShowUknownWithGuidFlag(opts)) { + if row.Type == platform.UnknownType && (platform.UnknownType.Equal(opts.SourceType) || shouldShowUknownWithGuidFlag(opts)) { filteredRows = append(filteredRows, row) } } @@ -203,13 +159,13 @@ func filterRows(opts optionsFlags, rows []displayRow) []displayRow { } func shouldShowUknownWithGuidFlag(opts optionsFlags) bool { - return opts.ShowGUID && !_platform.Equal(opts.SourceType) + return opts.ShowGUID && !platform.PlatformType.Equal(opts.SourceType) } type displayRow struct { Source string SourceID string - Type sourceType + Type platform.SourceType Count int64 Expired int64 CacheDuration time.Duration @@ -332,19 +288,19 @@ func getOptions(args []string, log Logger, mopts ...MetaOption) optionsFlags { opts.SourceType = strings.ToLower(opts.SourceType) opts.SortBy = strings.ToLower(opts.SortBy) - if opts.ShowGUID && (sortBySource.Equal(opts.SortBy) || sortBySourceType.Equal(opts.SortBy)) { + if opts.ShowGUID && (platform.SortBySource.Equal(opts.SortBy) || platform.SortBySourceType.Equal(opts.SortBy)) { log.Fatalf("When using --guid, sort by must be 'source-id', 'count', 'expired', 'cache-duration', or 'rate'.") } // validate what was entered before setting defaults if opts.SortBy == "" { - opts.SortBy = string(sortBySource) + opts.SortBy = string(platform.SortBySource) if opts.ShowGUID { - opts.SortBy = string(sortBySourceID) + opts.SortBy = string(platform.SortBySourceID) } } - if opts.ShowGUID && !(_platform.Equal(opts.SourceType) || _all.Equal(opts.SourceType) || _default.Equal(opts.SourceType)) { + if opts.ShowGUID && !(platform.PlatformType.Equal(opts.SourceType) || platform.AllType.Equal(opts.SourceType) || platform.DefaultType.Equal(opts.SourceType)) { log.Fatalf("Source type must be 'platform' when using the --guid flag") } @@ -356,7 +312,7 @@ func getOptions(args []string, log Logger, mopts ...MetaOption) optionsFlags { log.Fatalf("Sort by must be 'source-id', 'source', 'source-type', 'count', 'expired', 'cache-duration', or 'rate'.") } - if sortByRate.Equal(opts.SortBy) && !opts.EnableNoise { + if platform.SortByRate.Equal(opts.SortBy) && !opts.EnableNoise { log.Fatalf("Can't sort by rate column without --noise flag") } @@ -365,55 +321,55 @@ func getOptions(args []string, log Logger, mopts ...MetaOption) optionsFlags { func sortRows(opts optionsFlags, rows []displayRow) { switch opts.SortBy { - case string(sortBySourceID): + case string(platform.SortBySourceID): sort.Slice(rows, func(i, j int) bool { - if rows[i].Type == _unknown && rows[j].Type != _unknown { + if rows[i].Type == platform.UnknownType && rows[j].Type != platform.UnknownType { return false } - if rows[j].Type == _unknown && rows[i].Type != _unknown { + if rows[j].Type == platform.UnknownType && rows[i].Type != platform.UnknownType { return true } return rows[i].SourceID < rows[j].SourceID }) - case string(sortBySource): + case string(platform.SortBySource): sort.Slice(rows, func(i, j int) bool { - if rows[i].Type == _unknown && rows[j].Type != _unknown { + if rows[i].Type == platform.UnknownType && rows[j].Type != platform.UnknownType { return false } - if rows[j].Type == _unknown && rows[i].Type != _unknown { + if rows[j].Type == platform.UnknownType && rows[i].Type != platform.UnknownType { return true } return rows[i].Source < rows[j].Source }) - case string(sortBySourceType): + case string(platform.SortBySourceType): sort.Slice(rows, func(i, j int) bool { return rows[i].Type < rows[j].Type }) - case string(sortByCount): + case string(platform.SortByCount): sort.Slice(rows, func(i, j int) bool { return rows[i].Count < rows[j].Count }) - case string(sortByExpired): + case string(platform.SortByExpired): sort.Slice(rows, func(i, j int) bool { return rows[i].Expired < rows[j].Expired }) - case string(sortByCacheDuration): + case string(platform.SortByCacheDuration): sort.Slice(rows, func(i, j int) bool { return rows[i].CacheDuration < rows[j].CacheDuration }) - case string(sortByRate): + case string(platform.SortByRate): sort.Slice(rows, func(i, j int) bool { return rows[i].Delta < rows[j].Delta }) } } -func getSourceInfo(metaInfo map[string]*logcache_v1.MetaInfo, cli plugin.CliConnection) (map[string]source, error) { +func getSourceInfo(metaInfo map[string]*logcache_v1.MetaInfo, cli plugin.CliConnection) (map[string]platform.Source, error) { var ( - resources map[string]source + resources map[string]platform.Source sourceIDs []string ) - resources = make(map[string]source) + resources = make(map[string]platform.Source) meta := make(map[string]int) for k := range metaInfo { @@ -426,14 +382,14 @@ func getSourceInfo(metaInfo map[string]*logcache_v1.MetaInfo, cli plugin.CliConn return nil, err } for _, rb := range appInfo { - var r sourceInfo + var r platform.SourceInfo err := json.NewDecoder(strings.NewReader(rb)).Decode(&r) if err != nil { return nil, err } for _, res := range r.Resources { - res.Type = _application + res.Type = platform.ApplicationType resources[res.GUID] = res } } @@ -452,16 +408,16 @@ func getSourceInfo(metaInfo map[string]*logcache_v1.MetaInfo, cli plugin.CliConn } for _, rb := range serviceInfo { - var r servicesResponse + var r platform.ServicesResponse err := json.NewDecoder(strings.NewReader(rb)).Decode(&r) if err != nil { return nil, err } for _, res := range r.Resources { - resources[res.Metadata.GUID] = source{ + resources[res.Metadata.GUID] = platform.Source{ GUID: res.Metadata.GUID, Name: res.Entity.Name, - Type: _service, + Type: platform.ServiceType, } } } @@ -522,13 +478,13 @@ func logCacheEndpoint(cli plugin.CliConnection) (string, error) { } func invalidSourceType(st string) bool { - validSourceTypes := []sourceType{ - _platform, - _application, - _service, - _unknown, - _default, - _all, + validSourceTypes := []platform.SourceType{ + platform.PlatformType, + platform.ApplicationType, + platform.ServiceType, + platform.UnknownType, + platform.DefaultType, + platform.AllType, } if st == "" { @@ -545,14 +501,14 @@ func invalidSourceType(st string) bool { } func invalidSortBy(sb string) bool { - validSortBy := []sortBy{ - sortBySourceID, - sortBySource, - sortBySourceType, - sortByCount, - sortByExpired, - sortByCacheDuration, - sortByRate, + validSortBy := []platform.SortBy{ + platform.SortBySourceID, + platform.SortBySource, + platform.SortBySourceType, + platform.SortByCount, + platform.SortByExpired, + platform.SortByCacheDuration, + platform.SortByRate, } if sb == "" { diff --git a/internal/command/tail.go b/internal/command/tail.go index 81015ac6..fbc3bb71 100644 --- a/internal/command/tail.go +++ b/internal/command/tail.go @@ -13,6 +13,7 @@ import ( "unicode/utf8" "code.cloudfoundry.org/log-cache-cli/v4/internal/util/http" + "code.cloudfoundry.org/log-cache-cli/v4/internal/util/platform" "code.cloudfoundry.org/cli/plugin" logcache "code.cloudfoundry.org/go-log-cache" @@ -94,9 +95,9 @@ func Tail( headerPrinter := formatter.sourceHeader switch o.source.Type { - case _application: + case platform.ApplicationType: headerPrinter = formatter.appHeader - case _service: + case platform.ServiceType: headerPrinter = formatter.serviceHeader } @@ -132,7 +133,7 @@ func Tail( checkFeatureVersioning(client, ctx, log, o.nameFilter) sourceID := o.source.GUID - if o.source.Type == _unknown { + if o.source.Type == platform.UnknownType { // fall back to provided name sourceID = o.source.Name } @@ -209,7 +210,7 @@ type tailOptions struct { lines int follow bool - source source + source platform.Source outputTemplate *template.Template jsonOutput bool tokenRefreshInterval time.Duration @@ -267,7 +268,7 @@ func newTailOptions(cli plugin.CliConnection, args []string, log Logger) (tailOp } } - source := source{Name: args[0]} + source := platform.Source{Name: args[0]} populateSource(&source, cli, log) @@ -386,18 +387,18 @@ func translateEnvelopeType(t string, log Logger) logcache_v1.EnvelopeType { } } -func populateSource(s *source, cli plugin.CliConnection, log Logger) { +func populateSource(s *platform.Source, cli plugin.CliConnection, log Logger) { if guid := getAppGUID(s.Name, cli, log); guid != "" { s.GUID = guid - s.Type = _application + s.Type = platform.ApplicationType return } if guid := getServiceGUID(s.Name, cli, log); guid != "" { s.GUID = guid - s.Type = _service + s.Type = platform.ServiceType return } - s.Type = _unknown + s.Type = platform.UnknownType } func getAppGUID(appName string, cli plugin.CliConnection, log Logger) string { diff --git a/internal/util/platform/service.go b/internal/util/platform/service.go new file mode 100644 index 00000000..b2330970 --- /dev/null +++ b/internal/util/platform/service.go @@ -0,0 +1,14 @@ +package platform + +type ServiceInstance struct { + Metadata struct { + GUID string `json:"guid"` + } `json:"metadata"` + Entity struct { + Name string `json:"name"` + } `json:"entity"` +} + +type ServicesResponse struct { + Resources []ServiceInstance `json:"resources"` +} diff --git a/internal/util/platform/sortby.go b/internal/util/platform/sortby.go new file mode 100644 index 00000000..97777c84 --- /dev/null +++ b/internal/util/platform/sortby.go @@ -0,0 +1,17 @@ +package platform + +type SortBy string + +const ( + SortBySourceID SortBy = "source-id" + SortBySource SortBy = "source" + SortBySourceType SortBy = "source-type" + SortByCount SortBy = "count" + SortByExpired SortBy = "expired" + SortByCacheDuration SortBy = "cache-duration" + SortByRate SortBy = "rate" +) + +func (sb SortBy) Equal(value string) bool { + return string(sb) == value +} diff --git a/internal/util/platform/source.go b/internal/util/platform/source.go new file mode 100644 index 00000000..b7be671b --- /dev/null +++ b/internal/util/platform/source.go @@ -0,0 +1,19 @@ +package platform + +type Source struct { + GUID string `json:"guid"` + Name string `json:"name"` + Type SourceType +} + +func NewSource(GUID, name string, st SourceType) Source { + return Source{ + GUID: GUID, + Name: name, + Type: st, + } +} + +type SourceInfo struct { + Resources []Source `json:"resources"` +} diff --git a/internal/util/platform/sourcetype.go b/internal/util/platform/sourcetype.go new file mode 100644 index 00000000..54348010 --- /dev/null +++ b/internal/util/platform/sourcetype.go @@ -0,0 +1,16 @@ +package platform + +type SourceType string + +func (st SourceType) Equal(value string) bool { + return string(st) == value +} + +const ( + ApplicationType SourceType = "application" + ServiceType SourceType = "service" + PlatformType SourceType = "platform" + AllType SourceType = "all" + DefaultType SourceType = "default" + UnknownType SourceType = "unknown" +)