Skip to content

Commit

Permalink
Merge pull request #136 from x-motemen/from-file
Browse files Browse the repository at this point in the history
define func entryFromFile
  • Loading branch information
Songmu authored Nov 4, 2023
2 parents 80bd45b + 57e03c0 commit e5945cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
8 changes: 6 additions & 2 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,16 @@ func (b *broker) PostEntry(e *entry, isPage bool) error {
return b.Store(newEntry, b.LocalPath(newEntry), "")
}

func (b *broker) RemoveEntry(e *entry, p string) error {
func (b *broker) RemoveEntry(e *entry) error {
err := b.Client.DeleteEntry(e.EditURL)
if err != nil {
return err
}
return os.Remove(p)
p := b.LocalPath(e)
if _, err := os.Stat(p); err == nil {
return os.Remove(p)
}
return nil
}

func atomEndpointURLRoot(bc *blogConfig) string {
Expand Down
15 changes: 15 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,21 @@ func entryFromReader(source io.Reader) (*entry, error) {
return entry, nil
}

func entryFromFile(fpath string) (*entry, error) {
f, err := os.Open(fpath)
if err != nil {
return nil, err
}
defer f.Close()

e, err := entryFromReader(f)
if err != nil {
return nil, err
}
e.localPath = fpath
return e, nil
}

func asEntry(atomEntry *atom.Entry, err error) (*entry, error) {
if err != nil {
return nil, err
Expand Down
20 changes: 3 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,7 @@ var commandPush = &cli.Command{
return err
}
}

f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

entry, err := entryFromReader(f)
entry, err := entryFromFile(path)
if err != nil {
return err
}
Expand All @@ -248,7 +241,6 @@ var commandPush = &cli.Command{
ti := time.Now()
entry.LastModified = &ti
}
entry.localPath = path

if entry.EditURL == "" {
// post new entry
Expand Down Expand Up @@ -406,13 +398,7 @@ var commandRemove = &cli.Command{
}

for _, path := range c.Args().Slice() {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()

entry, err := entryFromReader(f)
entry, err := entryFromFile(path)
if err != nil {
return err
}
Expand All @@ -427,7 +413,7 @@ var commandRemove = &cli.Command{
return fmt.Errorf("cannot find blog for %s", path)
}

err = newBroker(bc, c.App.Writer).RemoveEntry(entry, path)
err = newBroker(bc, c.App.Writer).RemoveEntry(entry)
if err != nil {
return err
}
Expand Down

0 comments on commit e5945cd

Please sign in to comment.