github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/internal/cli/url/url.go (about)

     1  package url
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pkg/browser"
     7  	"github.com/pkg/errors"
     8  	"github.com/tj/go/clipboard"
     9  	"github.com/tj/kingpin"
    10  
    11  	"github.com/apex/up/internal/cli/root"
    12  	"github.com/apex/up/internal/stats"
    13  	"github.com/apex/up/internal/util"
    14  	"github.com/apex/up/internal/validate"
    15  )
    16  
    17  func init() {
    18  	cmd := root.Command("url", "Show, open, or copy a stage endpoint.")
    19  
    20  	cmd.Example(`up url`, "Show the staging endpoint.")
    21  	cmd.Example(`up url --open`, "Open the staging endpoint in the browser.")
    22  	cmd.Example(`up url --copy`, "Copy the staging endpoint to the clipboard.")
    23  	cmd.Example(`up url -s production`, "Show the production endpoint.")
    24  	cmd.Example(`up url -o -s production`, "Open the production endpoint in the browser.")
    25  	cmd.Example(`up url -c -s production`, "Copy the production endpoint to the clipboard.")
    26  
    27  	stage := cmd.Flag("stage", "Target stage name.").Short('s').Default("staging").String()
    28  	open := cmd.Flag("open", "Open endpoint in the browser.").Short('o').Bool()
    29  	copy := cmd.Flag("copy", "Copy endpoint to the clipboard.").Short('c').Bool()
    30  
    31  	cmd.Action(func(_ *kingpin.ParseContext) error {
    32  		c, p, err := root.Init()
    33  		if err != nil {
    34  			return errors.Wrap(err, "initializing")
    35  		}
    36  
    37  		region := c.Regions[0]
    38  
    39  		stats.Track("URL", map[string]interface{}{
    40  			"region": region,
    41  			"stage":  *stage,
    42  			"open":   *open,
    43  			"copy":   *copy,
    44  		})
    45  
    46  		if err := validate.List(*stage, c.Stages.RemoteNames()); err != nil {
    47  			return err
    48  		}
    49  
    50  		url, err := p.URL(region, *stage)
    51  		if err != nil {
    52  			return err
    53  		}
    54  
    55  		switch {
    56  		case *open:
    57  			browser.OpenURL(url)
    58  		case *copy:
    59  			clipboard.Write(url)
    60  			util.LogPad("Copied to clipboard!")
    61  		default:
    62  			fmt.Println(url)
    63  		}
    64  
    65  		return nil
    66  	})
    67  }