mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-01-07 17:35:53 +01:00
teach ntfy webpush
to write the keys to a file
This commit is contained in:
parent
9d3fc20e58
commit
49a548252c
3 changed files with 30 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -15,3 +15,4 @@ node_modules/
|
||||||
__pycache__
|
__pycache__
|
||||||
web/dev-dist/
|
web/dev-dist/
|
||||||
venv/
|
venv/
|
||||||
|
cmd/key-file.yaml
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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",
|
||||||
|
|
Loading…
Reference in a new issue