6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-25 16:28:20 +00:00

Fix pre/post hook support to accept stdin

This commit is contained in:
James Mills 2022-03-20 09:55:20 +10:00
parent 2f62274265
commit 24dee80aba
4 changed files with 24 additions and 12 deletions

@ -34,7 +34,7 @@ not specified defaults to the local user ($USER)`,
log.Fatal("error getting --pre-hook flag")
}
posthook, err := cmd.Flags().GetString("pre-hook")
posthook, err := cmd.Flags().GetString("post-hook")
if err != nil {
log.Fatal("error getting --post-hook flag")
}

@ -1,6 +1,7 @@
package saltyim
import (
"bytes"
"fmt"
"os"
"time"
@ -9,6 +10,7 @@ import (
"git.mills.io/prologic/msgbus/client"
"github.com/gen2brain/beeep"
"github.com/keys-pub/keys"
log "github.com/sirupsen/logrus"
"go.mills.io/salty"
)
@ -20,7 +22,11 @@ const (
func handleMessage(key *keys.EdX25519Key, prehook, posthook string, msgs chan string) msgbus.HandlerFunc {
return func(msg *msgbus.Message) error {
if prehook != "" {
_ = RunCmd(defaultRunCmdTimeout, prehook)
out, err := RunCmd(defaultRunCmdTimeout, prehook, bytes.NewBuffer(msg.Payload))
if err != nil {
log.WithError(err).Debugf("error running pre-hook %s", prehook)
}
log.Debugf("pre-hook: %q", out)
}
data, _, err := salty.Decrypt(key, msg.Payload)
@ -32,10 +38,16 @@ func handleMessage(key *keys.EdX25519Key, prehook, posthook string, msgs chan st
msgs <- string(data)
// Errors are ignored silently
_ = beeep.Alert("Salty IM", "You have a new Salty Message", "")
if err := beeep.Alert("Salty IM", "You have a new Salty Message", ""); err != nil {
log.WithError(err).Debug("error sending desktop notification")
}
if posthook != "" {
_ = RunCmd(defaultRunCmdTimeout, posthook)
out, err := RunCmd(defaultRunCmdTimeout, posthook, bytes.NewBuffer(data))
if err != nil {
log.WithError(err).Debugf("error running post-hook %s", posthook)
}
log.Debugf("post-hook: %q", out)
}
return nil

4
scripts/log-posthook.sh Executable file

@ -0,0 +1,4 @@
#!/bin/sh
cat >> msgs
echo >> msgs

@ -109,7 +109,7 @@ func CmdExists(cmd string) bool {
}
// RunCmd ...
func RunCmd(timeout time.Duration, command string, args ...string) error {
func RunCmd(timeout time.Duration, command string, stdin io.Reader, args ...string) (string, error) {
var (
ctx context.Context
cancel context.CancelFunc
@ -123,6 +123,7 @@ func RunCmd(timeout time.Duration, command string, args ...string) error {
defer cancel()
cmd := exec.CommandContext(ctx, command, args...)
cmd.Stdin = stdin
out, err := cmd.CombinedOutput()
if err != nil {
@ -134,13 +135,8 @@ func RunCmd(timeout time.Duration, command string, args ...string) error {
}
}
log.
WithError(err).
WithField("out", string(out)).
Errorf("error running command")
return err
return "", err
}
return nil
return string(out), nil
}