|
@@ -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
|
|
|
}
|
|
}
|