Ver Fonte

teach `ntfy webpush` to write the keys to a file

Nogweii há 1 ano atrás
pai
commit
49a548252c
3 ficheiros alterados com 30 adições e 1 exclusões
  1. 1 0
      .gitignore
  2. 22 1
      cmd/webpush.go
  3. 7 0
      cmd/webpush_test.go

+ 1 - 0
.gitignore

@@ -15,3 +15,4 @@ node_modules/
 __pycache__
 __pycache__
 web/dev-dist/
 web/dev-dist/
 venv/
 venv/
+cmd/key-file.yaml

+ 22 - 1
cmd/webpush.go

@@ -4,9 +4,16 @@ package cmd
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"os"
 
 
 	"github.com/SherClockHolmes/webpush-go"
 	"github.com/SherClockHolmes/webpush-go"
 	"github.com/urfave/cli/v2"
 	"github.com/urfave/cli/v2"
+	"github.com/urfave/cli/v2/altsrc"
+)
+
+var flagsWebpush = append(
+	[]cli.Flag{},
+	altsrc.NewStringFlag(&cli.StringFlag{Name: "key-file", Aliases: []string{"f"}, Usage: "write vapid keys to this file"}),
 )
 )
 
 
 func init() {
 func init() {
@@ -26,6 +33,7 @@ var cmdWebPush = &cli.Command{
 			Usage:     "Generate VAPID keys to enable browser background push notifications",
 			Usage:     "Generate VAPID keys to enable browser background push notifications",
 			UsageText: "ntfy webpush keys",
 			UsageText: "ntfy webpush keys",
 			Category:  categoryServer,
 			Category:  categoryServer,
+			Flags:     flagsWebpush,
 		},
 		},
 	},
 	},
 }
 }
@@ -35,7 +43,19 @@ func generateWebPushKeys(c *cli.Context) error {
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	_, err = fmt.Fprintf(c.App.ErrWriter, `Web Push keys generated. Add the following lines to your config file:
+
+	if keyFile := c.String("key-file"); keyFile != "" {
+		contents := fmt.Sprintf(`---
+web-push-public-key: %s
+web-push-private-key: %s
+`, publicKey, privateKey)
+		err = os.WriteFile(keyFile, []byte(contents), 0660)
+		if err != nil {
+			return err
+		}
+		_, err = fmt.Fprintf(c.App.ErrWriter, `Web Push keys written to %s.`, keyFile)
+	} else {
+		_, err = fmt.Fprintf(c.App.ErrWriter, `Web Push keys generated. Add the following lines to your config file:
 
 
 web-push-public-key: %s
 web-push-public-key: %s
 web-push-private-key: %s
 web-push-private-key: %s
@@ -44,5 +64,6 @@ web-push-email-address: <email address>
 
 
 See https://ntfy.sh/docs/config/#web-push for details.
 See https://ntfy.sh/docs/config/#web-push for details.
 `, publicKey, privateKey)
 `, publicKey, privateKey)
+	}
 	return err
 	return err
 }
 }

+ 7 - 0
cmd/webpush_test.go

@@ -14,6 +14,13 @@ func TestCLI_WebPush_GenerateKeys(t *testing.T) {
 	require.Contains(t, stderr.String(), "Web Push keys generated.")
 	require.Contains(t, stderr.String(), "Web Push keys generated.")
 }
 }
 
 
+func TestCLI_WebPush_WriteKeysToFile(t *testing.T) {
+	app, _, _, stderr := newTestApp()
+	require.Nil(t, runWebPushCommand(app, server.NewConfig(), "keys", "--key-file=key-file.yaml"))
+	require.Contains(t, stderr.String(), "Web Push keys written to key-file.yaml")
+	require.FileExists(t, "key-file.yaml")
+}
+
 func runWebPushCommand(app *cli.App, conf *server.Config, args ...string) error {
 func runWebPushCommand(app *cli.App, conf *server.Config, args ...string) error {
 	webPushArgs := []string{
 	webPushArgs := []string{
 		"ntfy",
 		"ntfy",