Skip to content

Commit d5fa8e3

Browse files
authored
Merge pull request #2 from origranot/feat/lint
feat: add helm lint option to support sub charts
2 parents 9983855 + 44c0cfa commit d5fa8e3

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

cmd/common.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ func createDependencyCommand(name, desc string) *cobra.Command {
2121
cmd.PrintErrln(err)
2222
return
2323
}
24-
err = helmutil.ManageDependencies(absPath, helmutil.Command(name))
24+
if name == "lint" {
25+
err = helmutil.LintCharts(absPath)
26+
} else {
27+
err = helmutil.ManageDependencies(absPath, helmutil.DependencyCommand(name))
28+
}
29+
2530
if err != nil {
2631
cmd.PrintErrln(err)
2732
}

cmd/lint.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cmd
2+
3+
var lintCmd = createDependencyCommand("lint", "Lint chart including his subcharts with correct values scope (this must be run from the root of the chart)")
4+
5+
func init() {
6+
rootCmd.AddCommand(lintCmd)
7+
}

pkg/dependencies.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
"github.com/fatih/color"
1313
)
1414

15-
type Command string
15+
type DependencyCommand string
1616

1717
const (
18-
Build Command = "build"
19-
Update Command = "update"
18+
Build DependencyCommand = "build"
19+
Update DependencyCommand = "update"
2020
)
2121

22-
func ManageDependencies(rootDir string, cmd Command) error {
22+
func ManageDependencies(rootDir string, cmd DependencyCommand) error {
2323
var charts []ChartInfo
2424
err := collectCharts(rootDir, &charts)
2525
if err != nil {
@@ -42,20 +42,40 @@ func ManageDependencies(rootDir string, cmd Command) error {
4242
fmt.Printf("%s\n", color.YellowString(stderr.String()))
4343
}
4444

45-
var successMsg string
46-
if cmd == Build {
47-
successMsg = "Successfully built"
48-
} else {
49-
successMsg = "Successfully updated"
45+
successMsg := fmt.Sprintf("dependencies %sd successfully", cmd)
46+
fmt.Printf("%s %s\n", color.BlueString(chart.Name), successMsg)
47+
}
48+
return nil
49+
}
50+
51+
func LintCharts(rootDir string) error {
52+
var charts []ChartInfo
53+
err := collectCharts(rootDir, &charts)
54+
if err != nil {
55+
return err
56+
}
57+
58+
for i := len(charts) - 1; i >= 0; i-- {
59+
chart := charts[i]
60+
helmCmd := exec.Command("helm", "lint", chart.Path, "--values", path.Join(rootDir, "values.yaml"))
61+
62+
var stdout, stderr bytes.Buffer
63+
helmCmd.Stdout = &stdout
64+
helmCmd.Stderr = &stderr
65+
66+
if err := helmCmd.Run(); err != nil {
67+
return fmt.Errorf("failed to lint chart %s:\n%s", chart.Path, color.RedString(stderr.String()))
68+
}
69+
if stderr.String() != "" {
70+
fmt.Printf("%s\n", color.YellowString(stderr.String()))
5071
}
5172

52-
fmt.Printf("%s %s\n", color.BlueString(chart.Name), successMsg)
73+
fmt.Printf("%s linted successfully\n", color.BlueString(chart.Name))
5374
}
5475
return nil
5576
}
5677

5778
func collectCharts(rootDir string, charts *[]ChartInfo) error {
58-
5979
_, err := os.Stat(path.Join(rootDir, "Chart.yaml"))
6080
if err != nil {
6181
return fmt.Errorf("chart not found: %s", rootDir)

plugin.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: "cascade"
2-
version: "0.0.3"
2+
version: "1.0.0"
33
usage: "Recursively manages Helm chart dependencies across all subcharts"
44
description: "Recursively manages Helm chart dependencies across all subcharts"
55
ignoreFlags: false

0 commit comments

Comments
 (0)