2
0

utils.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package preview
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "github.com/porter-dev/porter/api/types"
  8. "github.com/porter-dev/porter/cli/cmd/config"
  9. )
  10. type Source struct {
  11. Name string
  12. Repo string
  13. Version string
  14. IsApplication bool
  15. SourceValues map[string]interface{}
  16. }
  17. type Target struct {
  18. AppName string
  19. Project uint
  20. Cluster uint
  21. Namespace string
  22. }
  23. func GetSource(input map[string]interface{}) (*Source, error) {
  24. output := &Source{}
  25. // first read from env vars
  26. output.Name = os.Getenv("PORTER_SOURCE_NAME")
  27. output.Repo = os.Getenv("PORTER_SOURCE_REPO")
  28. output.Version = os.Getenv("PORTER_SOURCE_VERSION")
  29. // next, check for values in the YAML file
  30. if output.Name == "" {
  31. if name, ok := input["name"]; ok {
  32. nameVal, ok := name.(string)
  33. if !ok {
  34. return nil, fmt.Errorf("invalid name provided")
  35. }
  36. output.Name = nameVal
  37. }
  38. }
  39. if output.Name == "" {
  40. return nil, fmt.Errorf("source name required")
  41. }
  42. if output.Repo == "" {
  43. if repo, ok := input["repo"]; ok {
  44. repoVal, ok := repo.(string)
  45. if !ok {
  46. return nil, fmt.Errorf("invalid repo provided")
  47. }
  48. output.Repo = repoVal
  49. }
  50. }
  51. if output.Version == "" {
  52. if version, ok := input["version"]; ok {
  53. versionVal, ok := version.(string)
  54. if !ok {
  55. return nil, fmt.Errorf("invalid version provided")
  56. }
  57. output.Version = versionVal
  58. }
  59. }
  60. // lastly, just put in the defaults
  61. if output.Version == "" {
  62. output.Version = "latest"
  63. }
  64. output.IsApplication = output.Repo == "https://charts.getporter.dev"
  65. if output.Repo == "" {
  66. output.Repo = "https://charts.getporter.dev"
  67. values, err := existsInRepo(output.Name, output.Version, output.Repo)
  68. if err == nil {
  69. // found in "https://charts.getporter.dev"
  70. output.SourceValues = values
  71. output.IsApplication = true
  72. return output, nil
  73. }
  74. output.Repo = "https://chart-addons.getporter.dev"
  75. values, err = existsInRepo(output.Name, output.Version, output.Repo)
  76. if err == nil {
  77. // found in https://chart-addons.getporter.dev
  78. output.SourceValues = values
  79. return output, nil
  80. }
  81. return nil, fmt.Errorf("source does not exist in any repo")
  82. } else {
  83. // we look in the passed-in repo
  84. values, err := existsInRepo(output.Name, output.Version, output.Repo)
  85. if err == nil {
  86. output.SourceValues = values
  87. return output, nil
  88. }
  89. }
  90. return nil, fmt.Errorf("source '%s' does not exist in repo '%s'", output.Name, output.Repo)
  91. }
  92. func GetTarget(input map[string]interface{}) (*Target, error) {
  93. output := &Target{}
  94. // first read from env vars
  95. if projectEnv := os.Getenv("PORTER_PROJECT"); projectEnv != "" {
  96. project, err := strconv.Atoi(projectEnv)
  97. if err != nil {
  98. return nil, err
  99. }
  100. output.Project = uint(project)
  101. }
  102. if clusterEnv := os.Getenv("PORTER_CLUSTER"); clusterEnv != "" {
  103. cluster, err := strconv.Atoi(clusterEnv)
  104. if err != nil {
  105. return nil, err
  106. }
  107. output.Cluster = uint(cluster)
  108. }
  109. output.Namespace = os.Getenv("PORTER_NAMESPACE")
  110. // next, check for values in the YAML file
  111. if output.Project == 0 {
  112. if project, ok := input["project"]; ok {
  113. projectVal, ok := project.(uint)
  114. if !ok {
  115. return nil, fmt.Errorf("project value must be an integer")
  116. }
  117. output.Project = projectVal
  118. }
  119. }
  120. if output.Cluster == 0 {
  121. if cluster, ok := input["cluster"]; ok {
  122. clusterVal, ok := cluster.(uint)
  123. if !ok {
  124. return nil, fmt.Errorf("cluster value must be an integer")
  125. }
  126. output.Cluster = clusterVal
  127. }
  128. }
  129. if output.Namespace == "" {
  130. if namespace, ok := input["namespace"]; ok {
  131. namespaceVal, ok := namespace.(string)
  132. if !ok {
  133. return nil, fmt.Errorf("invalid namespace provided")
  134. }
  135. output.Namespace = namespaceVal
  136. }
  137. }
  138. if appName, ok := input["app_name"]; ok {
  139. appNameVal, ok := appName.(string)
  140. if !ok {
  141. return nil, fmt.Errorf("invalid app_name provided")
  142. }
  143. output.AppName = appNameVal
  144. }
  145. // lastly, just put in the defaults
  146. if output.Project == 0 {
  147. output.Project = config.GetCLIConfig().Project
  148. }
  149. if output.Cluster == 0 {
  150. output.Cluster = config.GetCLIConfig().Cluster
  151. }
  152. if output.Namespace == "" {
  153. output.Namespace = "default"
  154. }
  155. return output, nil
  156. }
  157. func existsInRepo(name, version, url string) (map[string]interface{}, error) {
  158. chart, err := config.GetAPIClient().GetTemplate(
  159. context.Background(),
  160. name, version,
  161. &types.GetTemplateRequest{
  162. TemplateGetBaseRequest: types.TemplateGetBaseRequest{
  163. RepoURL: url,
  164. },
  165. },
  166. )
  167. if err != nil {
  168. return nil, err
  169. }
  170. return chart.Values, nil
  171. }