0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

Merge pull request #800 from Prince-Bhagat/adding-temp-directory-for-managing-temporary-files

Remove Temporary Files if the program terminates abnormal
This commit is contained in:
Zack 2024-09-03 18:10:37 +02:00 committed by GitHub
commit cdf3aa0a31
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 5 deletions

22
main.go
View file

@ -6,8 +6,12 @@ package main
import ( import (
"log" "log"
"os"
"os/signal"
"syscall"
"github.com/schollz/croc/v10/src/cli" "github.com/schollz/croc/v10/src/cli"
"github.com/schollz/croc/v10/src/utils"
) )
func main() { func main() {
@ -27,7 +31,25 @@ func main() {
// fmt.Println("wrote profile") // fmt.Println("wrote profile")
// } // }
// }() // }()
// 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 { if err := cli.Run(); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
}()
// Wait for a termination signal
sig := <-sigs
log.Println("Received signal:", sig)
// Perform any necessary cleanup here
log.Println("Performing cleanup...")
utils.CleanupTempData()
// Exit the program gracefully
os.Exit(0)
} }

View file

@ -446,9 +446,15 @@ func getStdin() (fnames []string, err error) {
fnames = []string{f.Name()} fnames = []string{f.Name()}
return 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) { 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 { if err != nil {
return return
} }

View file

@ -610,3 +610,13 @@ func ValidFileName(fname string) (err error) {
} }
return return
} }
func CleanupTempData() {
path := "temp"
// Remove the directory and its contents
err := os.RemoveAll(path)
if err != nil {
log.Fatal(err)
} else {
log.Println("temp directory and its contents deleted successfully")
}
}