|
|
|
|
@@ -5,6 +5,7 @@ import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
@@ -45,16 +46,25 @@ func (c *Client) Run(ctx context.Context, args []string, env map[string]string,
|
|
|
|
|
if c.Bin == "" {
|
|
|
|
|
return "", "", 0, errors.New("ntfy binary not set")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Many (newer) ntfy versions support `--config/-c` for server-side commands
|
|
|
|
|
// (serve/user/access/token). Some older builds do not. We try with --config
|
|
|
|
|
// first (if configured) and fall back to running without it if the binary
|
|
|
|
|
// rejects the flag.
|
|
|
|
|
withConfig := args
|
|
|
|
|
if c.Config != "" {
|
|
|
|
|
args = append([]string{"--config", c.Config}, args...)
|
|
|
|
|
withConfig = append([]string{"--config", c.Config}, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tctx := ctx
|
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
if c.Timeout > 0 {
|
|
|
|
|
tctx, cancel = context.WithTimeout(ctx, c.Timeout)
|
|
|
|
|
defer cancel()
|
|
|
|
|
}
|
|
|
|
|
cmd := exec.CommandContext(tctx, c.Bin, args...)
|
|
|
|
|
// helper to execute once
|
|
|
|
|
runOnce := func(a []string) (string, string, int, error) {
|
|
|
|
|
cmd := exec.CommandContext(tctx, c.Bin, a...)
|
|
|
|
|
if stdin != "" {
|
|
|
|
|
cmd.Stdin = strings.NewReader(stdin)
|
|
|
|
|
}
|
|
|
|
|
@@ -62,7 +72,8 @@ func (c *Client) Run(ctx context.Context, args []string, env map[string]string,
|
|
|
|
|
cmd.Stdout = &outb
|
|
|
|
|
cmd.Stderr = &errb
|
|
|
|
|
if env != nil {
|
|
|
|
|
// inherit env automatically
|
|
|
|
|
// inherit env + add/override provided vars
|
|
|
|
|
cmd.Env = append([]string{}, os.Environ()...)
|
|
|
|
|
for k, v := range env {
|
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
|
}
|
|
|
|
|
@@ -82,6 +93,22 @@ func (c *Client) Run(ctx context.Context, args []string, env map[string]string,
|
|
|
|
|
return outb.String(), errb.String(), exit, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// first attempt (maybe with --config)
|
|
|
|
|
out, errOut, exit, err := runOnce(withConfig)
|
|
|
|
|
if c.Config == "" {
|
|
|
|
|
return out, errOut, exit, err
|
|
|
|
|
}
|
|
|
|
|
// fallback for older ntfy binaries that don't know --config
|
|
|
|
|
if exit != 0 {
|
|
|
|
|
errTrim := strings.TrimSpace(errOut)
|
|
|
|
|
if strings.Contains(errTrim, "flag provided but not defined: -config") ||
|
|
|
|
|
strings.Contains(errTrim, "unknown flag") && strings.Contains(errTrim, "config") {
|
|
|
|
|
return runOnce(args)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out, errOut, exit, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) ListUsers(ctx context.Context) ([]User, error) {
|
|
|
|
|
out, errOut, exit, err := c.Run(ctx, []string{"user", "list"}, nil, "")
|
|
|
|
|
if err != nil || exit != 0 {
|
|
|
|
|
|