Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcf8a63

Browse files
authored
feat: CLI first successful sync hint message (#21175)
Uses cloudquery/cloudquery-api-go#307 Shows a hint message on successful (total resources > 0) sync if the source is `aws` and one of the destinations is `postgresql` or `sqlite`. Works only with bearer tokens (`cloudquery login`). ``` Loading spec(s) from ./aws_to_sqlite.yaml Starting sync for: aws (cloudquery/[email protected]) -> [sqlite (cloudquery/[email protected])] Sync completed successfully. Resources: 1337, Errors: 0, Warnings: 0, Time: 3s 🎉 Success! Run the following command to get your oldest 10 EC2 instances: sqlite3 ./db.sql "SELECT account_id, instance_id, region, launch_time FROM aws_ec2_instances ORDER BY launch_time ASC LIMIT 10" ```
1 parent 48aaefd commit bcf8a63

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

cli/cmd/sync_v3.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88
"io"
99
"net/http"
1010
"os"
11+
"strconv"
1112
gosync "sync"
1213
"sync/atomic"
1314
"time"
1415

1516
"github.com/apache/arrow-go/v18/arrow"
1617
cloudquery_api "github.com/cloudquery/cloudquery-api-go"
1718
"github.com/cloudquery/cloudquery-api-go/auth"
19+
"github.com/cloudquery/cloudquery-api-go/config"
1820
"github.com/cloudquery/plugin-pb-go/managedplugin"
1921
"github.com/cloudquery/plugin-pb-go/metrics"
2022
"github.com/cloudquery/plugin-pb-go/pb/plugin/v3"
@@ -748,9 +750,65 @@ func syncConnectionV3(ctx context.Context, syncOptions syncV3Options) (syncErr e
748750
Str("result", msg).
749751
Msg("Sync summary")
750752

753+
if totalResources > 0 {
754+
hintSelectMessage(sourceSpec.Path, destinationSpecs, statsPerTable)
755+
}
756+
751757
return nil
752758
}
753759

760+
func hintSelectMessage(sourcePath string, destinationSpecs []specs.Destination, statsPerTable *utils.ConcurrentMap[string, cloudquery_api.SyncRunTableProgressValue]) {
761+
if sourcePath != "cloudquery/aws" {
762+
return
763+
}
764+
765+
val, _ := config.GetValue("first_sync_completed")
766+
firstSyncCompleted, _ := strconv.ParseBool(val)
767+
if firstSyncCompleted {
768+
return
769+
}
770+
771+
if auth.NewTokenClient().GetTokenType() != auth.BearerToken {
772+
return
773+
}
774+
775+
rows, ok := statsPerTable.Get("aws_ec2_instances")
776+
if !ok || rows.Rows == 0 {
777+
return
778+
}
779+
780+
destPaths := make(map[string]int, len(destinationSpecs))
781+
for i := range destinationSpecs {
782+
destPaths[destinationSpecs[i].Path] = i + 1
783+
}
784+
785+
switch {
786+
case destPaths["cloudquery/postgresql"] > 0:
787+
fmt.Println()
788+
fmt.Println("🎉 Success!")
789+
fmt.Println()
790+
fmt.Println("Run the following command to get your oldest 10 EC2 instances:")
791+
fmt.Println()
792+
fmt.Println(`SELECT account_id, instance_id, region, launch_time FROM aws_ec2_instances ORDER BY launch_time ASC LIMIT 10`)
793+
case destPaths["cloudquery/sqlite"] > 0:
794+
innerSpec := destinationSpecs[destPaths["cloudquery/sqlite"]-1].Spec
795+
796+
fmt.Println()
797+
fmt.Println("🎉 Success!")
798+
fmt.Println()
799+
fmt.Println("Run the following command to get your oldest 10 EC2 instances:")
800+
fmt.Println()
801+
fmt.Printf(`sqlite3 %s "SELECT account_id, instance_id, region, launch_time FROM aws_ec2_instances ORDER BY launch_time ASC LIMIT 10"`, innerSpec["connection_string"])
802+
fmt.Println()
803+
default:
804+
return
805+
}
806+
807+
if err := config.SetValue("first_sync_completed", "true"); err != nil {
808+
log.Debug().Err(err).Msg("Failed to set first_sync_completed")
809+
}
810+
}
811+
754812
func deleteStale(client safeWriteClient, tables map[string]bool, sourceName string, syncTime time.Time) error {
755813
for tableName := range tables {
756814
if err := client.Send(&plugin.Write_Request{

cli/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/apache/arrow-go/v18 v18.4.0
77
github.com/bradleyjkemp/cupaloy/v2 v2.8.0
88
github.com/cenkalti/backoff/v4 v4.3.0
9-
github.com/cloudquery/cloudquery-api-go v1.13.11
9+
github.com/cloudquery/cloudquery-api-go v1.14.0
1010
github.com/cloudquery/codegen v0.3.31
1111
github.com/cloudquery/plugin-pb-go v1.26.15
1212
github.com/cloudquery/plugin-sdk/v4 v4.88.1

cli/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5O
4242
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
4343
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
4444
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
45-
github.com/cloudquery/cloudquery-api-go v1.13.11 h1:7zstvvVgTh9Nj4DmWaF2Ve3FQybIOtZf/8ZDBZw+NsE=
46-
github.com/cloudquery/cloudquery-api-go v1.13.11/go.mod h1:eLlKRJgGvskJIGz9RVo7TwK9BRyAM39pUg+3omCDPKA=
45+
github.com/cloudquery/cloudquery-api-go v1.14.0 h1:5gc3NBjBTeZ+pf6ML4GIRofMvbL0d81TR/j8QPD1X7U=
46+
github.com/cloudquery/cloudquery-api-go v1.14.0/go.mod h1:eLlKRJgGvskJIGz9RVo7TwK9BRyAM39pUg+3omCDPKA=
4747
github.com/cloudquery/codegen v0.3.31 h1:YDqokUyWSECewoaISY4D2iIpFRTDnPtWmQOFgaQ60c0=
4848
github.com/cloudquery/codegen v0.3.31/go.mod h1:vU4G8lqQUPHF9ooUQY0RVbbjMPOD/6uqJDgMXfSgK8M=
4949
github.com/cloudquery/godebouncer v0.0.0-20230626172639-4b59d27e1b8c h1:o8Xwg6fiYnqCQuQVbt3YDvpgrB6Ipc0CNoBgAjvHp6s=

0 commit comments

Comments
 (0)