diff --git a/main.go b/main.go index b6e1a65..0f9c780 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "flag" "fmt" "net/http" + "os/exec" + "runtime" "time" ) @@ -36,6 +38,7 @@ func main() { for _, apod := range apods { printPrettyFormattedAPOD(apod) + openBrowser(apod.URL) } } @@ -93,3 +96,22 @@ func constructURL(apiKey, start, end string) string { apiURL, apiKey, start, end) } + +func openBrowser(url string) { + var err error + + switch runtime.GOOS { + case "linux": + err = exec.Command("xdg-open", url).Start() + case "windows": + err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() + case "darwin": + err = exec.Command("open", url).Start() + default: + err = fmt.Errorf("unsupported platform") + } + + if err != nil { + fmt.Println("Error opening browser", url, ":", err) + } +}