WIP CLI
This commit is contained in:
parent
1e8421e8ce
commit
a1f513f6a5
9 changed files with 138 additions and 65 deletions
|
@ -36,7 +36,7 @@ func New() *cli.App {
|
|||
|
||||
func execMainApp(c *cli.Context) error {
|
||||
log.Printf("\x1b[1;33mDeprecation notice: Please run the server using 'ntfy serve'; see 'ntfy -h' for help.\x1b[0m")
|
||||
log.Printf("\x1b[1;33mThis way of running the server will be removed Feb 2022.\x1b[0m")
|
||||
log.Printf("\x1b[1;33mThis way of running the server will be removed March 2022. See https://ntfy.sh/docs/deprecations/ for details.\x1b[0m")
|
||||
return execServe(c)
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
var cmdPublish = &cli.Command{
|
||||
Name: "publish",
|
||||
Aliases: []string{"pub", "send"},
|
||||
Aliases: []string{"pub", "send", "push"},
|
||||
Usage: "Send message via a ntfy server",
|
||||
UsageText: "ntfy send [OPTIONS..] TOPIC MESSAGE",
|
||||
Action: execPublish,
|
||||
|
|
|
@ -21,6 +21,8 @@ var cmdSubscribe = &cli.Command{
|
|||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{Name: "exec", Aliases: []string{"e"}, Usage: "execute command for each message event"},
|
||||
&cli.StringFlag{Name: "since", Aliases: []string{"s"}, Usage: "return events since (Unix timestamp, or all)"},
|
||||
&cli.BoolFlag{Name: "poll", Aliases: []string{"p"}, Usage: "return events and exit, do not listen for new events"},
|
||||
&cli.BoolFlag{Name: "scheduled", Aliases: []string{"sched", "S"}, Usage: "also return scheduled/delayed events"},
|
||||
},
|
||||
Description: `(THIS COMMAND IS INCUBATING. IT MAY CHANGE WITHOUT NOTICE.)
|
||||
|
||||
|
@ -45,7 +47,8 @@ are passed to the command as environment variables:
|
|||
Examples:
|
||||
ntfy subscribe mytopic # Prints JSON for incoming messages to stdout
|
||||
ntfy sub home.lan/backups alerts # Subscribe to two different topics
|
||||
ntfy sub --exec='notify-send "$m"' mytopic # Execute command for incoming messages'
|
||||
ntfy sub --exec='notify-send "$m"' mytopic # Execute command for incoming messages
|
||||
ntfy sub --exec=/my/script topic1 topic2 # Subscribe to two topics and execute command for each message
|
||||
`,
|
||||
}
|
||||
|
||||
|
@ -56,11 +59,37 @@ func execSubscribe(c *cli.Context) error {
|
|||
log.Printf("\x1b[1;33mThis command is incubating. The interface may change without notice.\x1b[0m")
|
||||
cl := client.DefaultClient
|
||||
command := c.String("exec")
|
||||
for _, topic := range c.Args().Slice() {
|
||||
cl.Subscribe(expandTopicURL(topic))
|
||||
since := c.String("since")
|
||||
poll := c.Bool("poll")
|
||||
scheduled := c.Bool("scheduled")
|
||||
topics := c.Args().Slice()
|
||||
var options []client.SubscribeOption
|
||||
if since != "" {
|
||||
options = append(options, client.WithSince(since))
|
||||
}
|
||||
for m := range cl.Messages {
|
||||
_ = dispatchMessage(c, command, m)
|
||||
if poll {
|
||||
options = append(options, client.WithPoll())
|
||||
}
|
||||
if scheduled {
|
||||
options = append(options, client.WithScheduled())
|
||||
}
|
||||
if poll {
|
||||
for _, topic := range topics {
|
||||
messages, err := cl.Poll(expandTopicURL(topic), options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, m := range messages {
|
||||
_ = dispatchMessage(c, command, m)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, topic := range topics {
|
||||
cl.Subscribe(expandTopicURL(topic), options...)
|
||||
}
|
||||
for m := range cl.Messages {
|
||||
_ = dispatchMessage(c, command, m)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -77,11 +106,9 @@ func execCommand(c *cli.Context, command string, m *client.Message) error {
|
|||
if m.Event == client.OpenEvent {
|
||||
log.Printf("[%s] Connection opened, subscribed to topic", collapseTopicURL(m.TopicURL))
|
||||
} else if m.Event == client.MessageEvent {
|
||||
go func() {
|
||||
if err := runCommandInternal(c, command, m); err != nil {
|
||||
log.Printf("[%s] Command failed: %s", collapseTopicURL(m.TopicURL), err.Error())
|
||||
}
|
||||
}()
|
||||
if err := runCommandInternal(c, command, m); err != nil {
|
||||
log.Printf("[%s] Command failed: %s", collapseTopicURL(m.TopicURL), err.Error())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue