Commit 854a5c14 authored by Christopher Harm's avatar Christopher Harm

Adding open command to devtool

parent 43b1d101
......@@ -242,14 +242,15 @@ func buildDockerAndDeploy(config config.Config) error {
}
return err
}
}
if viper.GetBool("build.openServices") {
err = environment.Run(true, "minikube", "service", releaseName)
if err != nil {
return err
}
if viper.GetBool("build.openServices") {
err := openBrowser(config)
if err != nil {
return err
}
}
return nil
}
......
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"os/exec"
"runtime"
"git.psu.edu/k8s/devtool/config"
"git.psu.edu/k8s/devtool/environment"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// buildCmd represents the build command
var openCmd = &cobra.Command{
Use: "open",
Short: "open the application in a browser",
Run: func(cmd *cobra.Command, args []string) {
prereqsInstalled := checkLocalSetup()
if !prereqsInstalled {
color.Red("Please install the required applications and try again.\n")
return
}
if minikubeProvider() && !minikubeRunning() {
color.Red("Minikube is not started... run `devtool start` first.")
return
}
if kindProvider() && !kindRunning() {
color.Red("kind cluster is not created/running... run `devtool start` first.")
return
}
conf, err := config.New(configFile)
if err != nil {
color.New(color.FgRed).Printf("Error reading configuration file: %s\n", configFile)
return
}
err = openBrowser(conf)
if err != nil {
color.New(color.FgRed).Printf("Failed to open the application: %v\n", err)
return
}
},
}
func init() {
rootCmd.AddCommand(openCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// buildCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// buildCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func openBrowser(config config.Config) error {
color.Blue("Launching the Application")
var host string
if minikubeProvider() {
output := environment.RunAndGetOutputRequired("minikube", "ip")
host = output[0]
} else if kindProvider() {
host = "localhost"
}
url := fmt.Sprintf("http://%s%s", host, config.OpenUrlPath)
fmt.Printf("Opening Browser: %s\n", url)
err := open(url)
if err != nil {
return err
}
return nil
}
// open opens the specified URL in the default browser of the user.
func open(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
......@@ -12,6 +12,7 @@ type (
Name string `json:"name"`
Language string `json:"language"`
Deployables []Deployable `json:"deployables"`
OpenUrlPath string `json:"openUrlPath"`
LocalEnvVars []string `json:"localEnvVars"`
LocalSecrets []string `json:"localSecrets"`
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment