use real cli flags for email & password args

This commit is contained in:
Danny 2018-05-23 15:39:55 +02:00
parent a152331ca8
commit f1b2ba1775
4 changed files with 30 additions and 14 deletions

View File

@ -16,7 +16,7 @@ For getting a development version of Fathom up & running, please go through the
1. Ensure you have [Golang](https://golang.org/doc/install#install) installed properly 1. Ensure you have [Golang](https://golang.org/doc/install#install) installed properly
1. Get code: `git clone git@github.com:usefathom/fathom.git $GOPATH/src/github.com/usefathom/fathom` 1. Get code: `git clone git@github.com:usefathom/fathom.git $GOPATH/src/github.com/usefathom/fathom`
1. Compile into binary & prepare assets: `make all` 1. Compile into binary & prepare assets: `make all`
1. Register your user account: `fathom register <email> <password>` 1. Register your user account: `fathom register --email=<email> --password=<password>`
1. Start the webserver: `fathom server` and then visit **http://localhost:8080** to access your analytics dashboard. 1. Start the webserver: `fathom server` and then visit **http://localhost:8080** to access your analytics dashboard.
To start tracking, include the following JavaScript on your site and replace `yourfathom.com` with the URL to your Fathom instance (twice). To start tracking, include the following JavaScript on your site and replace `yourfathom.com` with the URL to your Fathom instance (twice).

View File

@ -56,11 +56,20 @@ func main() {
}, },
}, },
{ {
Name: "register", Name: "register",
Aliases: []string{"r"}, Aliases: []string{"r"},
Usage: "register a new admin user", Usage: "register a new admin user",
ArgsUsage: "<email> <password>", Action: register,
Action: register, Flags: []cli.Flag{
cli.StringFlag{
Name: "email, e",
Usage: "user email",
},
cli.StringFlag{
Name: "password, p",
Usage: "user password",
},
},
}, },
} }

View File

@ -1,6 +1,9 @@
package main package main
import ( import (
"errors"
"fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
"github.com/usefathom/fathom/pkg/models" "github.com/usefathom/fathom/pkg/models"
@ -8,21 +11,25 @@ import (
) )
func register(c *cli.Context) error { func register(c *cli.Context) error {
if c.NArg() < 2 { email := c.String("email")
cli.ShowSubcommandHelp(c) if email == "" {
return nil return errors.New("invalid args: missing email address")
} }
hash, _ := bcrypt.GenerateFromPassword([]byte(c.String("password")), 10) password := c.String("password")
if password == "" {
return errors.New("invalid args: missing password")
}
hash, _ := bcrypt.GenerateFromPassword([]byte(password), 10)
user := &models.User{ user := &models.User{
Email: c.String("email"), Email: email,
Password: string(hash), Password: string(hash),
} }
err := app.database.SaveUser(user) err := app.database.SaveUser(user)
if err != nil { if err != nil {
log.Errorf("error creating user: %s", err) return fmt.Errorf("error creating user: %s", err)
return err
} }
log.Infof("created user %s", user.Email) log.Infof("created user %s", user.Email)

View File

@ -109,7 +109,7 @@ function new_fathom_user() {
echo "Create user account: " echo "Create user account: "
read -p " Email address: " USER_EMAIL read -p " Email address: " USER_EMAIL
read -p " Password: " USER_PASSWORD read -p " Password: " USER_PASSWORD
fathom --config="$SITE_DIR_ABS/config.env" register "$USER_EMAIL" "$USER_PASSWORD" fathom --config="$SITE_DIR_ABS/config.env" register --email="$USER_EMAIL" --password="$USER_PASSWORD"
echo "" echo ""
} }