diff --git a/plugin.go b/plugin.go index bf1ab39..e914e46 100644 --- a/plugin.go +++ b/plugin.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" "log" - "os" + "path/filepath" "strconv" "strings" @@ -112,14 +112,17 @@ func escapeMarkdownOne(str string) string { return str } -func fileExist(keys []string) []string { +func globList(keys []string) []string { var newKeys []string - for _, value := range keys { - if _, err := os.Stat(value); os.IsNotExist(err) { + for _, pattern := range keys { + pattern = strings.Trim(pattern, " ") + matches, err := filepath.Glob(pattern) + if err != nil { + fmt.Printf("Glob error for %q: %s\n", pattern, err) continue } - newKeys = append(newKeys, value) + newKeys = append(newKeys, matches...) } return newKeys @@ -233,12 +236,12 @@ func (p Plugin) Exec() error { bot.Debug = p.Config.Debug ids := parseTo(p.Config.To, p.Commit.Email, p.Config.MatchEmail) - photos := fileExist(trimElement(p.Config.Photo)) - documents := fileExist(trimElement(p.Config.Document)) - stickers := fileExist(trimElement(p.Config.Sticker)) - audios := fileExist(trimElement(p.Config.Audio)) - voices := fileExist(trimElement(p.Config.Voice)) - videos := fileExist(trimElement(p.Config.Video)) + photos := globList(trimElement(p.Config.Photo)) + documents := globList(trimElement(p.Config.Document)) + stickers := globList(trimElement(p.Config.Sticker)) + audios := globList(trimElement(p.Config.Audio)) + voices := globList(trimElement(p.Config.Voice)) + videos := globList(trimElement(p.Config.Video)) locations := trimElement(p.Config.Location) venues := trimElement(p.Config.Venue) diff --git a/plugin_test.go b/plugin_test.go index cbbf738..2695438 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -206,14 +206,17 @@ func TestParseTo(t *testing.T) { assert.Equal(t, 0, len(ids)) } -func TestCheckFileExist(t *testing.T) { +func TestGlobList(t *testing.T) { var input []string var result []string input = []string{"tests/gophercolor.png", "測試", "3"} result = []string{"tests/gophercolor.png"} + assert.Equal(t, result, globList(input)) - assert.Equal(t, result, fileExist(input)) + input = []string{"tests/*.mp3"} + result = []string{"tests/audio.mp3"} + assert.Equal(t, result, globList(input)) } func TestConvertLocation(t *testing.T) {