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

add collection listing #31

Merged
merged 4 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 27 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ func DoPost(c *cli.Context, post []byte, font string, encrypt, tor, code bool) (
return p, nil
}

// DoFetchCollections retrieves a list of the currently logged in users
// collections.
func DoFetchCollections(c *cli.Context) ([]RemoteColl, error) {
cl, err := NewClient(c, true)
if err != nil {
//
thebaer marked this conversation as resolved.
Show resolved Hide resolved
}

colls, err := cl.GetUserCollections()
if err != nil {
//
thebaer marked this conversation as resolved.
Show resolved Hide resolved
}

out := make([]RemoteColl, len(*colls))

for i, c := range *colls {
coll := RemoteColl{
Alias: c.Alias,
Title: c.Title,
URL: c.URL,
}
out[i] = coll
}

return out, nil
}

// DoUpdate updates the given post on Write.as.
func DoUpdate(c *cli.Context, post []byte, friendlyID, token, font string, tor, code bool) error {
cl, _ := NewClient(c, false)
Expand Down
9 changes: 9 additions & 0 deletions api/collections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package api

// RemoteColl represents a collection of posts
// It is a reduced set of data from a go-writeas Collection
type RemoteColl struct {
Alias string
Title string
URL string
}
23 changes: 6 additions & 17 deletions cmd/writeas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"os"

"github.com/writeas/writeas-cli/api"
"github.com/writeas/writeas-cli/commands"
"github.com/writeas/writeas-cli/config"
"github.com/writeas/writeas-cli/log"
Expand Down Expand Up @@ -170,24 +169,14 @@ func main() {
Usage: "Show list with URLs",
},
},
},
{
Name: "fetch",
Usage: "Fetch authenticated user's Write.as posts",
Action: api.CmdPull,
}, {
Name: "blogs",
Usage: "List blogs",
Action: commands.CmdCollections,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "tor, t",
Usage: "Authenticate via Tor hidden service",
},
cli.IntFlag{
Name: "tor-port",
Usage: "Use a different port to connect to Tor",
Value: 9150,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Make the operation more talkative",
Name: "url",
Usage: "Show list with URLs",
},
},
},
Expand Down
30 changes: 30 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,36 @@ func getPostURL(c *cli.Context, slug string) string {
return fmt.Sprintf("%s/%s%s", base, slug, ext)
}

func CmdCollections(c *cli.Context) error {
u, err := config.LoadUser(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
if err != nil {
return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1)
}
if u == nil {
return cli.NewExitError("You must be authenticated to view collections.\nLog in first with: writeas auth <username>", 1)
}
colls, err := api.DoFetchCollections(c)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Couldn't get collections for user %s: %v", u.User.Username, err), 1)
}
urls := c.Bool("url")
tw := tabwriter.NewWriter(os.Stdout, 8, 2, 0, ' ', tabwriter.TabIndent)
detail := "Title"
if urls {
detail = "URL"
}
fmt.Fprintf(tw, "%s\t%s\t\n", "Alias", detail)
for _, c := range colls {
dData := c.Title
if urls {
dData = c.URL
}
fmt.Fprintf(tw, "%s\t%s\t\n", c.Alias, dData)
}
tw.Flush()
return nil
}

func CmdAuth(c *cli.Context) error {
// Check configuration
u, err := config.LoadUser(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
Expand Down