Skip to content

Commit

Permalink
Merge pull request #401 from tphakala/watch-directory
Browse files Browse the repository at this point in the history
feat: add directory watch functionality and enhance audio file proces…
  • Loading branch information
tphakala authored Jan 19, 2025
2 parents cfbaf96 + 3060a6b commit 7603992
Show file tree
Hide file tree
Showing 8 changed files with 549 additions and 50 deletions.
41 changes: 39 additions & 2 deletions cmd/directory/directory.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package directory

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -18,13 +22,45 @@ func Command(settings *conf.Settings) *cobra.Command {
Long: "Provide a directory path to analyze all *.wav files within it.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Create a context that can be cancelled
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Set up signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)

// Handle shutdown in a separate goroutine
go func() {
sig := <-sigChan
fmt.Print("\n") // Add newline before the interrupt message
fmt.Printf("Received signal %v, initiating graceful shutdown...\n", sig)
cancel()
}()

// Ensure cleanup on exit
defer func() {
signal.Stop(sigChan)
}()

// The directory to analyze is passed as the first argument
settings.Input.Path = args[0]
return analysis.DirectoryAnalysis(settings)
err := analysis.DirectoryAnalysis(settings, ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
return nil
}
return err
}
return nil
},
}

// Set up flags specific to the 'file' command
// Disable printing usage on error
cmd.SilenceUsage = true
cmd.SilenceErrors = true

// Set up flags specific to the directory command
if err := setupFlags(cmd, settings); err != nil {
fmt.Printf("error setting up flags: %v\n", err)
os.Exit(1)
Expand All @@ -36,6 +72,7 @@ func Command(settings *conf.Settings) *cobra.Command {
// setupDirectoryFlags defines flags specific to the directory command.
func setupFlags(cmd *cobra.Command, settings *conf.Settings) error {
cmd.Flags().BoolVarP(&settings.Input.Recursive, "recursive", "r", false, "Recursively analyze subdirectories")
cmd.Flags().BoolVarP(&settings.Input.Watch, "watch", "w", false, "Watch directory for new files")
cmd.Flags().StringVarP(&settings.Output.File.Path, "output", "o", viper.GetString("output.file.path"), "Path to output directory")
cmd.Flags().StringVar(&settings.Output.File.Type, "type", viper.GetString("output.file.type"), "Output type: table, csv")

Expand Down
31 changes: 30 additions & 1 deletion cmd/file/file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package file

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -18,12 +22,37 @@ func Command(settings *conf.Settings) *cobra.Command {
Long: `Analyze a single audio file for bird calls and songs.`,
Args: cobra.ExactArgs(1), // the command expects exactly one argument
RunE: func(cmd *cobra.Command, args []string) error {
// Create a context that can be cancelled
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Set up signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)

// Handle shutdown in a separate goroutine
go func() {
sig := <-sigChan
fmt.Print("\n") // Add newline before the interrupt message
fmt.Printf("Received signal %v, initiating graceful shutdown...", sig)
cancel()
}()

// Input file path is the first argument
settings.Input.Path = args[0]
return analysis.FileAnalysis(settings)
err := analysis.FileAnalysis(settings, ctx)
if errors.Is(err, context.Canceled) {
// Return nil for user-initiated cancellation
return nil
}
return err
},
}

// Disable printing usage on error
cmd.SilenceUsage = true
cmd.SilenceErrors = true

// Set up flags specific to the 'file' command
if err := setupFlags(cmd, settings); err != nil {
fmt.Printf("error setting up flags: %v\n", err)
Expand Down
Loading

0 comments on commit 7603992

Please sign in to comment.