Skip to content

Commit

Permalink
add remove subcommand to remove the entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Nov 3, 2023
1 parent 0ee0cda commit a81e079
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
6 changes: 6 additions & 0 deletions atom/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func entryBody(e *Entry) (*bytes.Buffer, error) {
return body, nil
}

// DeleteEntry removes the blog entry
func (c *Client) DeleteEntry(url string) error {
_, err := c.http("DELETE", url, nil)
return err
}

var blogsyncDebug = os.Getenv("BLOGSYNC_DEBUG") != ""

var debugLogger = sync.OnceValue(func() *slog.Logger {
Expand Down
8 changes: 8 additions & 0 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ 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 {
err := b.Client.DeleteEntry(e.EditURL)
if err != nil {
return err
}
return os.Remove(p)
}

func (b *broker) originalPath(e *entry) string {
if e.URL == nil {
return ""
Expand Down
47 changes: 47 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
commandPush,
commandPost,
commandList,
commandRemove,
}
app.Version = fmt.Sprintf("%s (%s)", version, revision)
err := app.Run(os.Args)
Expand Down Expand Up @@ -347,3 +348,49 @@ var commandList = &cli.Command{
return nil
},
}

var commandRemove = &cli.Command{
Name: "remove",
Usage: "Remove blog entries",
Action: func(c *cli.Context) error {
first := c.Args().First()
if first == "" {
cli.ShowCommandHelp(c, "remove")
return errCommandHelp
}

conf, err := loadConfiguration()
if err != nil {
return err
}

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

entry, err := entryFromReader(f)
if err != nil {
return err
}

blogID, err := entry.blogID()
if err != nil {
return err
}

bc := conf.Get(blogID)
if bc == nil {
return fmt.Errorf("cannot find blog for %s", path)
}

err = newBroker(bc).RemoveEntry(entry, path)
if err != nil {
return err
}
}
return nil
},
}

0 comments on commit a81e079

Please sign in to comment.