github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/open.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/get"
     7  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     8  	"github.com/olli-ai/jx/v2/pkg/util"
     9  	"github.com/pkg/browser"
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/jenkins-x/jx-logging/pkg/log"
    13  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    14  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    15  )
    16  
    17  type OpenOptions struct {
    18  	ConsoleOptions
    19  }
    20  
    21  type ConsoleOptions struct {
    22  	get.GetURLOptions
    23  
    24  	OnlyViewURL     bool
    25  	ClassicMode     bool
    26  	JenkinsSelector opts.JenkinsSelectorOptions
    27  }
    28  
    29  var (
    30  	open_long = templates.LongDesc(`
    31  		Opens a named service in the browser.
    32  
    33  		You can use the '--url' argument to just display the URL without opening it`)
    34  
    35  	open_example = templates.Examples(`
    36  		# Open the Nexus console in a browser
    37  		jx open jenkins-x-sonatype-nexus
    38  
    39  		# Print the Nexus console URL but do not open a browser
    40  		jx open jenkins-x-sonatype-nexus -u
    41  
    42  		# List all the service URLs
    43  		jx open`)
    44  )
    45  
    46  func NewCmdOpen(commonOpts *opts.CommonOptions) *cobra.Command {
    47  	options := &OpenOptions{
    48  		ConsoleOptions: ConsoleOptions{
    49  			GetURLOptions: get.GetURLOptions{
    50  				Options: get.Options{
    51  					CommonOptions: commonOpts,
    52  				},
    53  			},
    54  		},
    55  	}
    56  	cmd := &cobra.Command{
    57  		Use:     "open",
    58  		Short:   "Open a service in a browser",
    59  		Long:    open_long,
    60  		Example: open_example,
    61  		Run: func(cmd *cobra.Command, args []string) {
    62  			options.Cmd = cmd
    63  			options.Args = args
    64  			err := options.Run()
    65  			helper.CheckErr(err)
    66  		},
    67  	}
    68  	options.addConsoleFlags(cmd)
    69  	return cmd
    70  }
    71  
    72  func (o *OpenOptions) Run() error {
    73  	if len(o.Args) == 0 {
    74  		return o.GetURLOptions.Run()
    75  	}
    76  	name := o.Args[0]
    77  	return o.ConsoleOptions.open(name, name)
    78  }
    79  
    80  func (o *ConsoleOptions) open(name string, label string) error {
    81  	var err error
    82  	svcURL := ""
    83  	ns := o.Namespace
    84  	if ns == "" && o.Environment != "" {
    85  		ns, err = o.FindEnvironmentNamespace(o.Environment)
    86  		if err != nil {
    87  			return err
    88  		}
    89  	}
    90  	if ns != "" {
    91  		svcURL, err = o.FindServiceInNamespace(name, ns)
    92  	} else {
    93  		svcURL, err = o.FindService(name)
    94  	}
    95  	if err != nil && name != "" {
    96  		log.Logger().Infof("If the app %s is running in a different environment you could try: %s", util.ColorInfo(name), util.ColorInfo("jx get applications"))
    97  	}
    98  	if err != nil {
    99  		return err
   100  	}
   101  	fullURL := svcURL
   102  	if name == "jenkins" {
   103  		fullURL = o.urlForMode(svcURL)
   104  	}
   105  	if o.OnlyViewHost {
   106  		host := util.URLToHostName(svcURL)
   107  		_, err = fmt.Fprintf(o.Out, "%s: %s\n", label, util.ColorInfo(host))
   108  		if err != nil {
   109  			return err
   110  		}
   111  	} else {
   112  		_, err = fmt.Fprintf(o.Out, "%s: %s\n", label, util.ColorInfo(fullURL))
   113  		if err != nil {
   114  			return err
   115  		}
   116  	}
   117  	if !o.OnlyViewURL && !o.OnlyViewHost {
   118  		err = browser.OpenURL(fullURL)
   119  		if err != nil {
   120  			return err
   121  		}
   122  	}
   123  	return nil
   124  }
   125  
   126  func (o *ConsoleOptions) addConsoleFlags(cmd *cobra.Command) {
   127  	cmd.Flags().BoolVarP(&o.OnlyViewURL, "url", "u", false, "Only displays and the URL and does not open the browser")
   128  	cmd.Flags().BoolVarP(&o.ClassicMode, "classic", "", false, "Use the classic Jenkins skin instead of Blue Ocean")
   129  
   130  	o.AddGetUrlFlags(cmd)
   131  	o.JenkinsSelector.AddFlags(cmd)
   132  }
   133  
   134  func (o *ConsoleOptions) urlForMode(url string) string {
   135  	if o.ClassicMode {
   136  		return url
   137  	}
   138  
   139  	blueOceanPath := "/blue"
   140  
   141  	return util.UrlJoin(url, blueOceanPath)
   142  
   143  }