Tool to implement robust cleanups even on process crash

This commit is contained in:
Kovid Goyal
2025-01-05 10:50:50 +05:30
parent d23adce11c
commit 0d5bcff353
3 changed files with 140 additions and 0 deletions

65
tools/cmd/atexit/main.go Normal file
View File

@@ -0,0 +1,65 @@
package atexit
import (
"bufio"
"fmt"
"os"
"os/signal"
"strings"
"kitty/tools/cli"
)
var _ = fmt.Print
func main() (rc int, err error) {
signal.Ignore()
done_channel := make(chan bool)
lines := []string{}
defer os.Stdout.Close()
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
fmt.Println(len(lines))
}
done_channel <- true
}()
keep_going := true
for keep_going {
select {
case <-done_channel:
keep_going = false
}
}
rc = 0
for _, line := range lines {
if action, rest, found := strings.Cut(line, " "); found {
switch action {
case "unlink":
if err := os.Remove(rest); err != nil {
fmt.Fprintln(os.Stderr, "Failed to remove:", rest, "with error:", err)
rc = 1
}
}
}
}
return
}
func EntryPoint(root *cli.Command) {
root.AddSubCommand(&cli.Command{
Name: "__atexit__",
Hidden: true,
OnlyArgsAllowed: true,
Run: func(cmd *cli.Command, args []string) (rc int, err error) {
if len(args) != 0 {
return 1, fmt.Errorf("Usage: __atexit__")
}
return main()
},
})
}

View File

@@ -21,6 +21,7 @@ import (
"kitty/kittens/unicode_input"
"kitty/tools/cli"
"kitty/tools/cmd/at"
"kitty/tools/cmd/atexit"
"kitty/tools/cmd/benchmark"
"kitty/tools/cmd/edit_in_kitty"
"kitty/tools/cmd/mouse_demo"
@@ -109,6 +110,8 @@ func KittyToolEntryPoints(root *cli.Command) {
})
// __convert_image__
images.ConvertEntryPoint(root)
// __atexit__
atexit.EntryPoint(root)
// __generate_man_pages__
root.AddSubCommand(&cli.Command{
Name: "__generate_man_pages__",