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

Allow resource mapping file in the output directory in map mode #361

Merged
merged 1 commit into from
Feb 16, 2023
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
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