Skip to content

Commit 79f2bbf

Browse files
committed
alpha: logging support
1 parent da9744e commit 79f2bbf

File tree

11 files changed

+411
-11
lines changed

11 files changed

+411
-11
lines changed

cmd/executor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func NewExecutor() *Executor {
6565
func setupEnvVars(env *cli.Environment) {
6666
env.AddVarWithDefault("plugins_cache_dir", "plugins cache directory", clipath.DataDir("plugin-cache"))
6767
env.AddVar("no_color", "disable color output")
68-
env.AddVarWithDefault("log_level", "set logging level: debug | info | warn | error", "info")
68+
env.AddVarWithDefault("log_level", "set logging level (options: debug, notice, info, warn, error)", "info")
6969
}
7070

7171
func (e *Executor) commandPreRun(ctx context.Context) error {
@@ -311,5 +311,9 @@ func (e *Executor) LogLevel() string {
311311
}
312312

313313
func (e *Executor) NoColor() bool {
314+
if _, ok := os.LookupEnv("NO_COLOR"); ok {
315+
return true
316+
}
317+
314318
return e.v.GetBool("no_color")
315319
}

cmd/logs.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
"time"
6+
7+
"github.com/ansel1/merry/v2"
8+
"github.com/araddon/dateparse"
9+
"github.com/outblocks/outblocks-cli/pkg/actions"
10+
"github.com/outblocks/outblocks-cli/pkg/config"
11+
apiv1 "github.com/outblocks/outblocks-plugin-go/gen/api/v1"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func parseTimeOrDuration(in string) (time.Time, error) {
16+
if in == "" {
17+
return time.Time{}, nil
18+
}
19+
20+
dur, err := time.ParseDuration(in)
21+
if err == nil {
22+
return time.Now().Add(-dur), nil
23+
}
24+
25+
t, err := dateparse.ParseLocal(in)
26+
if err != nil {
27+
return time.Time{}, merry.Errorf("invalid time format")
28+
}
29+
30+
return t, nil
31+
}
32+
33+
func (e *Executor) newLogsCmd() *cobra.Command {
34+
opts := &actions.LogsOptions{}
35+
36+
var (
37+
target []string
38+
severity string
39+
start, end string
40+
)
41+
42+
cmd := &cobra.Command{
43+
Use: "logs",
44+
Short: "Stream logs",
45+
Long: `Filter and stream logs of specific apps and dependencies.`,
46+
Annotations: map[string]string{
47+
cmdGroupAnnotation: cmdGroupMain,
48+
cmdProjectLoadModeAnnotation: cmdLoadModeEssential,
49+
cmdAppsLoadModeAnnotation: cmdLoadModeSkip,
50+
},
51+
SilenceUsage: true,
52+
RunE: func(cmd *cobra.Command, args []string) error {
53+
if e.cfg == nil {
54+
return config.ErrProjectConfigNotFound
55+
}
56+
57+
for _, t := range target {
58+
tsplit := strings.SplitN(t, ".", 2)
59+
if len(tsplit) != 2 {
60+
return merry.Errorf("wrong format for target '%s': specify in a form of <app type>.<name> or dep.<dep name>, e.g.: static.website", t)
61+
}
62+
63+
if tsplit[0] == "dep" {
64+
opts.Dependencies = append(opts.Dependencies, config.ComputeDependencyID(tsplit[1]))
65+
} else {
66+
opts.Apps = append(opts.Apps, config.ComputeAppID(tsplit[0], tsplit[1]))
67+
}
68+
}
69+
70+
var level apiv1.LogSeverity
71+
72+
switch strings.ToLower(severity) {
73+
case "debug":
74+
level = apiv1.LogSeverity_LOG_SEVERITY_DEBUG
75+
case "notice":
76+
level = apiv1.LogSeverity_LOG_SEVERITY_NOTICE
77+
case "info":
78+
level = apiv1.LogSeverity_LOG_SEVERITY_INFO
79+
case "warn":
80+
level = apiv1.LogSeverity_LOG_SEVERITY_WARN
81+
case "error":
82+
level = apiv1.LogSeverity_LOG_SEVERITY_ERROR
83+
case "":
84+
level = apiv1.LogSeverity_LOG_SEVERITY_UNSPECIFIED
85+
default:
86+
return merry.Errorf("unknown log level specified")
87+
}
88+
89+
opts.Severity = level
90+
91+
var err error
92+
opts.Start, err = parseTimeOrDuration(start)
93+
if err != nil {
94+
return merry.Errorf("wrong format for start time '%s': %w", start, err)
95+
}
96+
97+
opts.End, err = parseTimeOrDuration(end)
98+
if err != nil {
99+
return merry.Errorf("wrong format for end time '%s': %w", start, err)
100+
}
101+
102+
if !opts.End.IsZero() && opts.Follow {
103+
return merry.Errorf("while stream logs end time cannot be specified")
104+
}
105+
106+
return actions.NewLogs(e.Log(), e.cfg, opts).Run(cmd.Context())
107+
},
108+
}
109+
110+
f := cmd.Flags()
111+
f.StringSliceVarP(&target, "target", "t", nil, "target only specified apps or dependencies, can specify multiple or separate values with comma in a form of <app type>.<name> or dep.<dep name>, e.g.: static.website,service.api,dep.database")
112+
f.BoolVarP(&opts.OnlyApps, "only-apps", "a", false, "target only apps, skip all dependencies")
113+
f.StringVarP(&start, "start", "s", "5m", "start time")
114+
f.StringVarP(&end, "end", "d", "", "end time")
115+
f.StringVarP(&severity, "severity", "l", "", "minimum severity level (options: debug, notice, info, warn, error)")
116+
f.StringSliceVarP(&opts.Contains, "contains", "c", nil, "filter logs containing specific words")
117+
f.StringSliceVarP(&opts.NotContains, "not-contains", "x", nil, "filter logs not containing specific words")
118+
f.StringVarP(&opts.Filter, "filter", "q", "", "pass raw filter to logs, refer to cloud provider docs for possible options")
119+
f.BoolVarP(&opts.Follow, "follow", "w", false, "stream logs (end has to be unspecified)")
120+
121+
return cmd
122+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ func (e *Executor) newRoot() *cobra.Command {
351351
e.newAppsCmd(),
352352
e.newVersionCmd(),
353353
e.newStatusCmd(),
354+
e.newLogsCmd(),
354355
)
355356

356357
return cmd

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/Masterminds/sprig v2.22.0+incompatible
1010
github.com/Masterminds/vcs v1.13.3
1111
github.com/ansel1/merry/v2 v2.0.1
12+
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
1213
github.com/docker/docker v20.10.14+incompatible
1314
github.com/enescakir/emoji v1.0.0
1415
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
@@ -18,7 +19,7 @@ require (
1819
github.com/mholt/archiver/v3 v3.5.1
1920
github.com/mitchellh/go-homedir v1.1.0
2021
github.com/otiai10/copy v1.7.0
21-
github.com/outblocks/outblocks-plugin-go v0.0.0-20220420190545-2f7c87d49345
22+
github.com/outblocks/outblocks-plugin-go v0.0.0-20220427125140-5c3ad920eb15
2223
github.com/pkg/errors v0.9.1
2324
github.com/pterm/pterm v0.12.41
2425
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ github.com/ansel1/merry/v2 v2.0.1 h1:WeiKZdslHPAPFYxTtgX7clC2Vh75NCoWs5OjCZbIA0A
7575
github.com/ansel1/merry/v2 v2.0.1/go.mod h1:dD5OhpiPrVkvgseRYd+xgYlx7s6ytU3v9BTTJlDA7FM=
7676
github.com/ansel1/vespucci/v4 v4.1.1/go.mod h1:zzdrO4IgBfgcGMbGTk/qNGL8JPslmW3nPpcBHKReFYY=
7777
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
78+
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhPwqqXc4/vE0f7GvRjuAsbW+HOIe8KnA=
79+
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw=
7880
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
7981
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
8082
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
@@ -301,6 +303,7 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
301303
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
302304
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
303305
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
306+
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
304307
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
305308
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
306309
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
@@ -335,8 +338,8 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
335338
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
336339
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
337340
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
338-
github.com/outblocks/outblocks-plugin-go v0.0.0-20220420190545-2f7c87d49345 h1:yqCmSRXvjRl9Xxl99fL9m7Y3y2hXn1KRIq9zUniH9dc=
339-
github.com/outblocks/outblocks-plugin-go v0.0.0-20220420190545-2f7c87d49345/go.mod h1:tcD3iwXc4UZ0H0ad9wvY41ZOqbG2UJ6bbl1Ha+/eyJc=
341+
github.com/outblocks/outblocks-plugin-go v0.0.0-20220427125140-5c3ad920eb15 h1:+QY8R9j4C2cO+8cN/DqNBg312X9baO3VRqWh/C1NUBk=
342+
github.com/outblocks/outblocks-plugin-go v0.0.0-20220427125140-5c3ad920eb15/go.mod h1:tcD3iwXc4UZ0H0ad9wvY41ZOqbG2UJ6bbl1Ha+/eyJc=
340343
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
341344
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
342345
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
@@ -362,6 +365,7 @@ github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5b
362365
github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s=
363366
github.com/pterm/pterm v0.12.41 h1:e2BRfFo1H9nL8GY0S3ImbZqfZ/YimOk9XtkhoobKJVs=
364367
github.com/pterm/pterm v0.12.41/go.mod h1:LW/G4J2A42XlTaPTAGRPvbBfF4UXvHWhC6SN7ueU4jU=
368+
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
365369
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
366370
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
367371
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
@@ -372,6 +376,7 @@ github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XF
372376
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
373377
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
374378
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
379+
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
375380
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
376381
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
377382
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=

0 commit comments

Comments
 (0)