|
| 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 | +} |
0 commit comments