Skip to content

Commit

Permalink
Merge pull request #3 from dortlii/feature/save-helm-chart
Browse files Browse the repository at this point in the history
Feature/save helm chart
  • Loading branch information
dortlii authored Sep 28, 2024
2 parents 065c7d4 + b6d19db commit 33abb52
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 10 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# helm-chart-generator
Written in Go this cli tool creates helm chart files with certain standards. A fun project for writing code.

Written in Go this cli tool creates helm chart files with certain standards.
A fun project for writing code.

For now you just can create some basic helm structures and it doesn't provide
any additional functions to the already awesome official `helm` cli.

Maybe some time in the future this project will be an upgrade or benefit for
using it. For the time being, it's just for fun only.

Feel free to add some comments or issues, if you see any major mistakes or
better practices then I did it.

Enjoy!
15 changes: 12 additions & 3 deletions cmd/hcg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ import (
)

func main() {
helmChart, err := generator.NewHelmChart("2", "myChart", "1.0.0", "application")
helmChart, err := generator.NewHelmChart("v2", "myChart", "1.0.0", "application")
if err != nil {
log.Fatalf("Failed to create helm chart: %s", err)
}

// Add some text to the Template Notes for testing
helmChart.Template.Notes.Content = "I'm a note!"

// Add some values to Values for testing
helmChart.Values.Values = map[string]string{
"key": "value",
"anotherKey": "anotherValue",
}

emptyHelmChart, err := generator.NewHelmChart("", "", "", "")
if err != nil {
log.Fatalf("Failed to create helm chart: %s", err)
Expand All @@ -22,6 +31,6 @@ func main() {
fmt.Printf("This is my empty helm chart: %s\n", emptyHelmChart)

// Save to disk
helm := generator.NewHelmFileService("/tmp/", helmChart)
helm.Save()
hfs := generator.NewHelmFileService("/tmp/", helmChart)
hfs.Save()
}
8 changes: 6 additions & 2 deletions generator/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ func (hf HelmFiles) Save() error {
createFolder(helmDestFolder)
}

// Save all files to the filesystem
// TODO: Rethink the save cascading ...
hf.Helm.Save(fullRootHelmPath)

return nil
}

// createFolder helper function
func createFolder(path string) {
if err := os.Mkdir(path, os.ModePerm); err != nil {
func createFolder(folderPath string) {
if err := os.Mkdir(folderPath, os.ModePerm); err != nil {
log.Fatal(err)
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/dortlii/helm-chart-generator

go 1.23

require gopkg.in/yaml.v3 v3.0.1
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
28 changes: 27 additions & 1 deletion helm/chart.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package helm

import (
"log"
"os"
"path"

"gopkg.in/yaml.v3"
)

const (
chartFileName = "Chart.yaml"
)

// Chart item of the helm chart, equals to Chart.yaml
type Chart struct {
// ApiVersion represents the version of the helm chart schema
Expand All @@ -23,7 +35,7 @@ func NewChart() Chart {

// SetDefaults adds default values to a Chart struct
func SetDefaults(chart Chart) *Chart {
chart.ApiVersion = "2"
chart.ApiVersion = "v2"
chart.AppVersion = "0.0.0"
chart.Version = "0.0.0"
chart.Name = "default"
Expand All @@ -32,3 +44,17 @@ func SetDefaults(chart Chart) *Chart {

return &chart
}

// Save method for chart component
func (c Chart) Save(chartPath string) {
yamlData, err := yaml.Marshal(c)
if err != nil {
log.Fatal(err)
}

filePath := path.Join(chartPath, chartFileName)

if err := os.WriteFile(filePath, yamlData, os.ModePerm); err != nil {
log.Fatal(err)
}
}
12 changes: 12 additions & 0 deletions helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ type Helm struct {
// Template of the helm chart
Template Template `yaml:"template"`
}

// ComponentService exposes file operations
type ComponentService interface {
Save(chartPath string)
}

// Save implements the function how to save Values to the disk
func (h Helm) Save(chartPath string) {
h.Chart.Save(chartPath)
h.Values.Save(chartPath)
h.Template.Save(chartPath)
}
23 changes: 21 additions & 2 deletions helm/template.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
package helm

import (
"log"
"os"
"path"
)

const (
notesFileName = "Notes.txt"
)

// Template elements of the helm chart
type Template struct {
// Notes content for the Template
Notes Notes `yaml:"notes"`
Notes Notes
}

// Notes for the helm chart functionality
type Notes struct {
// Content describes the text which gets written to Notes
Content string `yaml:"content"`
Content string
}

// NewTemplate creates an empty template
func NewTemplate() Template {
return Template{}
}

// Save implements the function how to save Template to the disk
func (t Template) Save(chartPath string) {
notesFilePath := path.Join(chartPath, notesFileName)

if err := os.WriteFile(notesFilePath, []byte(t.Notes.Content), os.ModePerm); err != nil {
log.Fatal(err)
}
}
28 changes: 27 additions & 1 deletion helm/values.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
package helm

import (
"log"
"os"
"path"

"gopkg.in/yaml.v3"
)

const (
valuesFileName = "values.yaml"
)

// Values of the helm chart, equals to a values.yaml file
type Values struct {
Values []string `yaml:"values"`
Values map[string]string `yaml:"values"`
}

// NewValues returns a new, empty Values object
func NewValues() Values {
return Values{}
}

// Save implements the function how to save Values to the disk
func (v Values) Save(chartPath string) {
yamlData, err := yaml.Marshal(v)
if err != nil {
log.Fatal(err)
}

filePath := path.Join(chartPath, valuesFileName)

if err := os.WriteFile(filePath, yamlData, os.ModePerm); err != nil {
log.Fatal(err)
}
}

0 comments on commit 33abb52

Please sign in to comment.