2
0

open.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/utils"
  8. "github.com/spf13/cobra"
  9. )
  10. func registerCommand_Open() *cobra.Command {
  11. openCmd := &cobra.Command{
  12. Use: "open",
  13. Short: "Opens the browser at the currently set Porter instance",
  14. RunE: func(cmd *cobra.Command, args []string) error {
  15. ctx := cmd.Context()
  16. cliConf, _, err := currentProfileIncludingFlags(cmd)
  17. if err != nil {
  18. return fmt.Errorf("error getting current profile config: %w", err)
  19. }
  20. client, err := api.NewClientWithConfig(ctx, api.NewClientInput{
  21. BaseURL: fmt.Sprintf("%s/api", cliConf.Host),
  22. BearerToken: cliConf.Token,
  23. CookieFileName: "cookie.json",
  24. })
  25. if err != nil {
  26. _, _ = color.New(color.FgRed).Fprintf(os.Stderr, "error creating porter API client: %v\n", err)
  27. return err
  28. }
  29. user, err := client.AuthCheck(ctx)
  30. if err != nil {
  31. _ = utils.OpenBrowser(fmt.Sprintf("%s/register", cliConf.Host))
  32. return nil
  33. }
  34. _ = 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
  35. return nil
  36. },
  37. }
  38. return openCmd
  39. }