forked from opskumu/helm-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.go
107 lines (96 loc) · 2.32 KB
/
charts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"strings"
"github.com/gin-gonic/gin"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
)
var readmeFileNames = []string{"readme.md", "readme.txt", "readme"}
type file struct {
Name string `json:"name"`
Data string `json:"data"`
}
func findReadme(files []*chart.File) (file *chart.File) {
for _, file := range files {
for _, n := range readmeFileNames {
if strings.EqualFold(file.Name, n) {
return file
}
}
}
return nil
}
func showChartInfo(c *gin.Context) {
name := c.Query("chart")
if name == "" {
respErr(c, fmt.Errorf("chart name can not be empty"))
return
}
// local charts with abs path *.tgz
splitChart := strings.Split(name, ".")
if splitChart[len(splitChart)-1] == "tgz" {
name = helmConfig.UploadPath + "/" + name
}
info := c.Query("info") // all, readme, values, chart
if info == "" {
info = string(action.ShowAll)
}
version := c.Query("version")
client := action.NewShow(action.ShowAll)
client.Version = version
if info == string(action.ShowChart) {
client.OutputFormat = action.ShowChart
} else if info == string(action.ShowReadme) {
client.OutputFormat = action.ShowReadme
} else if info == string(action.ShowValues) {
client.OutputFormat = action.ShowValues
} else if info == string(action.ShowAll) {
client.OutputFormat = action.ShowAll
} else {
respErr(c, fmt.Errorf("bad info %s, chart info only support readme/values/chart", info))
return
}
cp, err := client.ChartPathOptions.LocateChart(name, settings)
if err != nil {
respErr(c, err)
return
}
chrt, err := loader.Load(cp)
if err != nil {
respErr(c, err)
return
}
if client.OutputFormat == action.ShowChart {
respOK(c, chrt.Metadata)
return
}
if client.OutputFormat == action.ShowValues {
var values string
for _, v := range chrt.Raw {
if v.Name == chartutil.ValuesfileName {
values = string(v.Data)
break
}
}
respOK(c, values)
return
}
if client.OutputFormat == action.ShowReadme {
respOK(c, string(findReadme(chrt.Files).Data))
return
}
if client.OutputFormat == action.ShowAll {
values := make([]*file, 0, len(chrt.Raw))
for _, v := range chrt.Raw {
values = append(values, &file{
Name: v.Name,
Data: string(v.Data),
})
}
respOK(c, values)
return
}
}