Skip to content

Commit

Permalink
adjust originalPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Oct 30, 2023
1 parent 7f4225d commit 4529e53
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 72 deletions.
19 changes: 10 additions & 9 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ func (b *broker) FetchRemoteEntries(published, drafts bool) ([]*entry, error) {
const entryExt = ".md" // TODO regard re.ContentType

func (b *broker) LocalPath(e *entry) string {
if e.localPath != "" {
return e.localPath
}
if e.URL == nil {
return ""
}
localPath := e.URL.Path

if e.IsDraft && strings.Contains(e.EditURL, "/atom/entry/") {
Expand All @@ -95,9 +101,11 @@ func (b *broker) LocalPath(e *entry) string {
return ""
}
if isGivenPath(entryPath) {
// EditURL is like bellow
// https://blog.hatena.ne.jp/Songmu/songmu.hatenadiary.org/atom/entry/6801883189050452361
paths := strings.Split(e.EditURL, "/")
if len(paths) == 8 {
localPath = subdir + "/entry/" + draftDir + entryPath
localPath = subdir + "/entry/" + draftDir + paths[7] // path[7] is entryID
}
}
}
Expand Down Expand Up @@ -170,7 +178,7 @@ func (b *broker) PutEntry(e *entry) error {
if err != nil {
return err
}
return b.Store(newEntry, b.LocalPath(newEntry), b.originalPath(e))
return b.Store(newEntry, b.LocalPath(newEntry), b.LocalPath(e))
}

func (b *broker) PostEntry(e *entry, isPage bool) error {
Expand All @@ -187,13 +195,6 @@ func (b *broker) PostEntry(e *entry, isPage bool) error {
return b.Store(newEntry, b.LocalPath(newEntry), "")
}

func (b *broker) originalPath(e *entry) string {
if e.URL == nil {
return ""
}
return b.LocalPath(e)
}

func atomEndpointURLRoot(bc *blogConfig) string {
owner := bc.Owner
if owner == "" {
Expand Down
63 changes: 0 additions & 63 deletions broker_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package main

import (
"net/url"
"runtime"
"testing"
"time"
)

func TestEntryEndPointUrl(t *testing.T) {
Expand Down Expand Up @@ -42,64 +39,4 @@ func TestEntryEndPointUrl(t *testing.T) {
}
}

func TestOriginalPath(t *testing.T) {
u, _ := url.Parse("http://hatenablog.example.com/2")
jst, _ := time.LoadLocation("Asia/Tokyo")
d := time.Date(2023, 10, 10, 0, 0, 0, 0, jst)

testCases := []struct {
name string
entry entry
expect string
expectWindows string
}{
{
name: "entry has URL",
entry: entry{
entryHeader: &entryHeader{
URL: &entryURL{u},
EditURL: u.String() + "/edit",
Title: "test",
Date: &d,
IsDraft: true,
},
LastModified: &d,
Content: "テスト",
},
expect: "example1.hatenablog.com/2.md",
expectWindows: "example1.hatenablog.com\\2.md",
},
{
name: "Not URL",
entry: entry{
entryHeader: &entryHeader{
EditURL: u.String() + "/edit",
Title: "hoge",
IsDraft: true,
},
LastModified: &d,
Content: "テスト",
},
expect: "",
expectWindows: "",
},
}

config := blogConfig{
BlogID: "example1.hatenablog.com",
Username: "sample1",
}
broker := newBroker(&config)

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := broker.originalPath(&tc.entry)
if runtime.GOOS == "windows" {
tc.expect = tc.expectWindows
}
if tc.expect != got {
t.Errorf("expect: %s, got: %s", tc.expect, got)
}
})
}
}
1 change: 1 addition & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type entry struct {
LastModified *time.Time
Content string
ContentType string
localPath string
}

func (e *entry) HeaderString() string {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ var commandPush = &cli.Command{
if err != nil {
return err
}
entry.localPath = path

if entry.EditURL == "" {
// post new entry
Expand Down

0 comments on commit 4529e53

Please sign in to comment.