subscribe_windows.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/urfave/cli/v2"
  5. "heckel.io/ntfy/client"
  6. "heckel.io/ntfy/util"
  7. "log"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. )
  12. const (
  13. defaultClientUserConfigFileRelative = "ntfy\\client.yml"
  14. defaultClientConfigFileDescriptionSuffix = `The default config file for all client commands is %AppData%\ntfy\client.yml.`
  15. )
  16. func runCommandInternal(c *cli.Context, command string, m *client.Message) error {
  17. scriptFile, err := createTmpScript(command)
  18. if err != nil {
  19. return err
  20. }
  21. defer os.Remove(scriptFile)
  22. verbose := c.Bool("verbose")
  23. if verbose {
  24. log.Printf("[%s] Executing: %s (for message: %s)", util.ShortTopicURL(m.TopicURL), command, m.Raw)
  25. }
  26. cmd := exec.Command("cmd.exe", "/Q", "/C", scriptFile)
  27. cmd.Stdin = c.App.Reader
  28. cmd.Stdout = c.App.Writer
  29. cmd.Stderr = c.App.ErrWriter
  30. cmd.Env = envVars(m)
  31. return cmd.Run()
  32. }
  33. func createTmpScript(command string) (string, error) {
  34. scriptFile := fmt.Sprintf("%s/ntfy-subscribe-%s.bat", os.TempDir(), util.RandomString(10))
  35. if err := os.WriteFile(scriptFile, []byte(command), 0700); err != nil {
  36. return "", err
  37. }
  38. return scriptFile, nil
  39. }
  40. func defaultConfigFile() string {
  41. homeDir, _ := os.UserConfigDir()
  42. return filepath.Join(homeDir, defaultClientUserConfigFileRelative)
  43. }