open.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package commands
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/fatih/color"
  6. api "github.com/porter-dev/porter/api/client"
  7. "github.com/porter-dev/porter/cli/cmd/config"
  8. "github.com/porter-dev/porter/cli/cmd/utils"
  9. "github.com/spf13/cobra"
  10. )
  11. func registerCommand_Open(cliConf config.CLIConfig) *cobra.Command {
  12. openCmd := &cobra.Command{
  13. Use: "open",
  14. Short: "Opens the browser at the currently set Porter instance",
  15. Run: func(cmd *cobra.Command, args []string) {
  16. ctx := cmd.Context()
  17. client, err := api.NewClientWithConfig(ctx, api.NewClientInput{
  18. BaseURL: fmt.Sprintf("%s/api", cliConf.Host),
  19. BearerToken: cliConf.Token,
  20. CookieFileName: "cookie.json",
  21. })
  22. if err != nil {
  23. _, _ = color.New(color.FgRed).Fprintf(os.Stderr, "error creating porter API client: %v\n", err)
  24. os.Exit(1)
  25. }
  26. user, err := client.AuthCheck(ctx)
  27. if err != nil {
  28. _ = utils.OpenBrowser(fmt.Sprintf("%s/register", cliConf.Host))
  29. return
  30. }
  31. _ = utils.OpenBrowser(fmt.Sprintf("%s/login?email=%s", cliConf.Host, user.Email)) //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error
  32. },
  33. }
  34. return openCmd
  35. }