errors.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package commands
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "github.com/fatih/color"
  8. api "github.com/porter-dev/porter/api/client"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/cli/cmd/config"
  11. cliErrors "github.com/porter-dev/porter/cli/cmd/errors"
  12. "github.com/spf13/cobra"
  13. )
  14. var (
  15. ErrNotLoggedIn error = errors.New("You are not logged in.")
  16. ErrCannotConnect error = errors.New("Unable to connect to the Porter server.")
  17. )
  18. type authenticatedRunnerFunc func(ctx context.Context, user *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, currentProfile string, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error
  19. var errCreatingPorterAPIClient = errors.New("unable to authenticate against server")
  20. func checkLoginAndRunWithConfig(cmd *cobra.Command, args []string, runner authenticatedRunnerFunc) error {
  21. ctx := cmd.Context()
  22. cliConf, currentProfile, err := currentProfileIncludingFlags(cmd)
  23. if err != nil {
  24. return fmt.Errorf("error whilst initialising config: %w", err)
  25. }
  26. client, err := api.NewClientWithConfig(ctx, api.NewClientInput{
  27. BaseURL: fmt.Sprintf("%s/api", cliConf.Host),
  28. BearerToken: cliConf.Token,
  29. CookieFileName: "cookie.json",
  30. })
  31. if err != nil {
  32. return errCreatingPorterAPIClient
  33. }
  34. user, err := client.AuthCheck(ctx)
  35. if err != nil {
  36. red := color.New(color.FgRed)
  37. if strings.Contains(err.Error(), "Forbidden") {
  38. red.Print("You are not logged in. Log in using \"porter auth login\"\n")
  39. return ErrNotLoggedIn
  40. } else if strings.Contains(err.Error(), "connection refused") {
  41. red.Printf("Unable to connect to the Porter server at %s\n", cliConf.Host)
  42. red.Print("To set a different host, run \"porter config set-host [HOST]\"\n")
  43. red.Print("To start a local server, run \"porter server start\"\n")
  44. return ErrCannotConnect
  45. }
  46. return fmt.Errorf("error checking login: %w", err)
  47. }
  48. project, err := client.GetProject(ctx, cliConf.Project)
  49. if err != nil {
  50. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run: %w", err)
  51. }
  52. if project == nil {
  53. return fmt.Errorf("project [%d] not found", cliConf.Project)
  54. }
  55. flags := config.FeatureFlags{
  56. ValidateApplyV2Enabled: project.ValidateApplyV2,
  57. }
  58. err = runner(ctx, user, client, cliConf, currentProfile, flags, cmd, args)
  59. if err != nil {
  60. red := color.New(color.FgRed)
  61. if strings.Contains(err.Error(), "403") {
  62. red.Print("You do not have the necessary permissions to view this resource")
  63. return nil
  64. } else if strings.Contains(err.Error(), "connection refused") {
  65. red.Printf("Unable to connect to the Porter server at %s\n", cliConf.Host)
  66. red.Print("To set a different host, run \"porter config set-host [HOST]\"")
  67. red.Print("To start a local server, run \"porter server start\"")
  68. return nil
  69. }
  70. cliErrors.GetErrorHandler(cliConf, currentProfile).HandleError(err)
  71. return fmt.Errorf("error running command: %w", err)
  72. }
  73. return nil
  74. }
  75. // currentProfileIncludingFlags returns the current profile, and initialises the config.
  76. // This ensures the the current profile is set to the one specified in the flags, env vars, or config in the correct order or precedence
  77. func currentProfileIncludingFlags(cmd *cobra.Command) (config.CLIConfig, string, error) {
  78. ctx := cmd.Context()
  79. flagsConfig := parseRootConfigFlags(cmd)
  80. profile, err := cmd.Flags().GetString("profile")
  81. if err != nil {
  82. return config.CLIConfig{}, "", fmt.Errorf("error getting profile flag: %w", err)
  83. }
  84. cliConfig, currentProfile, err := config.InitAndLoadConfig(ctx, profile, flagsConfig)
  85. if err != nil {
  86. return config.CLIConfig{}, "", fmt.Errorf("error whilst initialising config: %w", err)
  87. }
  88. return cliConfig, currentProfile, nil
  89. }