-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
179 lines (157 loc) · 4.35 KB
/
main.go
File metadata and controls
179 lines (157 loc) · 4.35 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"context"
"fmt"
"log/slog"
"os"
"strings"
"time"
"github.com/rrgmc/helm-render-ui/helm"
"github.com/urfave/cli/v3"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
"sigs.k8s.io/yaml"
)
func main() {
ctx := context.Background()
if err := run(ctx); err != nil {
slog.ErrorContext(ctx, "error running command", "error", err)
}
}
func run(ctx context.Context) error {
cmd := &cli.Command{
Name: "helm-render-ui",
ArgsUsage: "[helm chart folder]",
Arguments: []cli.Argument{
&cli.StringArgs{
Name: "helm-chart-folder",
UsageText: "A folder containing a Chart.yaml file",
Min: 1,
Max: 1,
},
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Usage: "helm repository URL. If set, the folder name parameter will be used as the chart name",
},
&cli.StringFlag{
Name: "chart-version",
Usage: "chart version (if downloading from repository)",
},
&cli.StringFlag{
Name: "namespace",
Aliases: []string{"n"},
Usage: "namespace",
Value: "default",
},
&cli.StringFlag{
Name: "release",
Aliases: []string{"r"},
Usage: "release name",
},
&cli.StringSliceFlag{
Name: "values",
Aliases: []string{"f"},
Usage: "extra configuration values file name",
},
&cli.IntFlag{
Name: "http-port",
Aliases: []string{"p"},
Usage: "http port",
},
&cli.BoolFlag{
Name: "is-upgrade",
Usage: "sets upgrade mode",
Value: false,
},
&cli.BoolFlag{
Name: "dev-port",
Usage: "dev http port",
Hidden: true,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
chartRepo := command.String("repo")
chartFolder := command.StringArgs("helm-chart-folder")[0]
if strings.TrimSpace(chartFolder) == "" {
return fmt.Errorf("helm chart folder is required")
}
var err error
var cht *chart.Chart
var chartVersions []string
if chartRepo != "" {
chartName := chartFolder
slog.InfoContext(ctx, "loading chart from repository",
"repo", chartRepo,
"chart", chartFolder,
"version", command.String("chart-version"))
repository, err := helm.LoadRepository(chartRepo)
if err != nil {
return err
}
latestChart, err := repository.GetChart(chartFolder, command.String("chart-version"))
if err != nil {
return err
}
latestChartFiles, err := latestChart.Download()
if err != nil {
return err
}
defer latestChartFiles.Close()
chartFolder = latestChartFiles.ChartPath()
for entry, err := range repository.ChartVersions(chartName, 20) {
if err != nil {
slog.Warn("error listing chart versions", "error", err)
break
}
var date string
if !entry.Created.IsZero() {
date = fmt.Sprintf(" [%s]", entry.Created.Format(time.RFC3339))
}
chartVersions = append(chartVersions, fmt.Sprintf("%s%s", entry.Version, date))
}
}
cht, err = loader.LoadDir(chartFolder)
if err != nil {
return fmt.Errorf("error loading chart from folder: %w", err)
}
var displayValueFiles []string
values := map[string]any{}
for _, valueFile := range command.StringSlice("values") {
currentMap := map[string]interface{}{}
displayValueFiles = append(displayValueFiles, ensureRelativePath(strings.TrimPrefix(valueFile, chartFolder)))
bytes, err := os.ReadFile(valueFile)
if err != nil {
return err
}
if err := yaml.Unmarshal(bytes, ¤tMap); err != nil {
return fmt.Errorf("failed to parse %s: %w", valueFile, err)
}
// Merge with the previous map
// values = mergeMaps(values, currentMap)
chartutil.CoalesceTables(values, currentMap)
}
if err := chartutil.ProcessDependencies(cht, values); err != nil {
return err
}
options := chartutil.ReleaseOptions{
Name: command.String("release"),
Namespace: command.String("namespace"),
Revision: 1,
IsInstall: !command.Bool("is-upgrade"),
IsUpgrade: command.Bool("is-upgrade"),
}
if options.Name == "" {
options.Name = cht.Metadata.Name
}
httpPort := command.Int("http-port")
if command.Bool("dev-port") {
httpPort = devHTTPPort
}
return runHTTP(ctx, httpPort, cht, displayValueFiles, values, options, chartVersions)
},
}
return cmd.Run(ctx, os.Args)
}