From 1226a7b70c8f1eb25d11a9a614be29c5890d0f1a Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Mon, 20 Jun 2022 10:56:45 -0400 Subject: [PATCH] ntfy publish --pid $PID ... --- cmd/publish.go | 21 +++++++++++++++++++++ cmd/publish_unix.go | 8 ++++++++ cmd/publish_windows.go | 10 ++++++++++ 3 files changed, 39 insertions(+) create mode 100644 cmd/publish_unix.go create mode 100644 cmd/publish_windows.go diff --git a/cmd/publish.go b/cmd/publish.go index c56aecad..f421f2e6 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -5,11 +5,13 @@ import ( "fmt" "github.com/urfave/cli/v2" "heckel.io/ntfy/client" + "heckel.io/ntfy/log" "heckel.io/ntfy/util" "io" "os" "path/filepath" "strings" + "time" ) func init() { @@ -30,6 +32,7 @@ var flagsPublish = append( &cli.StringFlag{Name: "file", Aliases: []string{"f"}, EnvVars: []string{"NTFY_FILE"}, Usage: "file to upload as an attachment"}, &cli.StringFlag{Name: "email", Aliases: []string{"mail", "e"}, EnvVars: []string{"NTFY_EMAIL"}, Usage: "also send to e-mail address"}, &cli.StringFlag{Name: "user", Aliases: []string{"u"}, EnvVars: []string{"NTFY_USER"}, Usage: "username[:password] used to auth against the server"}, + &cli.IntFlag{Name: "pid", Aliases: []string{"done", "w"}, EnvVars: []string{"NTFY_PID"}, Usage: "monitor process with given PID and publish when it exists"}, &cli.BoolFlag{Name: "no-cache", Aliases: []string{"C"}, EnvVars: []string{"NTFY_NO_CACHE"}, Usage: "do not cache message server-side"}, &cli.BoolFlag{Name: "no-firebase", Aliases: []string{"F"}, EnvVars: []string{"NTFY_NO_FIREBASE"}, Usage: "do not forward message to Firebase"}, &cli.BoolFlag{Name: "env-topic", Aliases: []string{"P"}, EnvVars: []string{"NTFY_ENV_TOPIC"}, Usage: "use topic from NTFY_TOPIC env variable"}, @@ -86,6 +89,7 @@ func execPublish(c *cli.Context) error { file := c.String("file") email := c.String("email") user := c.String("user") + pid := c.Int("pid") noCache := c.Bool("no-cache") noFirebase := c.Bool("no-firebase") envTopic := c.Bool("env-topic") @@ -178,6 +182,11 @@ func execPublish(c *cli.Context) error { } } } + if pid > 0 { + if err := waitForProcess(pid); err != nil { + return err + } + } cl := client.New(conf) m, err := cl.PublishReader(topic, body, options...) if err != nil { @@ -188,3 +197,15 @@ func execPublish(c *cli.Context) error { } return nil } + +func waitForProcess(pid int) error { + if !processExists(pid) { + return fmt.Errorf("process with PID %d not running", pid) + } + log.Debug("Waiting for process with PID %d to exit", pid) + for processExists(pid) { + time.Sleep(500 * time.Millisecond) + } + log.Debug("Process with PID %d exited", pid) + return nil +} diff --git a/cmd/publish_unix.go b/cmd/publish_unix.go new file mode 100644 index 00000000..2ff32cc6 --- /dev/null +++ b/cmd/publish_unix.go @@ -0,0 +1,8 @@ +package cmd + +import "syscall" + +func processExists(pid int) bool { + err := syscall.Kill(pid, syscall.Signal(0)) + return err == nil +} diff --git a/cmd/publish_windows.go b/cmd/publish_windows.go new file mode 100644 index 00000000..92ce3112 --- /dev/null +++ b/cmd/publish_windows.go @@ -0,0 +1,10 @@ +package cmd + +import ( + "os" +) + +func processExists(pid int) bool { + _, err := os.FindProcess(pid) + return err == nil +}