Skip to content

Commit

Permalink
Merge pull request #109 from tphakala/file-command-fix
Browse files Browse the repository at this point in the history
fix: file analysis restored
  • Loading branch information
tphakala authored Apr 6, 2024
2 parents d3ecea0 + a76c00d commit b34c445
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
8 changes: 4 additions & 4 deletions cmd/directory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
)

// DirectoryCommand creates a new cobra.Command for directory analysis.
func Command(ctx *conf.Context) *cobra.Command {
func Command(settings *conf.Settings) *cobra.Command {
cmd := &cobra.Command{
Use: "directory [path]",
Short: "Analyze all *.wav files in a directory",
Long: "Provide a directory path to analyze all *.wav files within it.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// The directory to analyze is passed as the first argument
ctx.Settings.Input.Path = args[0]
return analysis.DirectoryAnalysis(ctx)
settings.Input.Path = args[0]
return analysis.DirectoryAnalysis(settings)
},
}

// Set up flags specific to the 'file' command
if err := setupFlags(cmd, ctx.Settings); err != nil {
if err := setupFlags(cmd, settings); err != nil {
fmt.Printf("error setting up flags: %v\n", err)
os.Exit(1)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
)

// FileCommand creates a new file command for analyzing a single audio file.
func Command(ctx *conf.Context) *cobra.Command {
func Command(settings *conf.Settings) *cobra.Command {
cmd := &cobra.Command{
Use: "file [input.wav]",
Short: "Analyze an audio file",
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 {
// Input file path is the first argument
ctx.Settings.Input.Path = args[0]
return analysis.FileAnalysis(ctx)
settings.Input.Path = args[0]
return analysis.FileAnalysis(settings)
},
}

// Set up flags specific to the 'file' command
if err := setupFlags(cmd, ctx.Settings); err != nil {
if err := setupFlags(cmd, settings); err != nil {
fmt.Printf("error setting up flags: %v\n", err)
os.Exit(1)
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tphakala/birdnet-go/cmd/authors"
"github.com/tphakala/birdnet-go/cmd/directory"
"github.com/tphakala/birdnet-go/cmd/file"

//"github.com/tphakala/birdnet-go/cmd/directory"
//"github.com/tphakala/birdnet-go/cmd/file"
Expand All @@ -27,15 +29,15 @@ func RootCommand(settings *conf.Settings) *cobra.Command {
setupFlags(rootCmd, settings)

Check failure on line 29 in cmd/root.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)

// Add sub-commands to the root command.
//fileCmd := file.Command(ctx)
//directoryCmd := directory.Command(ctx)
fileCmd := file.Command(settings)
directoryCmd := directory.Command(settings)
realtimeCmd := realtime.Command(settings)
authorsCmd := authors.Command()
licenseCmd := license.Command()

subcommands := []*cobra.Command{
//fileCmd,
//directoryCmd,
fileCmd,
directoryCmd,
realtimeCmd,
authorsCmd,
licenseCmd,
Expand Down
5 changes: 2 additions & 3 deletions internal/analysis/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"path/filepath"
"strings"

"github.com/tphakala/birdnet-go/internal/birdnet"
"github.com/tphakala/birdnet-go/internal/conf"
)

// DirectoryAnalysis processes all .wav files in the given directory for analysis.
func DirectoryAnalysis(settings *conf.Settings, bn birdnet.BirdNET) error {
func DirectoryAnalysis(settings *conf.Settings) error {
analyzeFunc := func(path string, d os.DirEntry, err error) error {
if err != nil {
// Return the error to stop the walking process.
Expand All @@ -31,7 +30,7 @@ func DirectoryAnalysis(settings *conf.Settings, bn birdnet.BirdNET) error {
if strings.HasSuffix(d.Name(), ".wav") {
fmt.Println("Analyzing file:", path)
settings.Input.Path = path
if err := FileAnalysis(settings, bn); err != nil {
if err := FileAnalysis(settings); err != nil {
// If FileAnalysis returns an error log it and continue
log.Printf("Error analyzing file '%s': %v", path, err)
return nil
Expand Down
7 changes: 6 additions & 1 deletion internal/analysis/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import (

// executeFileAnalysis conducts an analysis of an audio file and outputs the results.
// It reads an audio file, analyzes it for bird sounds, and prints the results based on the provided configuration.
func FileAnalysis(settings *conf.Settings, bn birdnet.BirdNET) error {
func FileAnalysis(settings *conf.Settings) error {
// Initialize the BirdNET interpreter.
bn, err := birdnet.NewBirdNET(settings)
if err != nil {
return fmt.Errorf("failed to initialize BirdNET: %w", err)
}

fileInfo, err := os.Stat(settings.Input.Path)
if err != nil {
Expand Down

0 comments on commit b34c445

Please sign in to comment.