feat: Support wildcard attachment list (#62)

This commit is contained in:
Bo-Yi Wu 2018-04-06 10:28:55 +08:00 committed by GitHub
parent 8b6e33fcab
commit a2a49f1249
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 13 deletions

View File

@ -4,7 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"os" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -112,14 +112,17 @@ func escapeMarkdownOne(str string) string {
return str return str
} }
func fileExist(keys []string) []string { func globList(keys []string) []string {
var newKeys []string var newKeys []string
for _, value := range keys { for _, pattern := range keys {
if _, err := os.Stat(value); os.IsNotExist(err) { pattern = strings.Trim(pattern, " ")
matches, err := filepath.Glob(pattern)
if err != nil {
fmt.Printf("Glob error for %q: %s\n", pattern, err)
continue continue
} }
newKeys = append(newKeys, value) newKeys = append(newKeys, matches...)
} }
return newKeys return newKeys
@ -233,12 +236,12 @@ func (p Plugin) Exec() error {
bot.Debug = p.Config.Debug bot.Debug = p.Config.Debug
ids := parseTo(p.Config.To, p.Commit.Email, p.Config.MatchEmail) ids := parseTo(p.Config.To, p.Commit.Email, p.Config.MatchEmail)
photos := fileExist(trimElement(p.Config.Photo)) photos := globList(trimElement(p.Config.Photo))
documents := fileExist(trimElement(p.Config.Document)) documents := globList(trimElement(p.Config.Document))
stickers := fileExist(trimElement(p.Config.Sticker)) stickers := globList(trimElement(p.Config.Sticker))
audios := fileExist(trimElement(p.Config.Audio)) audios := globList(trimElement(p.Config.Audio))
voices := fileExist(trimElement(p.Config.Voice)) voices := globList(trimElement(p.Config.Voice))
videos := fileExist(trimElement(p.Config.Video)) videos := globList(trimElement(p.Config.Video))
locations := trimElement(p.Config.Location) locations := trimElement(p.Config.Location)
venues := trimElement(p.Config.Venue) venues := trimElement(p.Config.Venue)

View File

@ -206,14 +206,17 @@ func TestParseTo(t *testing.T) {
assert.Equal(t, 0, len(ids)) assert.Equal(t, 0, len(ids))
} }
func TestCheckFileExist(t *testing.T) { func TestGlobList(t *testing.T) {
var input []string var input []string
var result []string var result []string
input = []string{"tests/gophercolor.png", "測試", "3"} input = []string{"tests/gophercolor.png", "測試", "3"}
result = []string{"tests/gophercolor.png"} 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) { func TestConvertLocation(t *testing.T) {