Skip to content

Commit fe94ff7

Browse files
authored
feat: Backend delete prompt (#4091)
* feat: backend delete prompt * nfix: nil pointer if the remote_state block does not exist
1 parent 600f176 commit fe94ff7

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

cli/commands/backend/bootstrap/bootstrap.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ func Run(ctx context.Context, opts *options.TerragruntOptions) error {
1313
if err != nil {
1414
return err
1515
}
16+
if remoteState == nil {
17+
opts.Logger.Debug("Did not find remote `remote_state` block in the config")
18+
19+
return nil
20+
}
1621

1722
return remoteState.Init(ctx, opts)
1823
}

cli/commands/backend/delete/delete.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ func Run(ctx context.Context, cmdOpts *Options) error {
1515
if err != nil {
1616
return err
1717
}
18+
if remoteState == nil {
19+
opts.Logger.Debug("Did not find remote `remote_state` block in the config")
20+
21+
return nil
22+
}
1823

1924
if cmdOpts.DeleteBucket {
2025
// TODO: Do an extra check before commenting out the code. //return remoteState.DeleteBucket(ctx, opts)

internal/remotestate/backend/gcs/backend.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,14 @@ func (backend *Backend) Delete(ctx context.Context, backendConfig backend.Config
147147
prefix = extGCSCfg.RemoteStateConfigGCS.Prefix
148148
)
149149

150-
return client.DeleteGCSObjectIfNecessary(ctx, bucketName, prefix)
150+
prompt := fmt.Sprintf("GCS bucket %s objects with prefix %s will be deleted. Do you want to continue?", bucketName, prefix)
151+
if yes, err := shell.PromptUserForYesNo(ctx, prompt, opts); err != nil {
152+
return err
153+
} else if yes {
154+
return client.DeleteGCSObjectIfNecessary(ctx, bucketName, prefix)
155+
}
156+
157+
return nil
151158
}
152159

153160
// DeleteBucket deletes the entire bucket specified in the given config.

internal/remotestate/backend/s3/backend.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,24 @@ func (backend *Backend) Delete(ctx context.Context, backendConfig backend.Config
159159
if tableName != "" {
160160
tableKey := path.Join(bucketName, bucketKey+stateIDSuffix)
161161

162-
if err := client.DeleteTableItemIfNecessary(ctx, tableName, tableKey); err != nil {
162+
prompt := fmt.Sprintf("DynamoDB table %s key %s will be deleted. Do you want to continue?", tableName, tableKey)
163+
if yes, err := shell.PromptUserForYesNo(ctx, prompt, opts); err != nil {
163164
return err
165+
} else if yes {
166+
if err := client.DeleteTableItemIfNecessary(ctx, tableName, tableKey); err != nil {
167+
return err
168+
}
164169
}
165170
}
166171

167-
return client.DeleteS3ObjectIfNecessary(ctx, bucketName, bucketKey)
172+
prompt := fmt.Sprintf("S3 bucket %s key %s will be deleted. Do you want to continue?", bucketName, bucketKey)
173+
if yes, err := shell.PromptUserForYesNo(ctx, prompt, opts); err != nil {
174+
return err
175+
} else if yes {
176+
return client.DeleteS3ObjectIfNecessary(ctx, bucketName, bucketKey)
177+
}
178+
179+
return nil
168180
}
169181

170182
// DeleteBucket deletes the entire bucket specified in the given config.

0 commit comments

Comments
 (0)