mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 05:11:06 +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:
commit
cdf3aa0a31
3 changed files with 43 additions and 5 deletions
28
main.go
28
main.go
|
@ -6,8 +6,12 @@ package main
|
|||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/schollz/croc/v10/src/cli"
|
||||
"github.com/schollz/croc/v10/src/utils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -27,7 +31,25 @@ 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...")
|
||||
utils.CleanupTempData()
|
||||
|
||||
// Exit the program gracefully
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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("temp directory and its contents deleted successfully")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue