Skip to content

Commit 5dad115

Browse files
ctlongrroberts2222
authored andcommitted
Refactor the project structure
Moves most of the plugin code from main.go to it's own internal/logcache package to increase clarity. This is a pure refactor that should not affect functionality.
1 parent a3f3c80 commit 5dad115

File tree

3 files changed

+159
-129
lines changed

3 files changed

+159
-129
lines changed

internal/logcache/plugin.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package logcache
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"log"
7+
"net/http"
8+
"os"
9+
10+
"code.cloudfoundry.org/cli/plugin"
11+
"code.cloudfoundry.org/log-cache-cli/v4/internal/command"
12+
"golang.org/x/term"
13+
)
14+
15+
type LogCache struct {
16+
version plugin.VersionType
17+
}
18+
19+
func New(version plugin.VersionType) *LogCache {
20+
return &LogCache{version: version}
21+
}
22+
23+
func (lc *LogCache) Run(conn plugin.CliConnection, args []string) {
24+
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
25+
26+
skipSSL, err := conn.IsSSLDisabled()
27+
if err != nil {
28+
log.Fatal(err)
29+
}
30+
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
31+
InsecureSkipVerify: skipSSL, //nolint:gosec
32+
}
33+
34+
l := log.New(os.Stderr, "", 0)
35+
36+
switch args[0] {
37+
case "query":
38+
var opts []command.QueryOption
39+
command.Query(conn, args[1:], http.DefaultClient, l, os.Stdout, opts...)
40+
case "tail":
41+
var opts []command.TailOption
42+
if !isTerminal {
43+
opts = append(opts, command.WithTailNoHeaders())
44+
}
45+
command.Tail(context.Background(), conn, args[1:], http.DefaultClient, l, os.Stdout, opts...)
46+
case "log-meta":
47+
var opts []command.MetaOption
48+
if !isTerminal {
49+
opts = append(opts, command.WithMetaNoHeaders())
50+
}
51+
command.Meta(conn, args[1:], http.DefaultClient, l, os.Stdout, opts...)
52+
}
53+
}
54+
55+
func (lc *LogCache) GetMetadata() plugin.PluginMetadata {
56+
return plugin.PluginMetadata{
57+
Name: "log-cache",
58+
Version: lc.version,
59+
Commands: []plugin.Command{
60+
{
61+
Name: "tail",
62+
HelpText: "Output logs for a source-id/app",
63+
UsageDetails: plugin.Usage{
64+
Usage: `tail [options] <source-id/app>
65+
66+
ENVIRONMENT VARIABLES:
67+
LOG_CACHE_ADDR Overrides the default location of log-cache.
68+
LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`,
69+
Options: map[string]string{
70+
"-start-time": "Start of query range in UNIX nanoseconds.",
71+
"-end-time": "End of query range in UNIX nanoseconds.",
72+
"-envelope-type, -t": "Envelope type filter. Available filters: 'log', 'counter', 'gauge', 'timer', 'event', and 'any'.",
73+
"-envelope-class, -c": "Envelope class filter. Available filters: 'logs', 'metrics', and 'any'.",
74+
"-follow, -f": "Output appended to stdout as logs are egressed.",
75+
"-json": "Output envelopes in JSON format.",
76+
"-lines, -n": "Number of envelopes to return. Default is 10.",
77+
"-new-line": "Character used for new line substition, must be single unicode character. Default is '\\n'.",
78+
"-name-filter": "Filters metrics by name.",
79+
},
80+
},
81+
},
82+
{
83+
Name: "log-meta",
84+
HelpText: "Show all available meta information",
85+
UsageDetails: plugin.Usage{
86+
Usage: `log-meta [options]
87+
88+
ENVIRONMENT VARIABLES:
89+
LOG_CACHE_ADDR Overrides the default location of log-cache.
90+
LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`,
91+
Options: map[string]string{
92+
"-source-type": "Source type of information to show. Available: 'all', 'application', 'service', 'platform', and 'unknown'. Excludes unknown sources unless 'all' or 'unknown' is selected, or `--guid` is used. To receive information on platform or unknown source id's, you must have the doppler.firehose, or logs.admin scope.",
93+
"-sort-by": "Sort by specified column. Available: 'source-id', 'source', 'source-type', 'count', 'expired', 'cache-duration', and 'rate'.",
94+
"-noise": "Fetch and display the rate of envelopes per minute for the last minute. WARNING: This is slow...",
95+
"-guid": "Display raw source GUIDs with no source Names. Incompatible with 'source' and 'source-type' for --sort-by. Only allows 'platform' for --source-type",
96+
},
97+
},
98+
},
99+
{
100+
Name: "query",
101+
HelpText: "Issues a PromQL query against Log Cache",
102+
UsageDetails: plugin.Usage{
103+
Usage: `query <promql-query> [options]
104+
105+
ENVIRONMENT VARIABLES:
106+
LOG_CACHE_ADDR Overrides the default location of log-cache.
107+
LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`,
108+
Options: map[string]string{
109+
"-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.",
110+
"-start": "Start time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339.",
111+
"-end": "End time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339.",
112+
"-step": "Step interval for a range query. Cannot be used with --time.",
113+
},
114+
},
115+
},
116+
},
117+
}
118+
}

main.go

Lines changed: 4 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,14 @@
11
package main
22

33
import (
4-
"context"
5-
"crypto/tls"
6-
"log"
7-
"net/http"
8-
"os"
9-
"strconv"
10-
"strings"
11-
124
"code.cloudfoundry.org/cli/plugin"
13-
"code.cloudfoundry.org/log-cache-cli/v4/internal/command"
14-
"golang.org/x/term"
5+
"code.cloudfoundry.org/log-cache-cli/v4/internal/logcache"
156
)
167

17-
// semver version is set via ldflags at compile time
8+
// version is expected to be set via ldflags at compile time to a
9+
// `MAJOR.MINOR.BUILD` version string, e.g. `"1.2.3"`, `"4.0.0`.
1810
var version string
1911

20-
type LogCacheCLI struct{}
21-
22-
func (c *LogCacheCLI) Run(conn plugin.CliConnection, args []string) {
23-
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
24-
25-
skipSSL, err := conn.IsSSLDisabled()
26-
if err != nil {
27-
log.Fatal(err)
28-
}
29-
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
30-
InsecureSkipVerify: skipSSL, //nolint:gosec
31-
}
32-
33-
l := log.New(os.Stderr, "", 0)
34-
35-
switch args[0] {
36-
case "query":
37-
var opts []command.QueryOption
38-
command.Query(conn, args[1:], http.DefaultClient, l, os.Stdout, opts...)
39-
case "tail":
40-
var opts []command.TailOption
41-
if !isTerminal {
42-
opts = append(opts, command.WithTailNoHeaders())
43-
}
44-
command.Tail(context.Background(), conn, args[1:], http.DefaultClient, l, os.Stdout, opts...)
45-
case "log-meta":
46-
var opts []command.MetaOption
47-
if !isTerminal {
48-
opts = append(opts, command.WithMetaNoHeaders())
49-
}
50-
command.Meta(conn, args[1:], http.DefaultClient, l, os.Stdout, opts...)
51-
}
52-
}
53-
54-
func (c *LogCacheCLI) GetMetadata() plugin.PluginMetadata {
55-
// ignore any errors and use the default plugin.VersionType if version
56-
// cannot be parsed
57-
split := strings.Split(version, ".")
58-
v := plugin.VersionType{}
59-
if len(split) == 3 {
60-
v.Major = readOrZero(split[0])
61-
v.Minor = readOrZero(split[1])
62-
v.Build = readOrZero(split[2])
63-
}
64-
65-
return plugin.PluginMetadata{
66-
Name: "log-cache",
67-
Version: v,
68-
Commands: []plugin.Command{
69-
{
70-
Name: "tail",
71-
HelpText: "Output logs for a source-id/app",
72-
UsageDetails: plugin.Usage{
73-
Usage: `tail [options] <source-id/app>
74-
75-
ENVIRONMENT VARIABLES:
76-
LOG_CACHE_ADDR Overrides the default location of log-cache.
77-
LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`,
78-
Options: map[string]string{
79-
"-start-time": "Start of query range in UNIX nanoseconds.",
80-
"-end-time": "End of query range in UNIX nanoseconds.",
81-
"-envelope-type, -t": "Envelope type filter. Available filters: 'log', 'counter', 'gauge', 'timer', 'event', and 'any'.",
82-
"-envelope-class, -c": "Envelope class filter. Available filters: 'logs', 'metrics', and 'any'.",
83-
"-follow, -f": "Output appended to stdout as logs are egressed.",
84-
"-json": "Output envelopes in JSON format.",
85-
"-lines, -n": "Number of envelopes to return. Default is 10.",
86-
"-new-line": "Character used for new line substition, must be single unicode character. Default is '\\n'.",
87-
"-name-filter": "Filters metrics by name.",
88-
},
89-
},
90-
},
91-
{
92-
Name: "log-meta",
93-
HelpText: "Show all available meta information",
94-
UsageDetails: plugin.Usage{
95-
Usage: `log-meta [options]
96-
97-
ENVIRONMENT VARIABLES:
98-
LOG_CACHE_ADDR Overrides the default location of log-cache.
99-
LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`,
100-
Options: map[string]string{
101-
"-source-type": "Source type of information to show. Available: 'all', 'application', 'service', 'platform', and 'unknown'. Excludes unknown sources unless 'all' or 'unknown' is selected, or `--guid` is used. To receive information on platform or unknown source id's, you must have the doppler.firehose, or logs.admin scope.",
102-
"-sort-by": "Sort by specified column. Available: 'source-id', 'source', 'source-type', 'count', 'expired', 'cache-duration', and 'rate'.",
103-
"-noise": "Fetch and display the rate of envelopes per minute for the last minute. WARNING: This is slow...",
104-
"-guid": "Display raw source GUIDs with no source Names. Incompatible with 'source' and 'source-type' for --sort-by. Only allows 'platform' for --source-type",
105-
},
106-
},
107-
},
108-
{
109-
Name: "query",
110-
HelpText: "Issues a PromQL query against Log Cache",
111-
UsageDetails: plugin.Usage{
112-
Usage: `query <promql-query> [options]
113-
114-
ENVIRONMENT VARIABLES:
115-
LOG_CACHE_ADDR Overrides the default location of log-cache.
116-
LOG_CACHE_SKIP_AUTH Set to 'true' to disable CF authentication.`,
117-
Options: map[string]string{
118-
"-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.",
119-
"-start": "Start time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339.",
120-
"-end": "End time for a range query. Cannont be used with --time. Can be a unix timestamp or RFC3339.",
121-
"-step": "Step interval for a range query. Cannot be used with --time.",
122-
},
123-
},
124-
},
125-
},
126-
}
127-
}
128-
129-
func readOrZero(s string) int {
130-
n, err := strconv.Atoi(s)
131-
if err != nil {
132-
return 0
133-
}
134-
return n
135-
}
136-
13712
func main() {
138-
plugin.Start(&LogCacheCLI{})
13+
plugin.Start(logcache.New(versionType()))
13914
}

version.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
7+
"code.cloudfoundry.org/cli/plugin"
8+
)
9+
10+
// versionType parses version into a plugin.VersionType. If version cannot be
11+
// parsed correctly, then the default plugin.VersionType is returned, which
12+
// results in the plugin being marked as "dev".
13+
func versionType() plugin.VersionType {
14+
s := strings.Split(version, ".")
15+
if len(s) != 3 {
16+
return plugin.VersionType{}
17+
}
18+
19+
var (
20+
err error
21+
vt plugin.VersionType
22+
)
23+
vt.Major, err = strconv.Atoi(s[0])
24+
if err != nil {
25+
return plugin.VersionType{}
26+
}
27+
vt.Minor, err = strconv.Atoi(s[1])
28+
if err != nil {
29+
return plugin.VersionType{}
30+
}
31+
vt.Build, err = strconv.Atoi(s[2])
32+
if err != nil {
33+
return plugin.VersionType{}
34+
}
35+
36+
return vt
37+
}

0 commit comments

Comments
 (0)