diff --git a/cmd/start.go b/cmd/start.go index f36c22c6..9e215980 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -7,6 +7,7 @@ import ( "github.com/microcks/microcks-cli/pkg/config" "github.com/microcks/microcks-cli/pkg/connectors" "github.com/microcks/microcks-cli/pkg/errors" + "github.com/microcks/microcks-cli/pkg/util" "github.com/spf13/cobra" ) @@ -17,6 +18,7 @@ func NewStartCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command imageName string autoRemove bool driver string + launchBrowser bool ) var startCmd = &cobra.Command{ Use: "start", @@ -141,6 +143,7 @@ microcks start --name [name of you container/instance]`, errors.CheckError(err) fmt.Printf("Microcks started successfully at %s\n", server) + util.LaunchBrowser(server, launchBrowser) }, } startCmd.Flags().StringVar(&name, "name", "microcks", "name for you microcks instance") @@ -148,5 +151,6 @@ microcks start --name [name of you container/instance]`, startCmd.Flags().StringVar(&imageName, "image", "quay.io/microcks/microcks-uber:latest-native", "image which will be used to create a container") startCmd.Flags().BoolVar(&autoRemove, "rm", false, "mimic of '--rm' flag of dokcer to automatically remove the container when it exits") startCmd.Flags().StringVar(&driver, "driver", "docker", "use --driver to change driver from docker to podman") + startCmd.Flags().BoolVar(&launchBrowser, "launch-browser", true, "Automatically launch browser to the Microcks dashboard") return startCmd } diff --git a/pkg/util/browser.go b/pkg/util/browser.go new file mode 100644 index 00000000..37f8c21f --- /dev/null +++ b/pkg/util/browser.go @@ -0,0 +1,35 @@ +package util + +import ( + "fmt" + "os" + + "github.com/skratchdot/open-golang/open" +) + +/* +LaunchBrowser opens the default system browser to the specified URL if conditions are met. +It skips opening the browser if the launchBrowser flag is false, if running in a CI environment, +or if the MICROCKS_NO_BROWSER environment variable is set. +*/ +func LaunchBrowser(url string, launchBrowser bool) { + if !launchBrowser { + return + } + + if os.Getenv("CI") != "" { + //if in CI environment, do not attempt to open browser + return + } + + if os.Getenv("MICROCKS_NO_BROWSER") != "" { + // if MICROCKS_NO_BROWSER environment variable is set, do not attempt to open browser + return + } + + fmt.Printf("Opening system default browser for %s\n", url) + err := open.Start(url) + if err != nil { + fmt.Printf("Failed to open browser: %v\n", err) + } +}