github.com/hhsnopek/up@v0.1.1/internal/cli/url/url.go (about)

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