Skip to content

Commit

Permalink
Merge pull request #87 from x-motemen/fix-config
Browse files Browse the repository at this point in the history
[breaking change] normalize localRoot to absolute path
  • Loading branch information
Songmu authored Oct 13, 2023
2 parents 9072b74 + 457d26b commit d4dae75
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 147 deletions.
37 changes: 28 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"

"gopkg.in/yaml.v2"
)
Expand All @@ -22,30 +23,48 @@ type blogConfig struct {
Owner string `yaml:"owner"`
}

func loadConfig(r io.Reader) (*config, error) {
func loadConfig(r io.Reader, fpath string) (*config, error) {
bytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}

if !filepath.IsAbs(fpath) {
var err error
fpath, err = filepath.Abs(fpath)
if err != nil {
return nil, err
}
}
absDir := filepath.Dir(fpath)

var blogs map[string]*blogConfig
err = yaml.Unmarshal(bytes, &blogs)
if err != nil {
return nil, err
}

c := &config{
Default: blogs["default"],
Blogs: blogs,
}

delete(blogs, "default")
for key, b := range blogs {
if b == nil {
b = &blogConfig{}
blogs[key] = b
}
b.BlogID = key
if b.LocalRoot != "" && !filepath.IsAbs(b.LocalRoot) {
b.LocalRoot = filepath.Join(absDir, b.LocalRoot)
}
if b.BlogID != "default" {
b.BlogID = key
}
blogs[key] = b
}

defaultConf := blogs["default"]
if defaultConf == nil {
defaultConf = &blogConfig{}
}
delete(blogs, "default")
c := &config{
Default: defaultConf,
Blogs: blogs,
}
return c, nil
}
Expand Down
Loading

0 comments on commit d4dae75

Please sign in to comment.