/
gcp.go
91 lines (80 loc) · 2.16 KB
/
gcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cmd
import (
"os"
"strings"
"github.com/m-mizutani/gots/slice"
"github.com/m-mizutani/spout/pkg/infra"
"github.com/m-mizutani/spout/pkg/infra/gcp"
"github.com/m-mizutani/spout/pkg/model"
"github.com/m-mizutani/spout/pkg/usecase"
"github.com/m-mizutani/spout/pkg/utils"
"github.com/urfave/cli/v2"
)
func cmdGCP(globalCfg *config) *cli.Command {
var localCfg struct {
ProjectID model.GoogleProjectID
Limit int
Filter cli.StringSlice
}
var periodOpt periodOptions
var runOpt runOptions
flags := slice.Flatten(
[]cli.Flag{
&cli.StringFlag{
Name: "project",
Aliases: []string{"p"},
EnvVars: []string{"SPOUT_GCP_PROJECT"},
Usage: "Google Cloud Project ID",
Destination: (*string)(&localCfg.ProjectID),
Required: true,
},
&cli.StringSliceFlag{
Name: "filter",
Aliases: []string{"f"},
EnvVars: []string{"SPOUT_GCP_FILTER"},
Usage: "Google Cloud Logging filter",
Destination: &localCfg.Filter,
},
&cli.IntFlag{
Name: "limit",
EnvVars: []string{"SPOUT_GCP_LIMIT"},
Usage: "Limit of fetching log",
Destination: &localCfg.Limit,
Value: 1000,
},
},
periodOpt.Flags(),
runOpt.Flags(),
)
return &cli.Command{
Name: "gcp",
Aliases: []string{"g"},
Description: "Read Google Cloud logs",
Flags: flags,
Action: func(c *cli.Context) error {
utils.Logger.With("config", localCfg).Debug("fetching cloud logging")
period, err := model.NewPeriod(periodOpt.baseTime, periodOpt.duration, model.RangeType(periodOpt.rangeType))
if err != nil {
return err
}
var options []gcp.Option
filters := localCfg.Filter.Value()
if len(filters) > 0 {
options = append(options, gcp.WithFilter(strings.Join(filters, " ")))
}
reader := gcp.New(localCfg.ProjectID, localCfg.Limit, period, options...)
clients := infra.New(
infra.WithLogReader(reader),
infra.WithWriter(os.Stdout),
)
ctx := model.NewContext(
model.WithCtx(c.Context),
)
uc := usecase.New(clients)
if err := run(ctx, uc, &runOpt); err != nil {
return err
}
return nil
},
}
}