Skip to content

Commit 8048b1b

Browse files
committed
properly set latest version
1 parent 96595f0 commit 8048b1b

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

cmd/ctrlc/root/sync/google/secrets/secrets.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/ctrlplanedev/cli/internal/api"
1313
"github.com/spf13/cobra"
1414
"github.com/spf13/viper"
15-
secretmanager "google.golang.org/api/secretmanager/v1"
15+
"google.golang.org/api/secretmanager/v1"
1616
)
1717

1818
// NewSyncSecretsCmd creates a new cobra command for syncing Google Secret Manager secrets
@@ -138,33 +138,30 @@ func processSecret(_ context.Context, secretClient *secretmanager.Service, secre
138138

139139
// Get the most recent version
140140
mostRecentVersion := ""
141-
if len(secret.Topics) > 0 {
142-
// List versions
143-
versionsResponse, err := secretClient.Projects.Secrets.Versions.List(secret.Name).Do()
144-
if err == nil && versionsResponse.Versions != nil && len(versionsResponse.Versions) > 0 {
145-
// Find the most recent ENABLED version
146-
var latestVersion *secretmanager.SecretVersion
147-
for _, version := range versionsResponse.Versions {
148-
if version.State == "ENABLED" {
149-
if latestVersion == nil || version.CreateTime > latestVersion.CreateTime {
150-
latestVersion = version
151-
}
141+
versionsResponse, err := secretClient.Projects.Secrets.Versions.List(secret.Name).Do()
142+
if err == nil && len(versionsResponse.Versions) > 0 {
143+
// Find the most recent ENABLED version
144+
var latestVersion *secretmanager.SecretVersion
145+
for _, version := range versionsResponse.Versions {
146+
if version.State == "ENABLED" {
147+
if latestVersion == nil || version.CreateTime > latestVersion.CreateTime {
148+
latestVersion = version
152149
}
153150
}
151+
}
152+
153+
if latestVersion != nil {
154+
// Get just the version ID from the full name
155+
// Format: projects/{project}/secrets/{secret}/versions/{version}
156+
parts := strings.Split(latestVersion.Name, "/")
157+
mostRecentVersion = parts[len(parts)-1]
158+
metadata["secret/latest-version"] = mostRecentVersion
154159

155-
if latestVersion != nil {
156-
// Get just the version ID from the full name
157-
// Format: projects/{project}/secrets/{secret}/versions/{version}
158-
parts := strings.Split(latestVersion.Name, "/")
159-
mostRecentVersion = parts[len(parts)-1]
160-
metadata["secret/latest-version"] = mostRecentVersion
161-
162-
// Add version creation time
163-
if latestVersion.CreateTime != "" {
164-
creationTime, err := time.Parse(time.RFC3339, latestVersion.CreateTime)
165-
if err == nil {
166-
metadata["secret/latest-version-created"] = creationTime.Format(time.RFC3339)
167-
}
160+
// Add version creation time
161+
if latestVersion.CreateTime != "" {
162+
creationTime, err := time.Parse(time.RFC3339, latestVersion.CreateTime)
163+
if err == nil {
164+
metadata["secret/latest-version-created"] = creationTime.Format(time.RFC3339)
168165
}
169166
}
170167
}

0 commit comments

Comments
 (0)