|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "crypto/tls" |
6 | | - "log" |
7 | | - "net/http" |
8 | | - "os" |
9 | | - "strconv" |
10 | | - "strings" |
11 | | - |
12 | 4 | "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" |
15 | 6 | ) |
16 | 7 |
|
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`. |
18 | 10 | var version string |
19 | 11 |
|
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 | | - |
137 | 12 | func main() { |
138 | | - plugin.Start(&LogCacheCLI{}) |
| 13 | + plugin.Start(logcache.New(versionType())) |
139 | 14 | } |
0 commit comments