From 95de3790d7b7b10b00701ea2cf2fd75af16d797f Mon Sep 17 00:00:00 2001 From: Prince Date: Tue, 3 Sep 2024 18:27:19 -0700 Subject: [PATCH 1/4] added signal for programme termination --- main.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 65ad69a9..b29bb9d8 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,9 @@ package main import ( "log" + "os" + "os/signal" + "syscall" "github.com/schollz/croc/v10/src/cli" ) @@ -27,7 +30,24 @@ func main() { // fmt.Println("wrote profile") // } // }() - if err := cli.Run(); err != nil { - log.Fatalln(err) - } + + // Create a channel to receive OS signals + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + go func() { + if err := cli.Run(); err != nil { + log.Fatalln(err) + } + }() + + // Wait for a termination signal + sig := <-sigs + log.Println("Received signal:", sig) + + // Perform any necessary cleanup here + log.Println("Performing cleanup...") + + // Exit the program gracefully + os.Exit(0) } From 0d8e4e10d621bb9268a02a9641db0c83b475e1ed Mon Sep 17 00:00:00 2001 From: Prince Date: Tue, 3 Sep 2024 18:56:46 -0700 Subject: [PATCH 2/4] 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") + } +} From 986d005449ae3589c0437c5f63b0dd04d4dd4619 Mon Sep 17 00:00:00 2001 From: Prince Date: Tue, 3 Sep 2024 19:43:28 -0700 Subject: [PATCH 3/4] removed unnessary file --- device-a/text.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 device-a/text.txt diff --git a/device-a/text.txt b/device-a/text.txt deleted file mode 100644 index 5e40c087..00000000 --- a/device-a/text.txt +++ /dev/null @@ -1 +0,0 @@ -asdf \ No newline at end of file From e663aa90cb640f7e05d0d8bb8aae85845308c04a Mon Sep 17 00:00:00 2001 From: Prince Date: Tue, 3 Sep 2024 19:49:56 -0700 Subject: [PATCH 4/4] updated log message --- src/utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/utils.go b/src/utils/utils.go index f55bb703..3538aa8c 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -617,6 +617,6 @@ func CleanupTempData() { if err != nil { log.Fatal(err) } else { - log.Println("Directory and its contents deleted successfully") + log.Println("temp directory and its contents deleted successfully") } }