|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/techulus/push-cli/internal/api" |
| 11 | + "github.com/techulus/push-cli/internal/config" |
| 12 | +) |
| 13 | + |
| 14 | +var validSounds = []string{ |
| 15 | + "default", "arcade", "correct", "fail", "harp", "reveal", |
| 16 | + "bubble", "doorbell", "flute", "money", "scifi", "clear", |
| 17 | + "elevator", "guitar", "pop", |
| 18 | +} |
| 19 | + |
| 20 | +func readBodyFromStdinOrFlag(cmd *cobra.Command) (string, error) { |
| 21 | + body, _ := cmd.Flags().GetString("body") |
| 22 | + |
| 23 | + if body == "-" || body == "" { |
| 24 | + stat, _ := os.Stdin.Stat() |
| 25 | + if (stat.Mode() & os.ModeCharDevice) == 0 { |
| 26 | + data, err := io.ReadAll(os.Stdin) |
| 27 | + if err != nil { |
| 28 | + return "", fmt.Errorf("reading stdin: %w", err) |
| 29 | + } |
| 30 | + trimmed := strings.TrimSpace(string(data)) |
| 31 | + if trimmed == "" { |
| 32 | + return "", fmt.Errorf("body is required (use --body flag or pipe via stdin)") |
| 33 | + } |
| 34 | + return trimmed, nil |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if body == "" || body == "-" { |
| 39 | + return "", fmt.Errorf("body is required (use --body flag or pipe via stdin)") |
| 40 | + } |
| 41 | + |
| 42 | + return body, nil |
| 43 | +} |
| 44 | + |
| 45 | +func buildNotifyRequest(cmd *cobra.Command) (api.NotifyRequest, error) { |
| 46 | + title, _ := cmd.Flags().GetString("title") |
| 47 | + body, err := readBodyFromStdinOrFlag(cmd) |
| 48 | + if err != nil { |
| 49 | + return api.NotifyRequest{}, err |
| 50 | + } |
| 51 | + |
| 52 | + sound, _ := cmd.Flags().GetString("sound") |
| 53 | + if sound != "" { |
| 54 | + valid := false |
| 55 | + for _, s := range validSounds { |
| 56 | + if s == sound { |
| 57 | + valid = true |
| 58 | + break |
| 59 | + } |
| 60 | + } |
| 61 | + if !valid { |
| 62 | + return api.NotifyRequest{}, fmt.Errorf("invalid sound %q, valid sounds: %s", sound, strings.Join(validSounds, ", ")) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + channel, _ := cmd.Flags().GetString("channel") |
| 67 | + link, _ := cmd.Flags().GetString("link") |
| 68 | + image, _ := cmd.Flags().GetString("image") |
| 69 | + timeSensitive, _ := cmd.Flags().GetBool("time-sensitive") |
| 70 | + |
| 71 | + return api.NotifyRequest{ |
| 72 | + Title: title, |
| 73 | + Body: body, |
| 74 | + Sound: sound, |
| 75 | + Channel: channel, |
| 76 | + Link: link, |
| 77 | + Image: image, |
| 78 | + TimeSensitive: timeSensitive, |
| 79 | + }, nil |
| 80 | +} |
| 81 | + |
| 82 | +func newAPIClient() *api.Client { |
| 83 | + key := config.GetAPIKey() |
| 84 | + if key == "" { |
| 85 | + fmt.Fprintln(os.Stderr, "No API key configured. Run: push config set-key <api-key>") |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | + return api.NewClient(key) |
| 89 | +} |
| 90 | + |
| 91 | +func addNotifyFlags(cmd *cobra.Command) { |
| 92 | + cmd.Flags().String("title", "", "Notification title (required)") |
| 93 | + cmd.Flags().String("body", "", "Notification body (use '-' to read from stdin)") |
| 94 | + cmd.Flags().String("sound", "", "Notification sound") |
| 95 | + cmd.Flags().String("channel", "", "Notification channel") |
| 96 | + cmd.Flags().String("link", "", "URL to open when notification is tapped") |
| 97 | + cmd.Flags().String("image", "", "Image URL for the notification") |
| 98 | + cmd.Flags().Bool("time-sensitive", false, "Mark as time-sensitive") |
| 99 | + cmd.MarkFlagRequired("title") |
| 100 | +} |
| 101 | + |
| 102 | +var notifyCmd = &cobra.Command{ |
| 103 | + Use: "notify", |
| 104 | + Short: "Send a push notification", |
| 105 | + Run: func(cmd *cobra.Command, args []string) { |
| 106 | + req, err := buildNotifyRequest(cmd) |
| 107 | + if err != nil { |
| 108 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 109 | + os.Exit(1) |
| 110 | + } |
| 111 | + |
| 112 | + client := newAPIClient() |
| 113 | + resp, err := client.Notify(req) |
| 114 | + if err != nil { |
| 115 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 116 | + os.Exit(1) |
| 117 | + } |
| 118 | + |
| 119 | + fmt.Println(resp) |
| 120 | + }, |
| 121 | +} |
| 122 | + |
| 123 | +func init() { |
| 124 | + addNotifyFlags(notifyCmd) |
| 125 | + rootCmd.AddCommand(notifyCmd) |
| 126 | +} |
0 commit comments