Skip to content

Commit

Permalink
Merge pull request #5 from calvinmclean/fix/cover-image
Browse files Browse the repository at this point in the history
Improve logs for update and save CoverImage
  • Loading branch information
calvinmclean authored Nov 19, 2023
2 parents 96d0bc4 + 2e4deaa commit ee465f4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ vendor/
articles/
comment.md
article-sync
.DS_STORE
19 changes: 10 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Article struct {
Description string `json:"description"`
URL string `json:"url"`
Tags []string `json:"tags"`
CoverImage string `json:"cover_image"`

Gopher string `json:"gopher"`

Expand Down Expand Up @@ -323,11 +324,11 @@ func (c *client) syncArticleFromDirectory(dir string) (*Article, error) {
if err != nil {
return nil, fmt.Errorf("error checking if article needs update: %w", err)
}
if !shouldUpdate {
if shouldUpdate == "" {
logger.Info("article is up-to-date")
return article, nil
}
logger.Info("updating article with new body")
logger.With("reason", shouldUpdate).Info("updating article")
article.updated = true

if c.dryRun {
Expand Down Expand Up @@ -374,24 +375,24 @@ func writeArticleFile(path string, article *Article) error {
return nil
}

func (c *client) shouldUpdateArticle(markdownBody string, article *Article) (bool, error) {
func (c *client) shouldUpdateArticle(markdownBody string, article *Article) (string, error) {
articleData, err := c.getArticle(article.ID)
if err != nil {
return false, fmt.Errorf("error getting article: %w", err)
return "", fmt.Errorf("error getting article: %w", err)
}

articleMarkdown, ok := articleData["body_markdown"].(string)
if !ok {
return false, fmt.Errorf("error checking body_markdown")
return "", fmt.Errorf("error checking body_markdown")
}

article.URL, ok = articleData["url"].(string)
if !ok {
return false, fmt.Errorf("error getting article url")
return "", fmt.Errorf("error getting article url")
}

if articleMarkdown != markdownBody {
return true, nil
return "body changed", nil
}

articleTags := articleData["tags"].([]interface{})
Expand All @@ -401,8 +402,8 @@ func (c *client) shouldUpdateArticle(markdownBody string, article *Article) (boo
}

if !slices.Equal[[]string, string](existingTags, article.Tags) {
return true, nil
return "different tags", nil
}

return false, nil
return "", nil
}
1 change: 1 addition & 0 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (c *client) updateArticle(dir string, article *Article, markdownBody string
BodyMarkdown: &markdownBody,
Published: &published,
Tags: &article.Tags,
MainImage: &article.CoverImage,
}

resp, err := doWithRetry(func() (*api.UpdateArticleResponse, error) {
Expand Down

0 comments on commit ee465f4

Please sign in to comment.