Skip to content

Commit

Permalink
Merge pull request #361 from magodo/not_remove_mapping_file
Browse files Browse the repository at this point in the history
Allow resource mapping file in the output directory in `map` mode
  • Loading branch information
magodo authored Feb 16, 2023
2 parents 1f3d18e + c78dad6 commit 2ac2300
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 13 additions & 1 deletion internal/utils/remove_everything.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@ import (
"path/filepath"
)

func RemoveEverythingUnder(path string) error {
// RemoveEverythingUnder removes everything under a path.
//
// The top level directory entries whose name matches any "skipps" will be skipped.
func RemoveEverythingUnder(path string, skipps ...string) error {
// #nosec G304
dir, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to read directory %s: %v", path, err)
}

skipMap := map[string]bool{}
for _, v := range skipps {
skipMap[v] = true
}

entries, _ := dir.Readdirnames(0)
for _, entry := range entries {
if skipMap[entry] {
continue
}
if err := os.RemoveAll(filepath.Join(path, entry)); err != nil {
return fmt.Errorf("failed to remove %s: %v", entry, err)
}
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

internalconfig "github.com/Azure/aztfy/internal/config"
"github.com/Azure/aztfy/internal/meta"
"github.com/pkg/profile"

"github.com/Azure/aztfy/pkg/config"
Expand Down Expand Up @@ -130,7 +131,7 @@ func main() {
if !empty {
switch {
case flagOverwrite:
if err := utils.RemoveEverythingUnder(flagOutputDir); err != nil {
if err := utils.RemoveEverythingUnder(flagOutputDir, meta.ResourceMappingFileName); err != nil {
return fmt.Errorf("failed to clean up output directory %q: %v", flagOutputDir, err)
}
case flagAppend:
Expand All @@ -154,7 +155,7 @@ 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(flagOutputDir); err != nil {
if err := utils.RemoveEverythingUnder(flagOutputDir, meta.ResourceMappingFileName); err != nil {
return err
}
case "n":
Expand Down

0 comments on commit 2ac2300

Please sign in to comment.