Skip to content

Commit

Permalink
Add openBrowser function to open URL in default browser
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusziade committed Aug 31, 2023
1 parent 78dfa3e commit 5b01742
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"flag"
"fmt"
"net/http"
"os/exec"
"runtime"
"time"
)

Expand Down Expand Up @@ -36,6 +38,7 @@ func main() {

for _, apod := range apods {
printPrettyFormattedAPOD(apod)
openBrowser(apod.URL)
}
}

Expand Down Expand Up @@ -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)
}
}

0 comments on commit 5b01742

Please sign in to comment.