diff --git a/pkg/open/open_windows.go b/pkg/open/open_windows.go index 53bb42d8c7..bf73021817 100644 --- a/pkg/open/open_windows.go +++ b/pkg/open/open_windows.go @@ -2,10 +2,16 @@ package open import ( "os/exec" + "regexp" "syscall" ) +var unescapedAmpsRegex = regexp.MustCompile(`([^\\^])&`) + func browser(url string) error { + // Replace all instances of & that are not already escaped with ^. + // This is necessary because cmd.exe treats & as a command separator. + url = unescapedAmpsRegex.ReplaceAllString(url, "${1}^&") cmd := exec.Command("cmd", "/c", "start", url) cmd.SysProcAttr = &syscall.SysProcAttr{ // HideWindow avoids a brief cmd console from opening diff --git a/tools/open/main.go b/tools/open/main.go new file mode 100644 index 0000000000..36aca72fa4 --- /dev/null +++ b/tools/open/main.go @@ -0,0 +1,27 @@ +package main + +// This is a tool to test the "open" package. +// It will open the default browser at a given URL. + +import ( + "flag" + "fmt" + + "github.com/fleetdm/fleet/v4/pkg/open" +) + +func main() { + openTool := flag.String("url", "", "URL to open") + flag.Parse() + + if *openTool == "" { + fmt.Println("Please provide a URL to open using the -url flag") + return + } + + err := open.Browser(*openTool) + if err != nil { + fmt.Printf("Error opening URL: %v\n", err) + return + } +}