github.com/orteth01/up@v0.2.0/internal/cli/url/url.go (about)

     1  package open
     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  )
    14  
    15  func init() {
    16  	cmd := root.Command("url", "Show, open, or copy a stage endpoint.")
    17  
    18  	cmd.Example(`up url`, "Show the development endpoint.")
    19  	cmd.Example(`up url --open`, "Open the development endpoint in the browser.")
    20  	cmd.Example(`up url --copy`, "Copy the development endpoint to the clipboard.")
    21  	cmd.Example(`up url production`, "Show the production endpoint.")
    22  	cmd.Example(`up url -o production`, "Open the production endpoint in the browser.")
    23  	cmd.Example(`up url -c production`, "Copy the production endpoint to the clipboard.")
    24  
    25  	stage := cmd.Arg("stage", "Name of the stage.").Default("development").String()
    26  	open := cmd.Flag("open", "Open endpoint in the browser.").Short('o').Bool()
    27  	copy := cmd.Flag("copy", "Copy endpoint to the clipboard.").Short('c').Bool()
    28  
    29  	cmd.Action(func(_ *kingpin.ParseContext) error {
    30  		c, p, err := root.Init()
    31  		if err != nil {
    32  			return errors.Wrap(err, "initializing")
    33  		}
    34  
    35  		region := c.Regions[0]
    36  
    37  		stats.Track("URL", map[string]interface{}{
    38  			"region": region,
    39  			"stage":  *stage,
    40  			"open":   *open,
    41  			"copy":   *copy,
    42  		})
    43  
    44  		url, err := p.URL(region, *stage)
    45  		if err != nil {
    46  			return err
    47  		}
    48  
    49  		switch {
    50  		case *open:
    51  			browser.OpenURL(url)
    52  		case *copy:
    53  			clipboard.Write(url)
    54  			fmt.Println("Copied to clipboard!")
    55  		default:
    56  			fmt.Println(url)
    57  		}
    58  
    59  		return nil
    60  	})
    61  }