| 1 |
// Package verify provides the CLI command for verifying notification service URLs. |
| 2 |
package verify |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/spf13/cobra" |
| 10 |
|
| 11 |
internalUtil "github.com/nicholas-fedor/shoutrrr/internal/util" |
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/color" |
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 14 |
"github.com/nicholas-fedor/shoutrrr/pkg/router" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
// Cmd is the cobra command for verifying notification service URLs. |
| 19 |
// It validates that a URL is properly formatted and the service can be located. |
| 20 |
Cmd = &cobra.Command{ |
| 21 |
Use: "verify", |
| 22 |
Short: "Verify the validity of a notification service URL", |
| 23 |
PreRunE: internalUtil.LoadFlagsFromAltSources, |
| 24 |
Run: Run, |
| 25 |
Args: cobra.MaximumNArgs(1), |
| 26 |
} |
| 27 |
|
| 28 |
// serviceRouter manages service lookup and initialization. |
| 29 |
serviceRouter router.ServiceRouter |
| 30 |
) |
| 31 |
|
| 32 |
// init initializes the command flags for the verify command. |
| 33 |
func init() { |
| 34 |
Cmd.Flags().StringArrayP("url", "u", []string{}, "The notification URL(s) to verify") |
| 35 |
|
| 36 |
if err := Cmd.MarkFlagRequired("url"); err != nil { |
| 37 |
fmt.Fprintf(os.Stderr, "Error marking URL flag as required: %v\n", err) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// Run executes the verify command, validating the specified notification URL. |
| 42 |
// It locates the service, initializes it, and displays the parsed configuration. |
| 43 |
// |
| 44 |
// Parameters: |
| 45 |
// - cmd: The cobra command containing the parsed flags. |
| 46 |
// - _: Unused positional arguments. |
| 47 |
func Run(cmd *cobra.Command, _ []string) { |
| 48 |
// Retrieve the URL flag. |
| 49 |
urls, err := cmd.Flags().GetStringArray("url") |
| 50 |
if err != nil { |
| 51 |
_, _ = fmt.Fprint(os.Stderr, "Error getting URL flags: ", err, "\n") |
| 52 |
|
| 53 |
os.Exit(1) |
| 54 |
} |
| 55 |
|
| 56 |
// Validate URL count: the verify command supports exactly one URL. |
| 57 |
if len(urls) == 0 { |
| 58 |
_, _ = fmt.Fprint(os.Stderr, "Error: a URL is required. Use --url to specify a notification service URL.\n") |
| 59 |
|
| 60 |
os.Exit(1) |
| 61 |
} |
| 62 |
|
| 63 |
if len(urls) > 1 { |
| 64 |
_, _ = fmt.Fprint(os.Stderr, "Error: multiple --url values are not supported. Please specify a single URL.\n") |
| 65 |
|
| 66 |
os.Exit(1) |
| 67 |
} |
| 68 |
|
| 69 |
URL := urls[0] |
| 70 |
|
| 71 |
// Initialize the service router with default timeout (0 = no timeout). |
| 72 |
serviceRouter = router.ServiceRouter{ |
| 73 |
Timeout: 0, |
| 74 |
} |
| 75 |
|
| 76 |
// Locate the service for the provided URL. |
| 77 |
service, err := serviceRouter.Locate(URL) |
| 78 |
if err != nil { |
| 79 |
wrappedErr := fmt.Errorf("locating service for URL: %w", err) |
| 80 |
_, _ = fmt.Fprint(os.Stdout, "error verifying URL: ", sanitizeError(wrappedErr), "\n") |
| 81 |
|
| 82 |
os.Exit(1) |
| 83 |
} |
| 84 |
|
| 85 |
// Retrieve and display the service configuration. |
| 86 |
config := format.GetServiceConfig(service) |
| 87 |
configNode := format.GetConfigFormat(config) |
| 88 |
|
| 89 |
cfg := color.DefaultConfig() |
| 90 |
_, _ = fmt.Fprint(cfg.Output, format.ColorFormatTree(configNode, true)) |
| 91 |
} |
| 92 |
|
| 93 |
// sanitizeError removes sensitive details from an error message. |
| 94 |
// It replaces specific error patterns with generic messages to avoid leaking URL details. |
| 95 |
// |
| 96 |
// Parameters: |
| 97 |
// - err: The error to sanitize. |
| 98 |
// |
| 99 |
// Returns: |
| 100 |
// - string: A sanitized error message safe for display. |
| 101 |
func sanitizeError(err error) string { |
| 102 |
errStr := err.Error() |
| 103 |
|
| 104 |
// Check for common error patterns without exposing URL details. |
| 105 |
if strings.Contains(errStr, "unknown service") { |
| 106 |
return "service not recognized" |
| 107 |
} |
| 108 |
|
| 109 |
if strings.Contains(errStr, "parse") || strings.Contains(errStr, "invalid") { |
| 110 |
return "invalid URL format" |
| 111 |
} |
| 112 |
|
| 113 |
// Fallback for other errors. |
| 114 |
return "unable to process URL" |
| 115 |
} |