Kaynağa Gözat

Add ability to update posts

Finishes issue #1
tags/v0.2
Matt Baer 9 yıl önce
ebeveyn
işleme
f3124a7222
1 değiştirilmiş dosya ile 74 ekleme ve 0 silme
  1. +74
    -0
      writeas/cli.go

+ 74
- 0
writeas/cli.go Dosyayı Görüntüle

@@ -82,6 +82,22 @@ func main() {
},
},
{
Name: "update",
Usage: "Update (overwrite) a post",
Action: cmdUpdate,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "tor, t",
Usage: "Update via Tor hidden service",
},
cli.IntFlag{
Name: "tor-port",
Usage: "Use a different port to connect to Tor",
Value: 9150,
},
},
},
{
Name: "get",
Usage: "Read a raw post",
Action: cmdGet,
@@ -207,6 +223,35 @@ func cmdDelete(c *cli.Context) {
DoDelete(friendlyId, token, tor)
}

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

if token == "" {
// Search for the token locally
token = tokenFromID(friendlyId)
if token == "" {
fmt.Println("Couldn't find an edit token locally. Did you create this post here?")
fmt.Printf("If you have an edit token, use: writeas update %s <token>\n", friendlyId)
os.Exit(1)
}
}

// Read post body
fullPost := readStdIn()

tor := c.Bool("tor") || c.Bool("t")
if c.Int("tor-port") != 0 {
torPort = c.Int("tor-port")
}

DoUpdate(fullPost, friendlyId, token, tor)
}

func cmdGet(c *cli.Context) {
friendlyId := c.Args().Get(0)
if friendlyId == "" {
@@ -340,6 +385,35 @@ func DoPost(post []byte, encrypt, tor bool) {
}
}

func DoUpdate(post []byte, friendlyId, token string, tor bool) {
urlStr, client := client(false, tor, "", fmt.Sprintf("id=%s&t=%s", friendlyId, token))

data := url.Values{}
data.Set("w", string(post))

r, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(data.Encode()))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))

resp, err := client.Do(r)
check(err)
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
if tor {
fmt.Println("Post updated via hidden service.")
} else {
fmt.Println("Post updated.")
}
} else {
if DEBUG {
fmt.Printf("Problem updating: %s\n", resp.Status)
} else {
fmt.Printf("Post doesn't exist, or bad edit token given.\n")
}
}
}

func DoDelete(friendlyId, token string, tor bool) {
urlStr, client := client(false, tor, "", fmt.Sprintf("id=%s&t=%s", friendlyId, token))



Yükleniyor…
İptal
Kaydet