Skip to content

Commit

Permalink
Behavior change of --overwrite to avoid deleting user files (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Aug 13, 2024
1 parent 1aaa68b commit f1f6cce
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 55 deletions.
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

0 comments on commit f1f6cce

Please sign in to comment.