2
0

errors.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package commands
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "github.com/fatih/color"
  9. api "github.com/porter-dev/porter/api/client"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/cli/cmd/config"
  12. cliErrors "github.com/porter-dev/porter/cli/cmd/errors"
  13. "github.com/spf13/cobra"
  14. )
  15. var (
  16. ErrNotLoggedIn error = errors.New("You are not logged in.")
  17. ErrCannotConnect error = errors.New("Unable to connect to the Porter server.")
  18. )
  19. type authenticatedRunnerFunc func(ctx context.Context, user *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error
  20. func checkLoginAndRunWithConfig(cmd *cobra.Command, cliConf config.CLIConfig, args []string, runner authenticatedRunnerFunc) error {
  21. ctx := cmd.Context()
  22. cliConf = overrideConfigWithFlags(cmd, cliConf)
  23. red := color.New(color.FgRed)
  24. client, err := api.NewClientWithConfig(ctx, api.NewClientInput{
  25. BaseURL: fmt.Sprintf("%s/api", cliConf.Host),
  26. BearerToken: cliConf.Token,
  27. CookieFileName: "cookie.json",
  28. })
  29. if err != nil {
  30. red.Print("You are not logged in. Log in using \"porter auth login\"\n") // nolint:errcheck,gosec
  31. return fmt.Errorf("error creating porter API client: %w", err)
  32. }
  33. user, err := client.AuthCheck(ctx)
  34. if err != nil {
  35. if strings.Contains(err.Error(), "Forbidden") {
  36. red.Print("You are not logged in. Log in using \"porter auth login\"\n")
  37. return ErrNotLoggedIn
  38. } else if strings.Contains(err.Error(), "connection refused") {
  39. red.Printf("Unable to connect to the Porter server at %s\n", cliConf.Host)
  40. red.Print("To set a different host, run \"porter config set-host [HOST]\"\n")
  41. red.Print("To start a local server, run \"porter server start\"\n")
  42. return ErrCannotConnect
  43. }
  44. red.Fprintf(os.Stderr, "Error: %v\n", err.Error())
  45. return err
  46. }
  47. project, err := client.GetProject(ctx, cliConf.Project)
  48. if err != nil {
  49. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run: %w", err)
  50. }
  51. if project == nil {
  52. return fmt.Errorf("project [%d] not found", cliConf.Project)
  53. }
  54. flags := config.FeatureFlags{
  55. ValidateApplyV2Enabled: project.ValidateApplyV2,
  56. }
  57. err = runner(ctx, user, client, cliConf, flags, cmd, args)
  58. if err != nil {
  59. red := color.New(color.FgRed)
  60. if strings.Contains(err.Error(), "403") {
  61. red.Print("You do not have the necessary permissions to view this resource")
  62. return nil
  63. } else if strings.Contains(err.Error(), "connection refused") {
  64. red.Printf("Unable to connect to the Porter server at %s\n", cliConf.Host)
  65. red.Print("To set a different host, run \"porter config set-host [HOST]\"")
  66. red.Print("To start a local server, run \"porter server start\"")
  67. return nil
  68. }
  69. if errors.Is(err, context.Canceled) {
  70. color.New(color.FgYellow).Println("Command was canceled") // nolint:errcheck,gosec
  71. return nil
  72. }
  73. cliErrors.GetErrorHandler(cliConf).HandleError(err)
  74. return err
  75. }
  76. return nil
  77. }