Skip to content

Commit af5d662

Browse files
committed
Merge pull request #5 from writeas/develop
List local posts
2 parents af91e12 + fa0cb9a commit af5d662

4 files changed

Lines changed: 98 additions & 1 deletion

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ writeas-cli
22
===========
33
Command line interface for [Write.as](https://write.as) and [Write.as on Tor](http://writeas7pm7rcdqg.onion/). Works on Windows, OS X, and Linux.
44

5-
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.
5+
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.
6+
7+
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).
68

79
## Usage
810

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

utils/fileutils.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ func WriteData(path string, data []byte) {
3030
}
3131
}
3232

33+
func ReadData(p string) *[]string {
34+
f, err := os.Open(p)
35+
if err != nil {
36+
return nil
37+
}
38+
defer f.Close()
39+
40+
lines := []string{}
41+
42+
scanner := bufio.NewScanner(f)
43+
for scanner.Scan() {
44+
lines = append(lines, scanner.Text())
45+
}
46+
47+
if err := scanner.Err(); err != nil {
48+
return nil
49+
}
50+
51+
return &lines
52+
}
53+
3354
func RemoveLine(p, startsWith string) {
3455
f, err := os.Open(p)
3556
if err != nil {

writeas/cli.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,26 @@ func main() {
9797
},
9898
},
9999
},
100+
{
101+
Name: "add",
102+
Usage: "Add a post locally",
103+
Action: cmdAdd,
104+
},
105+
{
106+
Name: "list",
107+
Usage: "List local posts",
108+
Action: cmdList,
109+
Flags: []cli.Flag{
110+
cli.BoolFlag{
111+
Name: "id",
112+
Usage: "Show list with post IDs (default)",
113+
},
114+
cli.BoolFlag{
115+
Name: "url",
116+
Usage: "Show list with URLs",
117+
},
118+
},
119+
},
100120
}
101121

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

225+
func cmdAdd(c *cli.Context) {
226+
friendlyId := c.Args().Get(0)
227+
token := c.Args().Get(1)
228+
if friendlyId == "" || token == "" {
229+
fmt.Println("usage: writeas add <postId> <token>")
230+
os.Exit(1)
231+
}
232+
233+
addPost(friendlyId, token)
234+
}
235+
236+
func cmdList(c *cli.Context) {
237+
urls := c.Bool("url")
238+
ids := c.Bool("id")
239+
240+
var p Post
241+
posts := getPosts()
242+
for i := range *posts {
243+
p = (*posts)[len(*posts)-1-i]
244+
if ids || !urls {
245+
fmt.Printf("%s ", p.ID)
246+
}
247+
if urls {
248+
fmt.Printf("https://write.as/%s ", p.ID)
249+
}
250+
fmt.Print("\n")
251+
}
252+
}
253+
205254
func client(read, tor bool, path, query string) (string, *http.Client) {
206255
var u *url.URL
207256
var client *http.Client

writeas/posts.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ const (
1313
SEPARATOR = `|`
1414
)
1515

16+
type Post struct {
17+
ID string
18+
EditToken string
19+
}
20+
1621
func userDataDir() string {
1722
return filepath.Join(parentDataDir(), DATA_DIR_NAME)
1823
}
@@ -63,3 +68,21 @@ func tokenFromID(id string) string {
6368
func removePost(id string) {
6469
fileutils.RemoveLine(filepath.Join(userDataDir(), POSTS_FILE), id)
6570
}
71+
72+
func getPosts() *[]Post {
73+
lines := fileutils.ReadData(filepath.Join(userDataDir(), POSTS_FILE))
74+
75+
posts := []Post{}
76+
parts := make([]string, 2)
77+
78+
for _, l := range *lines {
79+
parts = strings.Split(l, SEPARATOR)
80+
if len(parts) < 2 {
81+
continue
82+
}
83+
posts = append(posts, Post{ID: parts[0], EditToken: parts[1]})
84+
}
85+
86+
return &posts
87+
88+
}

0 commit comments

Comments
 (0)