errors.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, currentProfile string, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error
  20. var errCreatingPorterAPIClient = errors.New("unable to authenticate against server")
  21. func checkLoginAndRunWithConfig(cmd *cobra.Command, args []string, runner authenticatedRunnerFunc) error {
  22. ctx := cmd.Context()
  23. cliConf, currentProfile, err := currentProfileIncludingFlags(cmd)
  24. if err != nil {
  25. return fmt.Errorf("error whilst initialising config: %w", err)
  26. }
  27. client, err := api.NewClientWithConfig(ctx, api.NewClientInput{
  28. BaseURL: fmt.Sprintf("%s/api", cliConf.Host),
  29. BearerToken: cliConf.Token,
  30. CookieFileName: "cookie.json",
  31. })
  32. if err != nil {
  33. return errCreatingPorterAPIClient
  34. }
  35. user, err := client.AuthCheck(ctx)
  36. if err != nil {
  37. red := color.New(color.FgRed)
  38. if strings.Contains(err.Error(), "Forbidden") {
  39. red.Print("You are not logged in. Log in using \"porter auth login\"\n")
  40. return ErrNotLoggedIn
  41. } else if strings.Contains(err.Error(), "connection refused") {
  42. red.Printf("Unable to connect to the Porter server at %s\n", cliConf.Host)
  43. red.Print("To set a different host, run \"porter config set-host [HOST]\"\n")
  44. red.Print("To start a local server, run \"porter server start\"\n")
  45. return ErrCannotConnect
  46. }
  47. red.Fprintf(os.Stderr, "Error: %v\n", err.Error())
  48. return err
  49. }
  50. project, err := client.GetProject(ctx, cliConf.Project)
  51. if err != nil {
  52. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run: %w", err)
  53. }
  54. if project == nil {
  55. return fmt.Errorf("project [%d] not found", cliConf.Project)
  56. }
  57. flags := config.FeatureFlags{
  58. ValidateApplyV2Enabled: project.ValidateApplyV2,
  59. }
  60. err = runner(ctx, user, client, cliConf, currentProfile, flags, cmd, args)
  61. if err != nil {
  62. red := color.New(color.FgRed)
  63. if strings.Contains(err.Error(), "403") {
  64. red.Print("You do not have the necessary permissions to view this resource")
  65. return nil
  66. } else if strings.Contains(err.Error(), "connection refused") {
  67. red.Printf("Unable to connect to the Porter server at %s\n", cliConf.Host)
  68. red.Print("To set a different host, run \"porter config set-host [HOST]\"")
  69. red.Print("To start a local server, run \"porter server start\"")
  70. return nil
  71. }
  72. cliErrors.GetErrorHandler(cliConf, currentProfile).HandleError(err)
  73. return err
  74. }
  75. return nil
  76. }
  77. // currentProfileIncludingFlags returns the current profile, and initialises the config.
  78. // 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
  79. func currentProfileIncludingFlags(cmd *cobra.Command) (config.CLIConfig, string, error) {
  80. ctx := cmd.Context()
  81. flagsConfig := parseRootConfigFlags(cmd)
  82. profile, err := cmd.Flags().GetString("profile")
  83. if err != nil {
  84. return config.CLIConfig{}, "", fmt.Errorf("error getting profile flag: %w", err)
  85. }
  86. cliConfig, currentProfile, err := config.InitAndLoadConfig(ctx, profile, flagsConfig)
  87. if err != nil {
  88. return config.CLIConfig{}, "", fmt.Errorf("error whilst initialising config: %w", err)
  89. }
  90. return cliConfig, currentProfile, nil
  91. }