diff --git a/command_before_func.go b/command_before_func.go index dc28ea6..1f0a323 100644 --- a/command_before_func.go +++ b/command_before_func.go @@ -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" @@ -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 { @@ -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 @@ -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.") diff --git a/internal/meta/base_meta.go b/internal/meta/base_meta.go index ce6004b..42afe43 100644 --- a/internal/meta/base_meta.go +++ b/internal/meta/base_meta.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "log/slog" "os" @@ -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 } } diff --git a/main.go b/main.go index 91ba8de..58045f3 100644 --- a/main.go +++ b/main.go @@ -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{