From 077b4763fef6f8c07a04dd36c6deef2c1f3af580 Mon Sep 17 00:00:00 2001 From: lawl Date: Fri, 4 Jun 2021 12:14:39 +0200 Subject: [PATCH] CLI: Cleanup temp files on exit Fixes #135 --- cli.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/cli.go b/cli.go index d8b09ee..7e026ff 100644 --- a/cli.go +++ b/cli.go @@ -39,15 +39,15 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) { if opt.setcap { err := makeBinarySetcapped() if err != nil { - os.Exit(1) + cleanupExit(librnnoise, 1) } - os.Exit(0) + cleanupExit(librnnoise, 0) } paClient, err := pulseaudio.NewClient() if err != nil { fmt.Fprintf(os.Stderr, "Couldn't create pulseaudio client: %v\n", err) - os.Exit(1) + cleanupExit(librnnoise, 1) } defer paClient.Close() @@ -77,7 +77,7 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) { fmt.Printf("\tDevice Name: %s\n\tDevice ID: %s\n\n", sinks[i].Name, sinks[i].ID) } - os.Exit(0) + cleanupExit(librnnoise, 0) } if opt.threshold > 0 { @@ -93,9 +93,9 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) { err := unloadSupressor(&ctx) if err != nil { fmt.Fprintf(os.Stderr, "Error unloading PulseAudio Module: %+v\n", err) - os.Exit(1) + cleanupExit(librnnoise, 1) } - os.Exit(0) + cleanupExit(librnnoise, 0) } if opt.loadInput { @@ -105,7 +105,7 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) { defaultSource, err := getDefaultSourceID(paClient) if err != nil { fmt.Fprintf(os.Stderr, "No source specified to load and failed to load default source: %+v\n", err) - os.Exit(1) + cleanupExit(librnnoise, 1) } opt.sinkName = defaultSource } @@ -115,13 +115,13 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) { err := loadSupressor(&ctx, &sources[i], &device{}) if err != nil { fmt.Fprintf(os.Stderr, "Error loading PulseAudio Module: %+v\n", err) - os.Exit(1) + cleanupExit(librnnoise, 1) } - os.Exit(0) + cleanupExit(librnnoise, 0) } } fmt.Fprintf(os.Stderr, "PulseAudio source not found: %s\n", opt.sinkName) - os.Exit(1) + cleanupExit(librnnoise, 1) } if opt.loadOutput { @@ -131,7 +131,7 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) { defaultSink, err := getDefaultSinkID(paClient) if err != nil { fmt.Fprintf(os.Stderr, "No sink specified to load and failed to load default sink: %+v\n", err) - os.Exit(1) + cleanupExit(librnnoise, 1) } opt.sinkName = defaultSink } @@ -141,13 +141,18 @@ func doCLI(opt CLIOpts, config *config, librnnoise string) { err := loadSupressor(&ctx, &device{}, &sinks[i]) if err != nil { fmt.Fprintf(os.Stderr, "Error loading PulseAudio Module: %+v\n", err) - os.Exit(1) + cleanupExit(librnnoise, 1) } - os.Exit(0) + cleanupExit(librnnoise, 0) } } fmt.Fprintf(os.Stderr, "PulseAudio sink not found: %s\n", opt.sinkName) - os.Exit(1) + cleanupExit(librnnoise, 1) } } + +func cleanupExit(librnnoise string, exitCode int) { + removeLib(librnnoise) + os.Exit(exitCode) +}