apply.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package v2
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "github.com/fatih/color"
  9. "github.com/porter-dev/api-contracts/generated/go/helpers"
  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. appProto := &porterv1.PorterApp{}
  17. if len(porterYamlPath) == 0 {
  18. return fmt.Errorf("porter yaml is empty")
  19. }
  20. porterYaml, err := os.ReadFile(filepath.Clean(porterYamlPath))
  21. if err != nil {
  22. return fmt.Errorf("could not read porter yaml file: %w", err)
  23. }
  24. b64YAML := base64.StdEncoding.EncodeToString(porterYaml)
  25. resp, err := client.ParseYAML(ctx, cliConf.Project, cliConf.Cluster, b64YAML)
  26. if err != nil {
  27. return fmt.Errorf("error calling parse yaml endpoint: %w", err)
  28. }
  29. if resp.B64AppProto == "" {
  30. return fmt.Errorf("b64 app proto is empty")
  31. }
  32. decoded, err := base64.StdEncoding.DecodeString(resp.B64AppProto)
  33. if err != nil {
  34. return fmt.Errorf("unable to decode b64 app: %w", err)
  35. }
  36. err = helpers.UnmarshalContractObject(decoded, appProto)
  37. if err != nil {
  38. return fmt.Errorf("unable to unmarshal app: %w", err)
  39. }
  40. color.New(color.FgGreen).Printf("Successfully parsed Porter YAML file %+v\n", appProto) // nolint:errcheck,gosec
  41. return nil
  42. }