Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavior change of --overwrite to avoid deleting user files #553

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions command_before_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"strings"

"github.com/Azure/aztfexport/internal/meta"
"github.com/Azure/aztfexport/internal/utils"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -131,9 +130,6 @@ func commandBeforeFunc(fset *FlagSet, mode Mode) func(ctx *cli.Context) error {
if !empty {
switch {
case fset.flagOverwrite:
if err := utils.RemoveEverythingUnder(fset.flagOutputDir, meta.ResourceMappingFileName); err != nil {
return fmt.Errorf("failed to clean up output directory %q: %v", fset.flagOutputDir, err)
}
case fset.flagAppend:
tfblock, err = utils.InspecTerraformBlock(fset.flagOutputDir)
if err != nil {
Expand All @@ -148,7 +144,7 @@ func commandBeforeFunc(fset *FlagSet, mode Mode) func(ctx *cli.Context) error {
fmt.Printf(`
The output directory is not empty. Please choose one of actions below:

* Press "Y" to overwrite the existing directory with new files
* Press "Y" to proceed that will likely pollute the existing files and cause errors
* Press "N" to append new files and add to the existing state instead
* Press other keys to quit

Expand All @@ -158,9 +154,6 @@ The output directory is not empty. Please choose one of actions below:
fmt.Scanf("%s", &ans)
switch strings.ToLower(ans) {
case "y":
if err := utils.RemoveEverythingUnder(fset.flagOutputDir, meta.ResourceMappingFileName); err != nil {
return err
}
case "n":
if fset.flagHCLOnly {
return fmt.Errorf("`--hcl-only` can only run within an empty directory. Use `-o` to specify an empty directory.")
Expand Down
53 changes: 7 additions & 46 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
Expand Down Expand Up @@ -541,53 +540,15 @@ func (meta baseMeta) ExportSkippedResources(_ context.Context, l ImportList) err
}

func (meta baseMeta) CleanUpWorkspace(_ context.Context) error {
// For hcl only mode with using terraform binary, we will have to clean up everything under the output directory,
// For hcl only mode with using terraform binary, we will have to clean up the state and terraform cli/provider related files the output directory,
// except for the TF code, resource mapping file and ignore list file.
if meta.hclOnly && meta.tfclient == nil {
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return err
}
defer func() {
// #nosec G104
os.RemoveAll(tmpDir)
}()

tmpMainCfg := filepath.Join(tmpDir, meta.outputFileNames.MainFileName)
tmpProviderCfg := filepath.Join(tmpDir, meta.outputFileNames.ProviderFileName)
tmpResourceMappingFileName := filepath.Join(tmpDir, ResourceMappingFileName)
tmpSkippedResourcesFileName := filepath.Join(tmpDir, SkippedResourcesFileName)

if err := utils.CopyFile(filepath.Join(meta.outdir, meta.outputFileNames.MainFileName), tmpMainCfg); err != nil {
return err
}
if err := utils.CopyFile(filepath.Join(meta.outdir, meta.outputFileNames.ProviderFileName), tmpProviderCfg); err != nil {
return err
}
if err := utils.CopyFile(filepath.Join(meta.outdir, ResourceMappingFileName), tmpResourceMappingFileName); err != nil {
return err
}
if err := utils.CopyFile(filepath.Join(meta.outdir, SkippedResourcesFileName), tmpSkippedResourcesFileName); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
}

if err := utils.RemoveEverythingUnder(meta.outdir); err != nil {
return err
}

if err := utils.CopyFile(tmpMainCfg, filepath.Join(meta.outdir, meta.outputFileNames.MainFileName)); err != nil {
return err
}
if err := utils.CopyFile(tmpProviderCfg, filepath.Join(meta.outdir, meta.outputFileNames.ProviderFileName)); err != nil {
return err
}
if err := utils.CopyFile(tmpResourceMappingFileName, filepath.Join(meta.outdir, ResourceMappingFileName)); err != nil {
return err
}
if err := utils.CopyFile(tmpSkippedResourcesFileName, filepath.Join(meta.outdir, SkippedResourcesFileName)); err != nil {
if !errors.Is(err, os.ErrNotExist) {
for _, entryName := range []string{
"terraform.tfstate",
".terraform",
".terraform.lock.hcl",
} {
if err := os.RemoveAll(filepath.Join(meta.outdir, entryName)); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func main() {
Name: "overwrite",
EnvVars: []string{"AZTFEXPORT_OVERWRITE"},
Aliases: []string{"f"},
Usage: "Overwrites the output directory if it is not empty (use with caution)",
Usage: "Proceed with non-empty output directory, which is likely to pollute the directory and cause errors (use with caution)",
Destination: &flagset.flagOverwrite,
},
&cli.BoolFlag{
Expand Down
Loading