| 1 |
// Package generate provides the CLI command for generating notification service URLs. |
| 2 |
package generate |
| 3 |
|
| 4 |
import ( |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"net/url" |
| 8 |
"os" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/spf13/cobra" |
| 12 |
|
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/color" |
| 14 |
"github.com/nicholas-fedor/shoutrrr/pkg/generators" |
| 15 |
"github.com/nicholas-fedor/shoutrrr/pkg/router" |
| 16 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 17 |
) |
| 18 |
|
| 19 |
// MaximumNArgs defines the maximum number of positional arguments allowed. |
| 20 |
const MaximumNArgs = 2 |
| 21 |
|
| 22 |
// redactedStr represents the placeholder string for masked text values. |
| 23 |
const redactedStr = "REDACTED" |
| 24 |
|
| 25 |
var ( |
| 26 |
// ErrNoServiceSpecified indicates that no service was provided for URL generation. |
| 27 |
ErrNoServiceSpecified = errors.New("no service specified") |
| 28 |
|
| 29 |
// serviceRouter manages the creation of notification services. |
| 30 |
serviceRouter router.ServiceRouter |
| 31 |
|
| 32 |
// Cmd is the cobra command for generating notification service URLs. |
| 33 |
// It creates a URL from user-provided properties and configuration. |
| 34 |
Cmd = &cobra.Command{ |
| 35 |
Use: "generate", |
| 36 |
Short: "Generates a notification service URL from user input", |
| 37 |
Run: Run, |
| 38 |
PreRun: loadArgsFromAltSources, |
| 39 |
Args: cobra.MaximumNArgs(MaximumNArgs), |
| 40 |
} |
| 41 |
) |
| 42 |
|
| 43 |
// init initializes the command flags for the generate command. |
| 44 |
func init() { |
| 45 |
serviceRouter = router.ServiceRouter{ |
| 46 |
Timeout: 0, |
| 47 |
} |
| 48 |
|
| 49 |
Cmd.Flags(). |
| 50 |
StringP("service", "s", "", "Notification service to generate a URL for (e.g., discord, smtp)") |
| 51 |
Cmd.Flags(). |
| 52 |
StringP("generator", "g", "basic", "Generator to use (e.g., basic, or service-specific)") |
| 53 |
Cmd.Flags(). |
| 54 |
StringArrayP("property", "p", []string{}, "Configuration property in key=value format (e.g., token=abc123)") |
| 55 |
Cmd.Flags(). |
| 56 |
BoolP("show-sensitive", "x", false, "Show sensitive data in the generated URL (default: masked)") |
| 57 |
} |
| 58 |
|
| 59 |
// Run executes the generate command, producing a notification service URL. |
| 60 |
// |
| 61 |
// Parameters: |
| 62 |
// - cmd: The cobra command containing the parsed flags. |
| 63 |
// - _: Unused positional arguments (handled by PreRun). |
| 64 |
func Run(cmd *cobra.Command, _ []string) { |
| 65 |
var service types.Service |
| 66 |
|
| 67 |
var err error |
| 68 |
|
| 69 |
// Retrieve command flags. |
| 70 |
serviceSchema, err := cmd.Flags().GetString("service") |
| 71 |
if err != nil { |
| 72 |
_, _ = fmt.Fprint(os.Stderr, "Error getting service flag: ", err, "\n") |
| 73 |
|
| 74 |
os.Exit(1) |
| 75 |
} |
| 76 |
|
| 77 |
generatorName, err := cmd.Flags().GetString("generator") |
| 78 |
if err != nil { |
| 79 |
_, _ = fmt.Fprint(os.Stderr, "Error getting generator flag: ", err, "\n") |
| 80 |
|
| 81 |
os.Exit(1) |
| 82 |
} |
| 83 |
|
| 84 |
propertyFlags, err := cmd.Flags().GetStringArray("property") |
| 85 |
if err != nil { |
| 86 |
_, _ = fmt.Fprint(os.Stderr, "Error getting property flag: ", err, "\n") |
| 87 |
|
| 88 |
os.Exit(1) |
| 89 |
} |
| 90 |
|
| 91 |
showSensitive, err := cmd.Flags().GetBool("show-sensitive") |
| 92 |
if err != nil { |
| 93 |
_, _ = fmt.Fprint(os.Stderr, "Error getting show-sensitive flag: ", err, "\n") |
| 94 |
|
| 95 |
os.Exit(1) |
| 96 |
} |
| 97 |
|
| 98 |
// Parse properties into a key-value map. |
| 99 |
props := make(map[string]string, len(propertyFlags)) |
| 100 |
|
| 101 |
cfg := color.DefaultConfig() |
| 102 |
|
| 103 |
for _, prop := range propertyFlags { |
| 104 |
parts := strings.Split(prop, "=") |
| 105 |
if len(parts) != MaximumNArgs { |
| 106 |
_, _ = fmt.Fprint( |
| 107 |
cfg.Output, |
| 108 |
"Invalid property key/value pair: ", |
| 109 |
color.HiYellowString(prop), |
| 110 |
"\n", |
| 111 |
) |
| 112 |
|
| 113 |
continue |
| 114 |
} |
| 115 |
|
| 116 |
props[parts[0]] = parts[1] |
| 117 |
} |
| 118 |
|
| 119 |
if len(propertyFlags) > 0 { |
| 120 |
// Add spacing after property warnings. |
| 121 |
_, _ = fmt.Fprint(cfg.Output, "\n") |
| 122 |
} |
| 123 |
|
| 124 |
// Validate and create the service. |
| 125 |
if serviceSchema == "" { |
| 126 |
err = ErrNoServiceSpecified |
| 127 |
} else { |
| 128 |
service, err = serviceRouter.NewService(serviceSchema) |
| 129 |
} |
| 130 |
|
| 131 |
if err != nil { |
| 132 |
_, _ = fmt.Fprint(os.Stdout, "Error: ", err, "\n") |
| 133 |
} |
| 134 |
|
| 135 |
if service == nil { |
| 136 |
services := serviceRouter.ListServices() |
| 137 |
serviceList := strings.Join(services, ", ") |
| 138 |
cmd.SetUsageTemplate(cmd.UsageTemplate() + "\nAvailable services:\n " + serviceList + "\n") |
| 139 |
|
| 140 |
if err := cmd.Usage(); err != nil { |
| 141 |
_, _ = fmt.Fprint(os.Stderr, "Error displaying usage: ", err, "\n") |
| 142 |
} |
| 143 |
|
| 144 |
os.Exit(1) |
| 145 |
} |
| 146 |
|
| 147 |
// Determine the generator to use. |
| 148 |
var generator types.Generator |
| 149 |
|
| 150 |
generatorFlag := cmd.Flags().Lookup("generator") |
| 151 |
if !generatorFlag.Changed { |
| 152 |
// Use the service-specific default generator if available and no explicit generator is set. |
| 153 |
generator, err = generators.NewGenerator(serviceSchema) |
| 154 |
if err != nil { |
| 155 |
// Service-specific generator not found, will try basic generator later. |
| 156 |
generator = nil |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
if generator != nil { |
| 161 |
generatorName = serviceSchema |
| 162 |
} else { |
| 163 |
var genErr error |
| 164 |
|
| 165 |
generator, genErr = generators.NewGenerator(generatorName) |
| 166 |
if genErr != nil { |
| 167 |
_, _ = fmt.Fprint(os.Stdout, "Error: ", genErr, "\n") |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if generator == nil { |
| 172 |
generatorList := strings.Join(generators.ListGenerators(), ", ") |
| 173 |
cmd.SetUsageTemplate( |
| 174 |
cmd.UsageTemplate() + "\nAvailable generators:\n " + generatorList + "\n", |
| 175 |
) |
| 176 |
|
| 177 |
if err := cmd.Usage(); err != nil { |
| 178 |
_, _ = fmt.Fprint(os.Stderr, "Error displaying usage: ", err, "\n") |
| 179 |
} |
| 180 |
|
| 181 |
os.Exit(1) |
| 182 |
} |
| 183 |
|
| 184 |
// Generate and display the URL. |
| 185 |
_, _ = fmt.Fprint(cfg.Output, "Generating URL for ", color.HiCyanString(serviceSchema)) |
| 186 |
_, _ = fmt.Fprint(cfg.Output, " using ", color.HiMagentaString(generatorName), " generator\n") |
| 187 |
|
| 188 |
serviceConfig, err := generator.Generate(service, props, cmd.Flags().Args()) |
| 189 |
if err != nil { |
| 190 |
_, _ = fmt.Fprint(os.Stdout, "Error: ", err, "\n") |
| 191 |
|
| 192 |
os.Exit(1) |
| 193 |
} |
| 194 |
|
| 195 |
_, _ = fmt.Fprint(cfg.Output, "\n") |
| 196 |
|
| 197 |
maskedURL := maskSensitiveURL(serviceSchema, serviceConfig.GetURL().String()) |
| 198 |
|
| 199 |
if showSensitive { |
| 200 |
_, _ = fmt.Fprint(os.Stdout, "URL: ", serviceConfig.GetURL().String(), "\n") |
| 201 |
} else { |
| 202 |
_, _ = fmt.Fprint(os.Stdout, "URL: ", maskedURL, "\n") |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// loadArgsFromAltSources populates command flags from positional arguments if provided. |
| 207 |
// This allows users to specify service and generator as positional args instead of flags. |
| 208 |
// |
| 209 |
// Parameters: |
| 210 |
// - cmd: The cobra command to populate with flag values. |
| 211 |
// - args: The positional arguments (args[0] = service, args[1] = generator). |
| 212 |
func loadArgsFromAltSources(cmd *cobra.Command, args []string) { |
| 213 |
if len(args) > 0 { |
| 214 |
if err := cmd.Flags().Set("service", args[0]); err != nil { |
| 215 |
_, _ = fmt.Fprint(os.Stderr, "Error setting service flag: ", err, "\n") |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if len(args) > 1 { |
| 220 |
if err := cmd.Flags().Set("generator", args[1]); err != nil { |
| 221 |
_, _ = fmt.Fprint(os.Stderr, "Error setting generator flag: ", err, "\n") |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// maskSensitiveURL masks sensitive parts of a Shoutrrr URL based on the service schema. |
| 227 |
// |
| 228 |
// Parameters: |
| 229 |
// - serviceSchema: The service type (e.g., "discord", "smtp", "pushover"). |
| 230 |
// - urlStr: The URL string to mask. |
| 231 |
// |
| 232 |
// Returns: |
| 233 |
// - string: The URL with sensitive information masked. |
| 234 |
func maskSensitiveURL(serviceSchema, urlStr string) string { |
| 235 |
parsedURL, err := url.Parse(urlStr) |
| 236 |
if err != nil { |
| 237 |
// Return original URL if parsing fails. |
| 238 |
return urlStr |
| 239 |
} |
| 240 |
|
| 241 |
switch serviceSchema { |
| 242 |
case "discord", "slack", "teams": |
| 243 |
maskUser(parsedURL, redactedStr) |
| 244 |
case "smtp": |
| 245 |
maskSMTPUser(parsedURL) |
| 246 |
case "pushover": |
| 247 |
maskPushoverQuery(parsedURL) |
| 248 |
case "gotify": |
| 249 |
maskGotifyQuery(parsedURL) |
| 250 |
default: |
| 251 |
maskGeneric(parsedURL) |
| 252 |
} |
| 253 |
|
| 254 |
return parsedURL.String() |
| 255 |
} |
| 256 |
|
| 257 |
// maskUser redacts the username in a URL with a placeholder. |
| 258 |
// |
| 259 |
// Parameters: |
| 260 |
// - parsedURL: The URL to modify. |
| 261 |
// - placeholder: The replacement string for the username. |
| 262 |
func maskUser(parsedURL *url.URL, placeholder string) { |
| 263 |
if parsedURL.User != nil { |
| 264 |
parsedURL.User = url.User(placeholder) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
// maskSMTPUser redacts the password in an SMTP URL, preserving the username. |
| 269 |
// |
| 270 |
// Parameters: |
| 271 |
// - parsedURL: The SMTP URL to modify. |
| 272 |
func maskSMTPUser(parsedURL *url.URL) { |
| 273 |
if parsedURL.User != nil { |
| 274 |
parsedURL.User = url.UserPassword(parsedURL.User.Username(), redactedStr) |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
// maskPushoverQuery redacts token and user query parameters in a Pushover URL. |
| 279 |
// |
| 280 |
// Parameters: |
| 281 |
// - parsedURL: The Pushover URL to modify. |
| 282 |
func maskPushoverQuery(parsedURL *url.URL) { |
| 283 |
queryParams := parsedURL.Query() |
| 284 |
if queryParams.Get("token") != "" { |
| 285 |
queryParams.Set("token", redactedStr) |
| 286 |
} |
| 287 |
|
| 288 |
if queryParams.Get("user") != "" { |
| 289 |
queryParams.Set("user", redactedStr) |
| 290 |
} |
| 291 |
|
| 292 |
parsedURL.RawQuery = queryParams.Encode() |
| 293 |
} |
| 294 |
|
| 295 |
// maskGotifyQuery redacts the token query parameter in a Gotify URL. |
| 296 |
// |
| 297 |
// Parameters: |
| 298 |
// - parsedURL: The Gotify URL to modify. |
| 299 |
func maskGotifyQuery(parsedURL *url.URL) { |
| 300 |
queryParams := parsedURL.Query() |
| 301 |
if queryParams.Get("token") != "" { |
| 302 |
queryParams.Set("token", redactedStr) |
| 303 |
} |
| 304 |
|
| 305 |
parsedURL.RawQuery = queryParams.Encode() |
| 306 |
} |
| 307 |
|
| 308 |
// maskGeneric redacts userinfo and all query parameters for unrecognized services. |
| 309 |
// |
| 310 |
// Parameters: |
| 311 |
// - parsedURL: The URL to modify. |
| 312 |
func maskGeneric(parsedURL *url.URL) { |
| 313 |
maskUser(parsedURL, redactedStr) |
| 314 |
|
| 315 |
queryParams := parsedURL.Query() |
| 316 |
for key := range queryParams { |
| 317 |
queryParams.Set(key, redactedStr) |
| 318 |
} |
| 319 |
|
| 320 |
parsedURL.RawQuery = queryParams.Encode() |
| 321 |
} |