template_handler.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package api
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "compress/gzip"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "net/http"
  12. "strings"
  13. "gopkg.in/yaml.v2"
  14. )
  15. var baseURL string = "https://porter-dev.github.io/chart-repo/"
  16. // IndexYAML represents a chart repo's index.yaml
  17. type IndexYAML struct {
  18. APIVersion string `yaml:"apiVersion"`
  19. Generated string `yaml:"generated"`
  20. Entries map[interface{}]ChartYAML `yaml:"entries"`
  21. }
  22. // ChartYAML represents the data for chart in index.yaml
  23. type ChartYAML []struct {
  24. APIVersion string `yaml:"apiVersion"`
  25. AppVersion string `yaml:"appVersion"`
  26. Created string `yaml:"created"`
  27. Description string `yaml:"description"`
  28. Digest string `yaml:"digest"`
  29. Icon string `yaml:"icon"`
  30. Name string `yaml:"name"`
  31. Type string `yaml:"type"`
  32. Urls []string `yaml:"urls"`
  33. Version string `yaml:"version"`
  34. }
  35. // PorterChart represents a bundled Porter template
  36. type PorterChart struct {
  37. Name string
  38. Description string
  39. Icon string
  40. Form FormYAML
  41. }
  42. // FormYAML represents a chart's values.yaml form abstraction
  43. type FormYAML struct {
  44. Name string `yaml:"name"`
  45. Description string `yaml:"description"`
  46. Tags []string `yaml:"tags"`
  47. Sections []struct {
  48. Name string `yaml:"name"`
  49. Contents []struct {
  50. Type string `yaml:"type"`
  51. Label string `yaml:"label"`
  52. Name string `yaml:"name,omitempty"`
  53. Variable string `yaml:"variable,omitempty"`
  54. Settings struct {
  55. Default int `yaml:"default"`
  56. } `yaml:"settings,omitempty"`
  57. } `yaml:"contents"`
  58. } `yaml:"sections"`
  59. }
  60. // HandleListTemplates retrieves a list of Porter templates
  61. func (app *App) HandleListTemplates(w http.ResponseWriter, r *http.Request) {
  62. resp, err := http.Get(baseURL + "index.yaml")
  63. if err != nil {
  64. fmt.Println(err)
  65. return
  66. }
  67. defer resp.Body.Close()
  68. body, _ := ioutil.ReadAll(resp.Body)
  69. form := IndexYAML{}
  70. if err := yaml.Unmarshal([]byte(body), &form); err != nil {
  71. fmt.Println(err)
  72. return
  73. }
  74. // Loop over charts in index.yaml
  75. porterCharts := []PorterChart{}
  76. for k := range form.Entries {
  77. indexChart := form.Entries[k][0]
  78. tarURL := indexChart.Urls[0]
  79. if !strings.Contains(tarURL, "http://") {
  80. tarURL = baseURL + tarURL
  81. }
  82. formData, err := getFormData(tarURL)
  83. if err != nil {
  84. fmt.Println(err)
  85. return
  86. }
  87. porterChart := PorterChart{}
  88. porterChart.Name = indexChart.Name
  89. porterChart.Description = indexChart.Description
  90. porterChart.Icon = indexChart.Icon
  91. porterChart.Form = *formData
  92. porterCharts = append(porterCharts, porterChart)
  93. }
  94. json.NewEncoder(w).Encode(porterCharts)
  95. }
  96. func getFormData(tarURL string) (*FormYAML, error) {
  97. resp, err := http.Get(tarURL)
  98. if err != nil {
  99. fmt.Println(err)
  100. return nil, err
  101. }
  102. defer resp.Body.Close()
  103. body, _ := ioutil.ReadAll(resp.Body)
  104. buf := bytes.NewBuffer(body)
  105. gzf, err := gzip.NewReader(buf)
  106. if err != nil {
  107. fmt.Println(err)
  108. return nil, err
  109. }
  110. // Process tarball to generate FormYAML
  111. tarReader := tar.NewReader(gzf)
  112. for {
  113. header, err := tarReader.Next()
  114. if err == io.EOF {
  115. break
  116. } else if err != nil {
  117. fmt.Println(err)
  118. return nil, err
  119. }
  120. name := header.Name
  121. switch header.Typeflag {
  122. case tar.TypeDir:
  123. continue
  124. case tar.TypeReg:
  125. // Handle form.yaml located in archive
  126. if strings.Contains(name, "form.yaml") {
  127. bufForm := new(bytes.Buffer)
  128. _, err := io.Copy(bufForm, tarReader)
  129. if err != nil {
  130. fmt.Println(err)
  131. return nil, err
  132. }
  133. // Unmarshal yaml byte buffer
  134. form := FormYAML{}
  135. if err := yaml.Unmarshal(bufForm.Bytes(), &form); err != nil {
  136. fmt.Println(err)
  137. return nil, err
  138. }
  139. return &form, nil
  140. }
  141. default:
  142. fmt.Printf("%s : %c %s %s\n",
  143. "Unknown type",
  144. header.Typeflag,
  145. "in file",
  146. name,
  147. )
  148. }
  149. }
  150. return nil, errors.New("no form.yaml found")
  151. }