Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the blogID argument of blogsync pull optional #123

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,24 @@ func (c *config) detectBlogConfig(fpath string) *blogConfig {
return retBc
}

func (c *config) localBlogIDs() []string {
var ret []string
for blogID, bc := range c.Blogs {
if bc.local {
ret = append(ret, blogID)
}
}
return ret
}

type blogConfig struct {
BlogID string `yaml:"-"`
LocalRoot string `yaml:"local_root"`
Username string
Password string
OmitDomain *bool `yaml:"omit_domain"`
Owner string `yaml:"owner"`
local bool
}

func (bc *blogConfig) localRoot() string {
Expand All @@ -57,7 +68,8 @@ func loadConfig(r io.Reader, fpath string) (*config, error) {
return nil, err
}
}
absDir := filepath.Dir(fpath)
absDir, fname := filepath.Split(fpath)
var isLocal = fname == "blogsync.yaml"

var blogs map[string]*blogConfig
err = yaml.Unmarshal(bytes, &blogs)
Expand All @@ -83,6 +95,7 @@ func loadConfig(r io.Reader, fpath string) (*config, error) {
}
if b.BlogID != "default" {
b.BlogID = key
b.local = isLocal
}
blogs[key] = b
}
Expand Down Expand Up @@ -137,6 +150,9 @@ func mergeBlogConfig(b1, b2 *blogConfig) *blogConfig {
if b1.OmitDomain == nil {
b1.OmitDomain = b2.OmitDomain
}
if !b1.local {
b1.local = b2.local
}
return b1
}

Expand Down
33 changes: 28 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func TestLoadConfigration(t *testing.T) {
BlogID: "blog1.example.com",
LocalRoot: "/data",
Username: "blog1",
local: true,
},
},
{
Expand All @@ -205,6 +206,7 @@ func TestLoadConfigration(t *testing.T) {
LocalRoot: "/",
Username: "blog1",
Password: "pww",
local: true,
},
},
{
Expand All @@ -224,6 +226,7 @@ func TestLoadConfigration(t *testing.T) {
LocalRoot: "/ddd",
Username: "mmm",
Password: "pww",
local: true,
},
},
{
Expand All @@ -245,7 +248,6 @@ func TestLoadConfigration(t *testing.T) {
Owner: "sample1",
},
},

{
name: "use system environment and system environment has priority over global conf",
envUsername: "mmm",
Expand All @@ -265,6 +267,7 @@ func TestLoadConfigration(t *testing.T) {
LocalRoot: "/ddd",
Username: "mmm",
Password: "pww",
local: true,
},
},
{
Expand All @@ -286,6 +289,7 @@ func TestLoadConfigration(t *testing.T) {
LocalRoot: "/ddd",
Username: "mmm",
Password: "pww",
local: true,
},
},
{
Expand All @@ -304,6 +308,7 @@ func TestLoadConfigration(t *testing.T) {
BlogID: "blog1.example.com",
LocalRoot: "/data",
Username: "blog1",
local: true,
},
},
{
Expand All @@ -320,13 +325,12 @@ func TestLoadConfigration(t *testing.T) {
LocalRoot: "/data",
Username: "mmm",
Password: "pww",
local: true,
},
},
{
name: "inherit default config, and no system environment",
envUsername: "",
envPassword: "",
localConf: nil,
name: "inherit default config, and no system environment",
localConf: nil,
globalConf: pstr(`---
default:
username: hoge
Expand All @@ -344,6 +348,25 @@ func TestLoadConfigration(t *testing.T) {
OmitDomain: pbool(false),
},
},
{
name: "config that are only global will have the local flag false",
localConf: pstr(`---
blog1.example.com:
local_root: /blog1`),
globalConf: pstr(`---
default:
username: hoge
local_root: /data
blog2.example.com:
local_root: /blog2`),
blogKey: "blog2.example.com",
expect: blogConfig{
BlogID: "blog2.example.com",
LocalRoot: "/blog2",
Username: "hoge",
local: false,
},
},
}

for _, tc := range testCases {
Expand Down
42 changes: 24 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,40 @@ var commandPull = &cli.Command{
&cli.BoolFlag{Name: "only-drafts"},
},
Action: func(c *cli.Context) error {
blog := c.Args().First()
if blog == "" {
cli.ShowCommandHelp(c, "pull")
return errCommandHelp
}

conf, err := loadConfiguration()
if err != nil {
return err
}
blogConfig := conf.Get(blog)
if blogConfig == nil {
return fmt.Errorf("blog not found: %s", blog)
}

b := newBroker(blogConfig)
remoteEntries, err := b.FetchRemoteEntries(
!c.Bool("only-drafts"), !c.Bool("no-drafts"))
if err != nil {
return err
blogs := c.Args().Slice()
if len(blogs) == 0 {
blogs = conf.localBlogIDs()
}
if len(blogs) == 0 {
cli.ShowCommandHelp(c, "pull")
return errCommandHelp
}

for _, re := range remoteEntries {
path := b.LocalPath(re)
_, err := b.StoreFresh(re, path)
for _, blog := range blogs {
blogConfig := conf.Get(blog)
if blogConfig == nil {
return fmt.Errorf("blog not found: %s", blog)
}

b := newBroker(blogConfig)
remoteEntries, err := b.FetchRemoteEntries(
!c.Bool("only-drafts"), !c.Bool("no-drafts"))
if err != nil {
return err
}

for _, re := range remoteEntries {
path := b.LocalPath(re)
_, err := b.StoreFresh(re, path)
if err != nil {
return err
}
}
}
return nil
},
Expand Down
Loading