From 5b85781fafb9bd8e1a3ac7b0a1c17c68046d576d Mon Sep 17 00:00:00 2001 From: Matt Welke Date: Thu, 9 Jan 2025 13:59:41 -0500 Subject: [PATCH] Add remove file prompt Signed-off-by: Matt Welke --- prompts/file.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/prompts/file.go b/prompts/file.go index a7c09c0..b1c9055 100644 --- a/prompts/file.go +++ b/prompts/file.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "github.com/pterm/pterm" "github.com/spectrocloud-labs/prompts-tui/prompts/mocks" ) @@ -182,3 +183,24 @@ func FilterLines(lines []string, validate func(input string) error) ([]string, e return out, nil } + +// RemoveFile prompts a user whether they want to remove a file. Removes the file if the user wants +// it to be removed. If no error is encountered, prints a message telling the user the result. In +// case of error, does not print any further message and returns the error to be handled by caller. +func RemoveFile(path string, defaultVal bool) error { + remove, err := ReadBool(fmt.Sprintf("Remove file %s from disk", path), defaultVal) + if err != nil { + return err + } + + if remove { + if err := os.Remove(path); err != nil { + return err + } + pterm.Info.Println("File removed.") + } else { + pterm.Info.Println("File kept.") + } + + return nil +}