github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/open/open.go (about)

     1  package open
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/henvic/browser"
     9  	"github.com/henvic/wedeploycli/cmdflagsfromhost"
    10  	"github.com/henvic/wedeploycli/command/internal/we"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var setupHost = cmdflagsfromhost.SetupHost{
    15  	Pattern: cmdflagsfromhost.FullHostPattern,
    16  
    17  	Requires: cmdflagsfromhost.Requires{
    18  		Project: true,
    19  		Service: true,
    20  	},
    21  
    22  	PromptMissingService: true,
    23  }
    24  
    25  // OpenCmd opens the browser on a service page
    26  var OpenCmd = &cobra.Command{
    27  	Use:     "open",
    28  	Short:   "Open service on your browser",
    29  	Args:    cobra.NoArgs,
    30  	PreRunE: openPreRun,
    31  	RunE:    openRun,
    32  }
    33  
    34  func init() {
    35  	setupHost.Init(OpenCmd)
    36  }
    37  
    38  func openPreRun(cmd *cobra.Command, args []string) error {
    39  	return setupHost.Process(context.Background(), we.Context())
    40  }
    41  
    42  func openRun(cmd *cobra.Command, args []string) error {
    43  	var link = fmt.Sprintf("https://%s", setupHost.Host())
    44  	err := browser.OpenURL(link)
    45  
    46  	if err != nil {
    47  		_, _ = fmt.Fprintf(os.Stderr, "Failed to open %v", link)
    48  		return err
    49  	}
    50  
    51  	fmt.Println("Service opened on your browser.")
    52  	return nil
    53  }