@@ -21,6 +21,7 @@ import (
21
21
"os"
22
22
"path/filepath"
23
23
"strconv"
24
+ "strings"
24
25
25
26
"github.com/kvannotten/pcd"
26
27
homedir "github.com/mitchellh/go-homedir"
@@ -88,7 +89,7 @@ func findPodcast(idOrName string) (*pcd.Podcast, error) {
88
89
if err != nil {
89
90
switch err .(type ) {
90
91
case * strconv.NumError : // try as a name instead
91
- return findByName (idOrName ), nil
92
+ return findByNameFragment (idOrName ), nil
92
93
default :
93
94
log .Print ("Could not parse podcast search argument, please use the ID or the name" )
94
95
return nil , err
@@ -107,9 +108,10 @@ func findAll() []pcd.Podcast {
107
108
return podcasts
108
109
}
109
110
110
- func findByName (name string ) * pcd.Podcast {
111
+ func findByNameFragment (name string ) * pcd.Podcast {
111
112
return findByFunc (func (podcast * pcd.Podcast ) bool {
112
- return podcast .Name == name
113
+ return strings .Contains (
114
+ strings .ToLower (podcast .Name ), strings .ToLower (name ))
113
115
})
114
116
}
115
117
@@ -121,12 +123,19 @@ func findByID(id int) *pcd.Podcast {
121
123
122
124
func findByFunc (fn func (podcast * pcd.Podcast ) bool ) * pcd.Podcast {
123
125
podcasts := findAll ()
126
+ var matchedPodcasts = make ([]* pcd.Podcast , 0 )
124
127
125
128
for _ , podcast := range podcasts {
126
129
if fn (& podcast ) {
127
- return & podcast
130
+ matchedPodcast := podcast
131
+ matchedPodcasts = append (matchedPodcasts , & matchedPodcast )
128
132
}
129
133
}
134
+ if len (matchedPodcasts ) == 1 {
135
+ return matchedPodcasts [0 ]
136
+ } else {
137
+ log .Fatalf ("Provided search term matched too many podcasts: %v" , matchedPodcasts )
138
+ }
130
139
131
140
return nil
132
141
}
0 commit comments