From 0d8e4e10d621bb9268a02a9641db0c83b475e1ed Mon Sep 17 00:00:00 2001 From: Prince Date: Tue, 3 Sep 2024 18:56:46 -0700 Subject: [PATCH] added cleanup function to remove temporary file --- device-a/text.txt | 1 + main.go | 2 ++ src/cli/cli.go | 10 ++++++++-- src/utils/utils.go | 10 ++++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 device-a/text.txt diff --git a/device-a/text.txt b/device-a/text.txt new file mode 100644 index 00000000..5e40c087 --- /dev/null +++ b/device-a/text.txt @@ -0,0 +1 @@ +asdf \ No newline at end of file diff --git a/main.go b/main.go index b29bb9d8..f36d2e80 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "syscall" "github.com/schollz/croc/v10/src/cli" + "github.com/schollz/croc/v10/src/utils" ) func main() { @@ -47,6 +48,7 @@ func main() { // Perform any necessary cleanup here log.Println("Performing cleanup...") + utils.CleanupTempData() // Exit the program gracefully os.Exit(0) diff --git a/src/cli/cli.go b/src/cli/cli.go index d65281aa..fa856082 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -446,9 +446,15 @@ func getStdin() (fnames []string, err error) { fnames = []string{f.Name()} return } - +func makeTempFolder() { + path := "temp" + if _, err := os.Stat(path); os.IsNotExist(err) { + os.Mkdir(path, os.ModePerm) + } +} func makeTempFileWithString(s string) (fnames []string, err error) { - f, err := os.CreateTemp(".", "croc-stdin-") + makeTempFolder() + f, err := os.CreateTemp("temp", "croc-stdin-") if err != nil { return } diff --git a/src/utils/utils.go b/src/utils/utils.go index 1f09c2ac..f55bb703 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -610,3 +610,13 @@ func ValidFileName(fname string) (err error) { } return } +func CleanupTempData() { + path := "temp" + // Remove the directory and its contents + err := os.RemoveAll(path) + if err != nil { + log.Fatal(err) + } else { + log.Println("Directory and its contents deleted successfully") + } +}