diff --git a/README.md b/README.md index ca29e41..9ad692e 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ the regex argument takes in a regex with specifically-named named capture groups ## Config File Example -The configuration file is by default loaded from a file named `osiris.yml` the current user's configuration directory (`~/.config/osiris` on Linux). The config file location can be overridden with the `-c` flag. +The configuration file is by default loaded from a file named `osiris.yml` (or `osiris.yaml`, or `osiris.toml`) in the current user's configuration directory (`~/.config/osiris` on Linux). The config file location can be overridden with the `-c` flag. ```yaml --- @@ -77,6 +77,21 @@ regex: yify: '(?P[\w\.]+)\.(?P<year>\d{4})\.(?P<scene>(?:2160|1080|720)p[\w\.]+YIFY)' ``` +```toml +SeriesYear = true +Scene = true + +[Templates] +Series = "{{ .Title }}{{if .Options.SeriesYear}} ({{ .Year }}){{end}} - {{ .Episode}}{{if EpisodeTitle}} - {{ .EpisodeTitle}}{{end}}{{if .Options.Scene }} ({{ .Scene }}){{end}}" +Film = "{{ .Title }} ({{ .Year }}){{if .Options.Scene }} ({{ .Scene }}){{end}}" + +[Regex] +Series = '(?P<title>[\w\.]+)\.(?P<ep>S\d{2}E\d{2})\.(?P<eptitle>[\w\.-]+)(?P<scene>1080p.+)' + +[Regex.Custom] +yify = '(?P<title>[\w\.]+)\.(?P<year>\d{4})\.(?P<scene>(?:2160|1080|720)p[\w\.]+YIFY)' +``` + ## Usage Example ```shell diff --git a/config.go b/config.go index 5eab2dc..ba291aa 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,36 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + package main import ( + "errors" + "log" + "os" + "path" + + "github.com/BurntSushi/toml" "gopkg.in/yaml.v3" ) @@ -19,7 +49,11 @@ type config struct { } func (c *config) Parse(data []byte) error { - return yaml.Unmarshal(data, c) + err := toml.Unmarshal(data, c) + if err != nil { + return yaml.Unmarshal(data, c) + } + return err } // Argparse replaces config values with provided cli flag values @@ -38,3 +72,25 @@ func (c *config) Argparse(args *args) { c.SeriesYear = &args.SeriesYear } } + +func ConfigFile() (string, error) { + cfgdir, err := os.UserConfigDir() + if err != nil { + log.Fatalln(err) + } + cfgFile := path.Join(cfgdir, "osiris", "osiris.yml") + + if !fileExists(cfgFile) { + cfgFile = path.Join(cfgdir, "osiris", "osiris.yaml") + } + + if !fileExists(cfgFile) { + cfgFile = path.Join(cfgdir, "osiris", "osiris.toml") + } + + if !fileExists(cfgFile) { + return "", errors.New("config file does not exist") + } + + return cfgFile, nil +} diff --git a/go.mod b/go.mod index b5bc6e2..c41f89e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module git.gryffyn.io/gryffyn/osiris go 1.14 require ( + github.com/BurntSushi/toml v1.2.0 // indirect github.com/fatih/color v1.13.0 github.com/jessevdk/go-flags v1.5.0 - gopkg.in/yaml.v3 v3.0.1 // indirect + gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index a0207f1..b5635a4 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= +github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= @@ -12,8 +14,7 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/osiris.go b/osiris.go index dce65f7..95c1fdf 100644 --- a/osiris.go +++ b/osiris.go @@ -26,8 +26,8 @@ package main import ( "bytes" + "errors" "fmt" - "io/ioutil" "log" "os" "path" @@ -80,7 +80,8 @@ func main() { var args args _, err := flags.Parse(&args) - if e, ok := err.(*flags.Error); ok { + var e *flags.Error + if ok := errors.Is(err, e); ok { if e.Type == flags.ErrHelp { os.Exit(0) } else { @@ -89,18 +90,15 @@ func main() { } var data []byte - var cfgFile string + cfgFile, err := ConfigFile() + if err != nil { + log.Fatalln(err) + } if args.ConfigFile != "" { cfgFile = args.ConfigFile - } else { - cfgdir, err := os.UserConfigDir() - if err != nil { - log.Fatalln(err) - } - cfgFile = path.Join(cfgdir, "osiris", "osiris.yml") } - data, err = ioutil.ReadFile(cfgFile) + data, err = os.ReadFile(cfgFile) if err != nil { log.Fatalln(err) } @@ -132,7 +130,6 @@ func main() { } re, err = regexp.Compile(*cfg.Regex.Series) } - } if err != nil { @@ -142,10 +139,10 @@ func main() { for _, filename := range args.Positional.Filename { newfilename := getFilename(filename, re, &cfg, args.Year, args.Title, args.Film) if !args.Silent { - printRename(&filename, &newfilename) + printRename(&filename, &newfilename) //nolint:gosec } if !args.Dryrun { - renameFile(&filename, &newfilename) + renameFile(&filename, &newfilename) //nolint:gosec } } } @@ -190,13 +187,15 @@ func getFilename(filepath string, re *regexp.Regexp, cfg *config, year, title st if film { if *cfg.Templates.Film != "" { tmpl = *cfg.Templates.Film + } else { + tmpl = filmTemplate } - tmpl = filmTemplate } else { if *cfg.Templates.Series != "" { tmpl = *cfg.Templates.Series + } else { + tmpl = seriesTemplate } - tmpl = seriesTemplate } releaseTemplate, _ := template.New("release").Parse(tmpl) @@ -233,3 +232,8 @@ func getCustomPreset(cfg *config, preset *string) (*string, error) { return nil, fmt.Errorf("preset '%s' does not exist", *preset) } + +func fileExists(path string) bool { + _, err := os.Stat(path) + return !errors.Is(err, os.ErrNotExist) +}