apply.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package v2
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "github.com/fatih/color"
  10. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  11. api "github.com/porter-dev/porter/api/client"
  12. "github.com/porter-dev/porter/cli/cmd/config"
  13. )
  14. // Apply implements the functionality of the `porter apply` command for validate apply v2 projects
  15. func Apply(ctx context.Context, cliConf *config.CLIConfig, client *api.Client, porterYamlPath string) error {
  16. if len(porterYamlPath) == 0 {
  17. return fmt.Errorf("porter yaml is empty")
  18. }
  19. porterYaml, err := os.ReadFile(filepath.Clean(porterYamlPath))
  20. if err != nil {
  21. return fmt.Errorf("could not read porter yaml file: %w", err)
  22. }
  23. b64YAML := base64.StdEncoding.EncodeToString(porterYaml)
  24. parseResp, err := client.ParseYAML(ctx, cliConf.Project, cliConf.Cluster, b64YAML)
  25. if err != nil {
  26. return fmt.Errorf("error calling parse yaml endpoint: %w", err)
  27. }
  28. if parseResp.B64AppProto == "" {
  29. return errors.New("b64 app proto is empty")
  30. }
  31. targetResp, err := client.DefaultDeploymentTarget(ctx, cliConf.Project, cliConf.Cluster)
  32. if err != nil {
  33. return fmt.Errorf("error calling default deployment target endpoint: %w", err)
  34. }
  35. if targetResp.DeploymentTargetID == "" {
  36. return errors.New("deployment target id is empty")
  37. }
  38. validateResp, err := client.ValidatePorterApp(ctx, cliConf.Project, cliConf.Cluster, parseResp.B64AppProto, targetResp.DeploymentTargetID)
  39. if err != nil {
  40. return fmt.Errorf("error calling validate endpoint: %w", err)
  41. }
  42. if validateResp.ValidatedBase64AppProto == "" {
  43. return errors.New("validated b64 app proto is empty")
  44. }
  45. applyResp, err := client.ApplyPorterApp(ctx, cliConf.Project, cliConf.Cluster, validateResp.ValidatedBase64AppProto, targetResp.DeploymentTargetID)
  46. if err != nil {
  47. return fmt.Errorf("error calling apply endpoint: %w", err)
  48. }
  49. if applyResp.AppRevisionId == "" {
  50. return errors.New("app revision id is empty")
  51. }
  52. if applyResp.CLIAction == porterv1.EnumCLIAction_ENUM_CLI_ACTION_UNSPECIFIED {
  53. return errors.New("cli action is unknown")
  54. }
  55. color.New(color.FgGreen).Printf("Successfully applied Porter YAML as revision %v, next action: %v\n", applyResp.AppRevisionId, applyResp.CLIAction) // nolint:errcheck,gosec
  56. return nil
  57. }