github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/web/webutil/browser.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build linux darwin
     6  
     7  package webutil
     8  
     9  import (
    10  	"os"
    11  	"os/exec"
    12  	"runtime"
    13  )
    14  
    15  // StartBrowser tries to open the URL in a browser and reports whether it
    16  // succeeds.
    17  func StartBrowser(url string) bool {
    18  	// try to start the browser
    19  	var args []string
    20  	aws_env := os.Getenv("AWS_ENV")
    21  	switch runtime.GOOS {
    22  	case "darwin":
    23  		if aws_env != "" {
    24  			args = []string{"open", "-na", "Google Chrome", "--args", "--profile-directory=" + aws_env, "--new-window"}
    25  		} else {
    26  			args = []string{"open"}
    27  		}
    28  	case "windows":
    29  		args = []string{"cmd", "/c", "start"}
    30  	default:
    31  		args = []string{"xdg-open"}
    32  	}
    33  	cmd := exec.Command(args[0], append(args[1:], url)...)
    34  	return cmd.Start() == nil
    35  }