Merge pull request #327 from Kenix3/add_user_supports_envvar

Add user now supports reading password from an env var.
pull/335/head
Philipp C. Heckel 2022-06-20 15:40:23 -04:00 committed by GitHub
commit 5b68915fff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 4 deletions

View File

@ -6,11 +6,12 @@ import (
"crypto/subtle" "crypto/subtle"
"errors" "errors"
"fmt" "fmt"
"strings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc" "github.com/urfave/cli/v2/altsrc"
"heckel.io/ntfy/auth" "heckel.io/ntfy/auth"
"heckel.io/ntfy/util" "heckel.io/ntfy/util"
"strings"
) )
func init() { func init() {
@ -40,6 +41,7 @@ var cmdUser = &cli.Command{
Action: execUserAdd, Action: execUserAdd,
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{Name: "role", Aliases: []string{"r"}, Value: string(auth.RoleUser), Usage: "user role"}, &cli.StringFlag{Name: "role", Aliases: []string{"r"}, Value: string(auth.RoleUser), Usage: "user role"},
&cli.StringFlag{Name: "password", Aliases: []string{"p"}, EnvVars: []string{"NTFY_PASSWORD"}, Usage: "user password"},
}, },
Description: `Add a new user to the ntfy user database. Description: `Add a new user to the ntfy user database.
@ -137,6 +139,7 @@ Examples:
func execUserAdd(c *cli.Context) error { func execUserAdd(c *cli.Context) error {
username := c.Args().Get(0) username := c.Args().Get(0)
role := auth.Role(c.String("role")) role := auth.Role(c.String("role"))
password := c.String("password")
if username == "" { if username == "" {
return errors.New("username expected, type 'ntfy user add --help' for help") return errors.New("username expected, type 'ntfy user add --help' for help")
} else if username == userEveryone { } else if username == userEveryone {
@ -151,9 +154,14 @@ func execUserAdd(c *cli.Context) error {
if user, _ := manager.User(username); user != nil { if user, _ := manager.User(username); user != nil {
return fmt.Errorf("user %s already exists", username) return fmt.Errorf("user %s already exists", username)
} }
password, err := readPasswordAndConfirm(c) // If the password env var was not set, read it from stdin
if err != nil { if password == "" {
return err p, err := readPasswordAndConfirm(c)
if err != nil {
return err
}
password = p
} }
if err := manager.AddUser(username, password, role); err != nil { if err := manager.AddUser(username, password, role); err != nil {
return err return err