parse.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package porter_app
  2. import (
  3. "context"
  4. v2 "github.com/porter-dev/porter/internal/porter_app/v2"
  5. "sigs.k8s.io/yaml"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/porter-dev/porter/internal/telemetry"
  8. )
  9. // PorterYamlVersion is a string type for the version of the porter yaml
  10. type PorterYamlVersion string
  11. const (
  12. // PorterYamlVersion_V2 is the v2 version of the porter yaml
  13. PorterYamlVersion_V2 PorterYamlVersion = "v2"
  14. )
  15. // ParseYAML converts a Porter YAML file into a PorterApp proto object
  16. func ParseYAML(ctx context.Context, porterYaml []byte) (*porterv1.PorterApp, error) {
  17. ctx, span := telemetry.NewSpan(ctx, "porter-app-parse-yaml")
  18. defer span.End()
  19. if porterYaml == nil {
  20. return nil, telemetry.Error(ctx, span, nil, "porter yaml is nil")
  21. }
  22. version := &yamlVersion{}
  23. err := yaml.Unmarshal(porterYaml, version)
  24. if err != nil {
  25. return nil, telemetry.Error(ctx, span, err, "error unmarshaling porter yaml")
  26. }
  27. var appProto *porterv1.PorterApp
  28. switch version.Version {
  29. case PorterYamlVersion_V2:
  30. appProto, err = v2.AppProtoFromYaml(ctx, porterYaml)
  31. if err != nil {
  32. return nil, telemetry.Error(ctx, span, err, "error converting v2 yaml to proto")
  33. }
  34. default:
  35. return nil, telemetry.Error(ctx, span, nil, "porter yaml version not supported")
  36. }
  37. if appProto == nil {
  38. return nil, telemetry.Error(ctx, span, nil, "porter yaml is nil")
  39. }
  40. return appProto, nil
  41. }
  42. // yamlVersion is a struct used to unmarshal the version field of a Porter YAML file
  43. type yamlVersion struct {
  44. Version PorterYamlVersion `yaml:"version"`
  45. }