Bläddra i källkod

Merge pull request #5 from writeas/develop

List local posts
tags/v0.2
Matt Baer 9 år sedan
förälder
incheckning
af5d6621b2
4 ändrade filer med 98 tillägg och 1 borttagningar
  1. +5
    -1
      README.md
  2. +21
    -0
      utils/fileutils.go
  3. +49
    -0
      writeas/cli.go
  4. +23
    -0
      writeas/posts.go

+ 5
- 1
README.md Visa fil

@@ -2,7 +2,9 @@ writeas-cli
===========
Command line interface for [Write.as](https://write.as) and [Write.as on Tor](http://writeas7pm7rcdqg.onion/). Works on Windows, OS X, and Linux.

Like the [Android app](https://play.google.com/store/apps/details?id=com.abunchtell.writeas), the command line client keeps track of the posts you make, so future editing / deleting is easier than [doing it with cURL](http://cmd.write.as/). It is currently **ALPHA**, so only basic functionality is available. But the goal is for this to hold the logic behind any future GUI app we build for the desktop.
Like the [Android app](https://play.google.com/store/apps/details?id=com.abunchtell.writeas), the command line client keeps track of the posts you make, so future editing / deleting is easier than [doing it with cURL](http://cmd.write.as/). The goal is for this to serve as the backend for any future GUI app we build for the desktop.

It is currently **alpha**, so a) functionality is basic and b) everything is subject to change — i.e., watch the [changelog](https://write.as/changelog-cli.html).

## Usage

@@ -13,6 +15,8 @@ COMMANDS:
post Alias for default action: create post from stdin
delete Delete a post
get Read a raw post
add Add a post locally
list List local posts
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:


+ 21
- 0
utils/fileutils.go Visa fil

@@ -30,6 +30,27 @@ func WriteData(path string, data []byte) {
}
}

func ReadData(p string) *[]string {
f, err := os.Open(p)
if err != nil {
return nil
}
defer f.Close()

lines := []string{}

scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

if err := scanner.Err(); err != nil {
return nil
}

return &lines
}

func RemoveLine(p, startsWith string) {
f, err := os.Open(p)
if err != nil {


+ 49
- 0
writeas/cli.go Visa fil

@@ -97,6 +97,26 @@ func main() {
},
},
},
{
Name: "add",
Usage: "Add a post locally",
Action: cmdAdd,
},
{
Name: "list",
Usage: "List local posts",
Action: cmdList,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "id",
Usage: "Show list with post IDs (default)",
},
cli.BoolFlag{
Name: "url",
Usage: "Show list with URLs",
},
},
},
}

app.Run(os.Args)
@@ -202,6 +222,35 @@ func cmdGet(c *cli.Context) {
DoFetch(friendlyId, tor)
}

func cmdAdd(c *cli.Context) {
friendlyId := c.Args().Get(0)
token := c.Args().Get(1)
if friendlyId == "" || token == "" {
fmt.Println("usage: writeas add <postId> <token>")
os.Exit(1)
}

addPost(friendlyId, token)
}

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

var p Post
posts := getPosts()
for i := range *posts {
p = (*posts)[len(*posts)-1-i]
if ids || !urls {
fmt.Printf("%s ", p.ID)
}
if urls {
fmt.Printf("https://write.as/%s ", p.ID)
}
fmt.Print("\n")
}
}

func client(read, tor bool, path, query string) (string, *http.Client) {
var u *url.URL
var client *http.Client


+ 23
- 0
writeas/posts.go Visa fil

@@ -13,6 +13,11 @@ const (
SEPARATOR = `|`
)

type Post struct {
ID string
EditToken string
}

func userDataDir() string {
return filepath.Join(parentDataDir(), DATA_DIR_NAME)
}
@@ -63,3 +68,21 @@ func tokenFromID(id string) string {
func removePost(id string) {
fileutils.RemoveLine(filepath.Join(userDataDir(), POSTS_FILE), id)
}

func getPosts() *[]Post {
lines := fileutils.ReadData(filepath.Join(userDataDir(), POSTS_FILE))

posts := []Post{}
parts := make([]string, 2)

for _, l := range *lines {
parts = strings.Split(l, SEPARATOR)
if len(parts) < 2 {
continue
}
posts = append(posts, Post{ID: parts[0], EditToken: parts[1]})
}

return &posts

}

Laddar…
Avbryt
Spara