Debian Codegraph @

shoutrrr/cmd/verify/verify.go

raw
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 // Locate does not send. The zero timeout leaves each service's own budget in effect.
72 serviceRouter = router.ServiceRouter{}
73
74 // Locate the service for the provided URL.
75 service, err := serviceRouter.Locate(URL)
76 if err != nil {
77 wrappedErr := fmt.Errorf("locating service for URL: %w", err)
78 _, _ = fmt.Fprint(os.Stdout, "error verifying URL: ", sanitizeError(wrappedErr), "\n")
79
80 os.Exit(1)
81 }
82
83 // Retrieve and display the service configuration.
84 config := format.GetServiceConfig(service)
85 configNode := format.GetConfigFormat(config)
86
87 cfg := color.DefaultConfig()
88 _, _ = fmt.Fprint(cfg.Output, format.ColorFormatTree(configNode, true))
89 }
90
91 // sanitizeError removes sensitive details from an error message.
92 // It replaces specific error patterns with generic messages to avoid leaking URL details.
93 //
94 // Parameters:
95 // - err: The error to sanitize.
96 //
97 // Returns:
98 // - string: A sanitized error message safe for display.
99 func sanitizeError(err error) string {
100 errStr := err.Error()
101
102 // Check for common error patterns without exposing URL details.
103 if strings.Contains(errStr, "unknown service") {
104 return "service not recognized"
105 }
106
107 if strings.Contains(errStr, "parse") || strings.Contains(errStr, "invalid") {
108 return "invalid URL format"
109 }
110
111 // Fallback for other errors.
112 return "unable to process URL"
113 }