-
Notifications
You must be signed in to change notification settings - Fork 19
/
time_filters.go
141 lines (109 loc) · 3.41 KB
/
time_filters.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package flags
import (
"time"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/storage/sos/object"
)
const (
OlderThan = "older-than"
OlderThanTimestamp = "older-than-timestamp"
NewerThan = "newer-than"
NewerThanTimestamp = "newer-than-timestamp"
iso8601TimestampLayout = "2006-01-02T15:04:05Z07:00"
)
func parseTimestamp(s string) (time.Time, error) {
return time.Parse(iso8601TimestampLayout, s)
}
func AddTimeFilterFlags(cmd *cobra.Command) {
cmd.Flags().Duration(OlderThan, 0, "only objects older than a duration. Accepts durations in the format of Go's time.ParseDuration. examples: \"2h45m\", \"10m\", \"45s\"")
cmd.Flags().String(OlderThanTimestamp, "", "only objects older than an ISO 8601 timestamp. examples: '2023-06-07T10:00:00+02:00', use the date command $(date -d \"yesterday 10am\" --iso-8601=seconds)")
cmd.Flags().Duration(NewerThan, 0, "only objects newer than a duration. Accepts durations in the format of Go's time.ParseDuration. examples: \"2h45m\", \"10m\", \"45s\"")
cmd.Flags().String(NewerThanTimestamp, "", "only objects newer than an ISO 8601 timestamp. examples: '2023-06-07T10:00:00+02:00', use the date command $(date -d \"yesterday 10am\" --iso-8601=seconds)")
}
func ValidateTimestampFlags(cmd *cobra.Command) error {
flagsToValidate := []string{
OlderThanTimestamp,
NewerThanTimestamp,
}
for _, fl := range flagsToValidate {
s, err := cmd.Flags().GetString(fl)
if err != nil {
return err
}
if s == "" {
continue
}
if _, err := parseTimestamp(s); err != nil {
return err
}
}
return nil
}
func TranslateTimeFilterFlagsToFilterFuncs(cmd *cobra.Command) ([]object.ObjectFilterFunc, error) {
var filters []object.ObjectFilterFunc
filterCreationFuncs := []filterCreationFunc{
olderThanDurationFilter,
olderThanTimestampFilter,
newerThanDurationFilter,
newerThanTimestampFilter,
}
for _, fcf := range filterCreationFuncs {
newFilter, err := fcf(cmd)
if err != nil {
return nil, err
}
if newFilter != nil {
filters = append(filters, newFilter)
}
}
return filters, nil
}
type filterCreationFunc func(*cobra.Command) (object.ObjectFilterFunc, error)
func olderThanDurationFilter(cmd *cobra.Command) (object.ObjectFilterFunc, error) {
dur, err := cmd.Flags().GetDuration(OlderThan)
if err != nil {
return nil, err
}
if dur == 0 {
return nil, nil
}
return object.OlderThanFilterFunc(time.Now().Add(-dur)), nil
}
func olderThanTimestampFilter(cmd *cobra.Command) (object.ObjectFilterFunc, error) {
timestampStr, err := cmd.Flags().GetString(OlderThanTimestamp)
if err != nil {
return nil, err
}
if timestampStr == "" {
return nil, nil
}
timestamp, err := parseTimestamp(timestampStr)
if err != nil {
return nil, err
}
return object.OlderThanFilterFunc(timestamp), nil
}
func newerThanDurationFilter(cmd *cobra.Command) (object.ObjectFilterFunc, error) {
dur, err := cmd.Flags().GetDuration(NewerThan)
if err != nil {
return nil, err
}
if dur == 0 {
return nil, nil
}
return object.NewerThanFilterFunc(time.Now().Add(-dur)), nil
}
func newerThanTimestampFilter(cmd *cobra.Command) (object.ObjectFilterFunc, error) {
timestampStr, err := cmd.Flags().GetString(NewerThanTimestamp)
if err != nil {
return nil, err
}
if timestampStr == "" {
return nil, nil
}
timestamp, err := parseTimestamp(timestampStr)
if err != nil {
return nil, err
}
return object.NewerThanFilterFunc(timestamp), nil
}