Bläddra i källkod

Require authentication for post operations in wf

Unlike Write.as, WriteFreely doesn't support anonymous (unauthenticated)
publishing. With these changes, we reflect that in wf-cli by wrapping
creating, updating, and deleting posts in a func that checks
authentication state.

Ref T586
pull/36/head
Matt Baer 4 år sedan
förälder
incheckning
b55874c6e6
2 ändrade filer med 29 tillägg och 6 borttagningar
  1. +23
    -0
      cmd/wf/commands.go
  2. +6
    -6
      cmd/wf/main.go

+ 23
- 0
cmd/wf/commands.go Visa fil

@@ -0,0 +1,23 @@
package main

import (
"fmt"

"github.com/writeas/writeas-cli/config"
"github.com/writeas/writeas-cli/executable"
cli "gopkg.in/urfave/cli.v1"
)

func requireAuth(f cli.ActionFunc, action string) cli.ActionFunc {
return func(c *cli.Context) error {
u, err := config.LoadUser(c)
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 "+action+".\nLog in first with: "+executable.Name()+" auth <username>", 1)
}

return f(c)
}
}

+ 6
- 6
cmd/wf/main.go Visa fil

@@ -34,13 +34,13 @@ func main() {
app.ExtraInfo = func() map[string]string {
return appInfo
}
app.Action = commands.CmdPost
app.Action = requireAuth(commands.CmdPost, "publish")
app.Flags = append(config.PostFlags, flags...)
app.Commands = []cli.Command{
{
Name: "post",
Usage: "Alias for default action: create post from stdin",
Action: commands.CmdPost,
Action: requireAuth(commands.CmdPost, "publish"),
Flags: config.PostFlags,
Description: `Create a new post on WriteFreely from stdin.

@@ -67,19 +67,19 @@ func main() {
If posting fails for any reason, 'wf' will show you the temporary file
location and how to pipe it to 'wf' to retry.`,
Action: commands.CmdNew,
Action: requireAuth(commands.CmdNew, "publish"),
Flags: config.PostFlags,
},
{
Name: "publish",
Usage: "Publish a file",
Action: commands.CmdPublish,
Action: requireAuth(commands.CmdPublish, "publish"),
Flags: config.PostFlags,
},
{
Name: "delete",
Usage: "Delete a post",
Action: commands.CmdDelete,
Action: requireAuth(commands.CmdDelete, "delete"),
Flags: []cli.Flag{
cli.BoolFlag{
Name: "tor, t",
@@ -99,7 +99,7 @@ func main() {
{
Name: "update",
Usage: "Update (overwrite) a post",
Action: commands.CmdUpdate,
Action: requireAuth(commands.CmdUpdate, "update"),
Flags: []cli.Flag{
cli.BoolFlag{
Name: "tor, t",


Laddar…
Avbryt
Spara