Skip to content

Commit 4242ce5

Browse files
author
Przemysław Stępień
authored
feat: Support different formats for time variables (#20626)
#### Summary ⚠️ **If you're contributing to a plugin please read this section of the [contribution guidelines](https://github.com/cloudquery/cloudquery/blob/main/CONTRIBUTING.md#open-core-vs-open-source) 🧑‍🎓 before submitting this PR** ⚠️ This PR follows changes introduced in #20399. In enables formatting substituted times using '|' operator according to golang time layout spec https://go.dev/src/time/format.go.
1 parent c2b041e commit 4242ce5

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

cli/internal/specs/v0/spec_reader.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,18 @@ func expandTime(cfg []byte) ([]byte, error) {
7777
var expandErr error
7878
cfg = timeRegex.ReplaceAllFunc(cfg, func(match []byte) []byte {
7979
relativeTime := timeRegex.FindSubmatch(match)[1]
80-
parsedTime, err := configtype.ParseTime(string(relativeTime))
80+
splitString := strings.Split(string(relativeTime), "|")
81+
relativeTimeString := splitString[0]
82+
timeFormat := time.RFC3339
83+
if len(splitString) > 1 {
84+
timeFormat = splitString[1]
85+
}
86+
parsedTime, err := configtype.ParseTime(relativeTimeString)
8187
if err != nil {
8288
expandErr = errors.Join(errors.New("failed to substitute time"), err)
8389
return nil
8490
}
85-
return []byte(parsedTime.AsTime(time.Now()).Format(time.RFC3339))
91+
return []byte(parsedTime.AsTime(time.Now()).Format(timeFormat))
8692
})
8793
return cfg, expandErr
8894
}

cli/internal/specs/v0/spec_reader_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,39 @@ var specLoaderTestCases = []specLoaderTestCase{
687687
},
688688
},
689689
},
690+
{
691+
name: "Time substitution with formatting",
692+
path: []string{getPath("time_substitution_with_format.yml")},
693+
err: func() string {
694+
return ""
695+
},
696+
sources: []*Source{
697+
{
698+
Metadata: Metadata{
699+
Name: "aws",
700+
Path: "cloudquery/aws",
701+
Version: "v1.3.3",
702+
Registry: RegistryCloudQuery,
703+
registryInferred: true,
704+
},
705+
Destinations: []string{"postgresql"},
706+
Tables: []string{"test"},
707+
Spec: map[string]any{"table_options": map[string]any{"aws_cloudtrail_events": map[string]any{"lookup_events": []any{map[string]any{"start_time": time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC).Format(time.DateOnly)}}}}},
708+
},
709+
},
710+
destinations: []*Destination{
711+
{
712+
Metadata: Metadata{
713+
Name: "postgresql",
714+
Path: "cloudquery/postgresql",
715+
Version: "v1.6.3",
716+
Registry: RegistryCloudQuery,
717+
registryInferred: true,
718+
},
719+
Spec: map[string]any{},
720+
},
721+
},
722+
},
690723
{
691724
name: "Time substitution with error",
692725
path: []string{getPath("time_substitution_with_error.yml")},
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
kind: source
2+
spec:
3+
name: aws
4+
version: v1.3.3
5+
destinations: [postgresql]
6+
path: cloudquery/aws
7+
tables: [test]
8+
spec:
9+
table_options:
10+
aws_cloudtrail_events:
11+
lookup_events:
12+
- start_time: ${time:2025-01-01|2006-01-02} # Specifying non-relative time for easier testing
13+
---
14+
kind: destination
15+
spec:
16+
name: postgresql
17+
path: cloudquery/postgresql
18+
version: v1.6.3

0 commit comments

Comments
 (0)