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

ref T611 detailed post list #37

Open
wants to merge 1 commit into
base: version-two
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions cmd/writeas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func main() {
Name: "id",
Usage: "Show list with post IDs (default)",
},
cli.BoolFlag{
Name: "d",
Usage: "Show detailed view of posts",
},
cli.BoolFlag{
Name: "md",
Usage: "Use with --url to return URLs with Markdown enabled",
Expand Down
49 changes: 4 additions & 45 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,53 +185,12 @@ func CmdAdd(c *cli.Context) error {
}

func CmdListPosts(c *cli.Context) error {
urls := c.Bool("url")
ids := c.Bool("id")

var p api.Post
posts := api.GetPosts(c)
tw := tabwriter.NewWriter(os.Stdout, 10, 0, 2, ' ', tabwriter.TabIndent)
numPosts := len(*posts)
if ids || !urls && numPosts != 0 {
fmt.Fprintf(tw, "Local\t%s\t%s\t\n", "ID", "Token")
} else if numPosts != 0 {
fmt.Fprintf(tw, "Local\t%s\t%s\t\n", "URL", "Token")
} else {
fmt.Fprintf(tw, "No local posts found\n")
}
for i := range *posts {
p = (*posts)[numPosts-1-i]
if ids || !urls {
fmt.Fprintf(tw, "unsynced\t%s\t%s\t\n", p.ID, p.EditToken)
} else {
fmt.Fprintf(tw, "unsynced\t%s\t%s\t\n", getPostURL(c, p.ID), p.EditToken)
}
}
u, _ := config.LoadUser(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
if u != nil {
remotePosts, err := api.GetUserPosts(c)
if err != nil {
fmt.Println(err)
}

if len(remotePosts) > 0 {
identifier := "URL"
if ids || !urls {
identifier = "ID"
}
fmt.Fprintf(tw, "\nAccount\t%s\t%s\t\n", identifier, "Title")
}
for _, p := range remotePosts {
identifier := getPostURL(c, p.ID)
if ids || !urls {
identifier = p.ID
}
synced := "unsynced"
if p.Synced {
synced = "synced"
}
fmt.Fprintf(tw, "%s\t%s\t%s\t\n", synced, identifier, p.Title)
}
printLocalPosts(c, tw, posts)
err := printRemotePosts(c, tw)
if err != nil {
log.Errorln("Could not print remote posts: %v", err)
}
return tw.Flush()
}
Expand Down
101 changes: 101 additions & 0 deletions commands/list_posts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package commands

import (
"fmt"
"text/tabwriter"
"time"

"github.com/writeas/writeas-cli/api"
"github.com/writeas/writeas-cli/config"
cli "gopkg.in/urfave/cli.v1"
)

const (
dashBar = "--------------------------------------------------------------------------------"
)

func printLocalPosts(c *cli.Context, tw *tabwriter.Writer, posts *[]api.Post) {
ids := c.Bool("id")
urls := c.Bool("url")
numPosts := len(*posts)
if ids || !urls && numPosts != 0 {
fmt.Fprintf(tw, "Local\t%s\t%s\t\n", "ID", "Token")
} else if numPosts != 0 {
fmt.Fprintf(tw, "Local\t%s\t%s\t\n", "URL", "Token")
} else {
fmt.Fprintf(tw, "No local posts found\n")
}
for i := range *posts {
p := (*posts)[numPosts-1-i]
if ids || urls {
fmt.Fprintf(tw, "unsynced\t%s\t%s\n", p.ID, p.EditToken)
} else {
fmt.Fprintf(tw, "unsynced\t%s\t%s\n", getPostURL(c, p.ID), p.EditToken)
}
}
}

func printRemotePosts(c *cli.Context, tw *tabwriter.Writer) error {
ids := c.Bool("id")
urls := c.Bool("url")
details := c.Bool("d")
u, _ := config.LoadUser(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
if u != nil {
remotePosts, err := api.GetUserPosts(c)
if err != nil {
return err
}

if len(remotePosts) > 0 {
identifier := "URL"
if ids || !urls {
identifier = "ID"
}
if !details {
fmt.Fprintf(tw, "\nAccount\t%s\t%s\n", identifier, "Title")
}
}
for _, p := range remotePosts {
identifier := getPostURL(c, p.ID)
if ids || !urls {
identifier = p.ID
}
if details {
slug := p.ID
if p.Slug != "" {
slug = p.Slug
if p.Collection != "" {
slug = p.Collection + "/" + slug
}
}
url := getPostURL(c, slug)
if p.Slug == "" {
p.Slug = "no-slug"
}
if p.Collection == "" {
p.Collection = "no-blog"
}
fmt.Fprintf(tw, "\n%s\t%s\t%s\n", "Title: ", p.Title, " ")
fmt.Fprintf(tw, "%s\t%s\t%s\n", "Last Updated: ", prettyDate(p.Updated), "")
fmt.Fprintf(tw, "%s\t%s\t%s\n", "Blog: ", p.Collection, " ")
fmt.Fprintf(tw, "%s\t%s\t%s\n", "Slug/ID: ", p.Slug+" / "+p.ID, " ")
fmt.Fprintf(tw, "%s\t%s\t%s\n\n", "URL: ", url, " ")
}
synced := "unsynced"
if p.Synced {
synced = "synced"
}
if details {
fmt.Fprintln(tw, p.Excerpt)
fmt.Fprintln(tw, dashBar)
} else {
fmt.Fprintf(tw, "%s\t%s\t%s\n", synced, identifier, p.Title)
}
}
}
return nil
}

func prettyDate(date time.Time) string {
return date.Local().Format(time.RFC822)
}