✨ Filter files properly
This commit is contained in:
parent
3ae78a6704
commit
53b2e169d0
4 changed files with 49 additions and 8 deletions
|
|
@ -10,18 +10,22 @@ import (
|
|||
)
|
||||
|
||||
type Config struct {
|
||||
Source string `toml:"source"`
|
||||
SavePath string `toml:"save_path"`
|
||||
MusicPath string `toml:"music_path"`
|
||||
Source string `toml:"source"`
|
||||
SourceExtensions []string `toml:"source_ext"`
|
||||
Recurse bool `toml:"recurse"`
|
||||
SavePath string `toml:"save_path"`
|
||||
MusicPath string `toml:"music_path"`
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
func newConfig() Config {
|
||||
return Config{
|
||||
Source: xdg.UserDirs.Download,
|
||||
SavePath: filepath.Join(xdg.UserDirs.Documents, "StripBeats"),
|
||||
MusicPath: xdg.UserDirs.Music,
|
||||
Source: xdg.UserDirs.Download,
|
||||
SourceExtensions: []string{"webm", "mp4"},
|
||||
Recurse: true,
|
||||
SavePath: filepath.Join(xdg.UserDirs.Documents, "StripBeats"),
|
||||
MusicPath: xdg.UserDirs.Music,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,12 +2,46 @@ package files
|
|||
|
||||
import (
|
||||
"codeberg.org/danjones000/strip-beats/config"
|
||||
"github.com/akrennmair/slice"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func isValidFile(file string) bool {
|
||||
exts := config.GetConfig().SourceExtensions
|
||||
return slice.ReduceWithInitialValue(exts, false, func(acc bool, ext string) bool {
|
||||
return acc || filepath.Ext(file) == "."+ext
|
||||
})
|
||||
}
|
||||
|
||||
func filterToValidFiles(ret []string) []string {
|
||||
return slice.Filter(ret, isValidFile)
|
||||
}
|
||||
|
||||
func GetCandidates() []string {
|
||||
source := config.GetConfig().Source
|
||||
ret, _ := filepath.Glob(filepath.Join(source, "*/*"))
|
||||
recurse := config.GetConfig().Recurse
|
||||
|
||||
if !recurse {
|
||||
ret, _ := filepath.Glob(filepath.Join(source, "*"))
|
||||
return filterToValidFiles(ret)
|
||||
}
|
||||
|
||||
ret := []string{}
|
||||
filepath.WalkDir(source, func(file string, info fs.DirEntry, err error) error {
|
||||
if info.IsDir() && err != nil {
|
||||
return fs.SkipDir
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if isValidFile(file) {
|
||||
ret = append(ret, file)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return ret
|
||||
}
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -5,6 +5,7 @@ go 1.20
|
|||
require (
|
||||
github.com/BurntSushi/toml v1.3.2
|
||||
github.com/adrg/xdg v0.4.0
|
||||
github.com/akrennmair/slice v0.0.0-20220105203817-49445747ab81
|
||||
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
|
||||
github.com/spf13/cobra v1.7.0
|
||||
)
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -2,9 +2,11 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
|
|||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
|
||||
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
|
||||
github.com/akrennmair/slice v0.0.0-20220105203817-49445747ab81 h1:HnuAxArB0uUxqjRvZdjhBxE3uPXNeJvNNbuaV4QhHMU=
|
||||
github.com/akrennmair/slice v0.0.0-20220105203817-49445747ab81/go.mod h1:jk5mJ+KFznfxbCEsOPgmJkozvBfVGeaqIMs31NhXlv0=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue