-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudfunction.go
80 lines (67 loc) · 2.68 KB
/
cloudfunction.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
package cmd
import (
"fmt"
"cloud.google.com/go/logging"
"cloud.google.com/go/logging/apiv2/loggingpb"
"github.com/owenrumney/gtail/internal/pkg/logfilter"
"github.com/owenrumney/gtail/internal/pkg/logs"
"github.com/owenrumney/gtail/pkg/output"
"github.com/spf13/cobra"
)
var cloudFunctionCmd = &cobra.Command{
Use: "cloud-function",
Aliases: []string{"cf"},
Short: "Tail logs for a Cloud Function revision",
RunE: func(cmd *cobra.Command, args []string) error {
outputWriter := output.New(outputFormat, severities, defaultCloudFunctionLogWriter)
la := logs.New(projectID, severities, outputWriter)
lf := logfilter.New(projectID, logfilter.CloudRunLogFilterType).
WithFunctionName(functionName).
WithRegion(region)
return la.StreamLogEntries(lf)
},
}
var historicCloudFunctionCmd = &cobra.Command{
Use: "historic",
Short: "Get the cloud function logs for a run that has already exited",
RunE: func(cmd *cobra.Command, args []string) error {
outputWriter := output.New(outputFormat, severities, defaultCloudRunLogWriter)
la := logs.New(projectID, severities, outputWriter)
lf := logfilter.New(projectID, logfilter.CloudFunctionLogFilterType).
WithFunctionName(functionName)
return la.GetHistoricalLogEntries(lf)
},
}
func getCloudFunctionCommand() *cobra.Command {
// add the child commands
cloudFunctionCmd.AddCommand(historicCloudFunctionCmd)
// add the flags
cloudFunctionCmd.PersistentFlags().StringVar(&functionName, "function", "", "Cloud Function name for the logs to get")
cloudFunctionCmd.PersistentFlags().StringVar(&logID, "execution-id", logID, "The Cloud Function execution ID")
cloudFunctionCmd.PersistentFlags().StringSliceVar(&severities, "severity", severities, "The severity of logs to include")
cloudFunctionCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o", outputFormat, "The output format either json or a template string")
historicCloudFunctionCmd.Flags().IntVar(&hoursAgo, "hours-ago", hoursAgo, "Roughly how many hours ago the log happened. Searches a window of time from then till now")
return cloudFunctionCmd
}
func defaultCloudFunctionLogWriter(value interface{}) error {
switch t := value.(type) {
case *logging.Entry:
if content, ok := t.Payload.(string); ok {
fmt.Printf("%v\n", content)
}
case *loggingpb.LogEntry:
var content string
switch payload := t.Payload.(type) {
case *loggingpb.LogEntry_TextPayload:
content = payload.TextPayload
case *loggingpb.LogEntry_ProtoPayload:
content = fmt.Sprintf("%v", payload.ProtoPayload)
case *loggingpb.LogEntry_JsonPayload:
content = fmt.Sprintf("%v", payload.JsonPayload)
}
fmt.Printf("%v\n", content)
default:
fmt.Printf("%v\n", value)
}
return nil
}