Skip to content

Commit 5b85781

Browse files
committed
Add remove file prompt
Signed-off-by: Matt Welke <matt.welke@spectrocloud.com>
1 parent 62c9044 commit 5b85781

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

prompts/file.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/pterm/pterm"
1213
"github.com/spectrocloud-labs/prompts-tui/prompts/mocks"
1314
)
1415

@@ -182,3 +183,24 @@ func FilterLines(lines []string, validate func(input string) error) ([]string, e
182183

183184
return out, nil
184185
}
186+
187+
// RemoveFile prompts a user whether they want to remove a file. Removes the file if the user wants
188+
// it to be removed. If no error is encountered, prints a message telling the user the result. In
189+
// case of error, does not print any further message and returns the error to be handled by caller.
190+
func RemoveFile(path string, defaultVal bool) error {
191+
remove, err := ReadBool(fmt.Sprintf("Remove file %s from disk", path), defaultVal)
192+
if err != nil {
193+
return err
194+
}
195+
196+
if remove {
197+
if err := os.Remove(path); err != nil {
198+
return err
199+
}
200+
pterm.Info.Println("File removed.")
201+
} else {
202+
pterm.Info.Println("File kept.")
203+
}
204+
205+
return nil
206+
}

0 commit comments

Comments
 (0)