add delete command

This commit is contained in:
Yasuhiro Matsumoto 2017-04-20 02:04:20 +09:00
parent 293a9e0aba
commit 1a101faaae
4 changed files with 76 additions and 0 deletions

28
cmd/mstdn/cmd_delete.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"context"
"errors"
"strconv"
"github.com/mattn/go-mastodon"
"github.com/urfave/cli"
)
func cmdDelete(c *cli.Context) error {
client := c.App.Metadata["client"].(*mastodon.Client)
if !c.Args().Present() {
return errors.New("arguments required")
}
for i := 0; i < c.NArg(); i++ {
id, err := strconv.ParseInt(c.Args().Get(i), 10, 64)
if err != nil {
return err
}
err = client.DeleteStatus(context.Background(), id)
if err != nil {
return err
}
}
return nil
}

View File

@ -0,0 +1,41 @@
package main
import (
"fmt"
"net/http"
"testing"
"github.com/urfave/cli"
)
func TestCmdDelete(t *testing.T) {
ok := false
f := func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/statuses/123":
fmt.Fprintln(w, `{}`)
ok = true
return
}
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
testWithServer(
f, func(app *cli.App) {
app.Run([]string{"mstdn", "delete", "122"})
},
)
if ok {
t.Fatal("something wrong to sequence to follow account")
}
ok = false
testWithServer(
f, func(app *cli.App) {
app.Run([]string{"mstdn", "delete", "123"})
},
)
if !ok {
t.Fatal("something wrong to sequence to follow account")
}
}

View File

@ -13,6 +13,8 @@ func testWithServer(h http.HandlerFunc, testFunc func(*cli.App)) string {
ts := httptest.NewServer(h)
defer ts.Close()
cli.OsExiter = func(n int) {}
client := mastodon.NewClient(&mastodon.Config{
Server: ts.URL,
ClientID: "foo",

View File

@ -257,6 +257,11 @@ func makeApp() *cli.App {
Usage: "upload file",
Action: cmdUpload,
},
{
Name: "delete",
Usage: "delete status",
Action: cmdDelete,
},
}
app.Setup()
return app