Skip to content

Commit

Permalink
[go] parse environment variables (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Reed committed Jan 3, 2022
1 parent 54e4cd9 commit 0acad4c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
38 changes: 25 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ var plotCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
client, err := reckoner.NewClient(courseFile, version, runAll, onlyRun, true, dryRun, createNamespaces, courseSchema)
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
err = client.Plot()
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
},
}
Expand All @@ -111,11 +113,13 @@ var templateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
client, err := reckoner.NewClient(courseFile, version, runAll, onlyRun, false, true, false, courseSchema)
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
tmpl, err := client.TemplateAll()
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
fmt.Println(tmpl)
},
Expand Down Expand Up @@ -149,14 +153,17 @@ var diffCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
client, err := reckoner.NewClient(courseFile, version, runAll, onlyRun, false, true, false, courseSchema)
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
if err := client.UpdateHelmRepos(); err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
err = client.Diff()
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
},
}
Expand All @@ -172,7 +179,8 @@ var lintCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
_, err := reckoner.NewClient(courseFile, version, runAll, onlyRun, false, true, false, courseSchema)
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
klog.Infof("course file %s is good to go!", courseFile)
},
Expand All @@ -187,20 +195,23 @@ var convertCmd = &cobra.Command{
return validateArgs(cmd, args)
},
Run: func(cmd *cobra.Command, args []string) {
newCourse, err := course.ConvertV1toV2(courseFile)
newCourse, err := course.OpenCourseV2(courseFile, courseSchema)
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
w := os.Stdout
if inPlaceConvert {
f, err := os.OpenFile(courseFile, os.O_RDWR, 0644)
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
defer f.Close()
err = f.Truncate(0)
if err != nil {
klog.Fatalf("failed to truncate course file \"%s\": %s", courseFile, err)
color.Red("failed to truncate course file \"%s\": %s", courseFile, err)
os.Exit(1)
}
w = f
}
Expand All @@ -211,7 +222,8 @@ var convertCmd = &cobra.Command{

err = e.Encode(newCourse)
if err != nil {
klog.Fatal(err)
color.Red(err.Error())
os.Exit(1)
}
},
}
Expand Down
39 changes: 34 additions & 5 deletions pkg/course/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/fatih/color"
"github.com/xeipuuv/gojsonschema"
Expand Down Expand Up @@ -184,8 +186,8 @@ type RepositoryV1 struct {
// RepositoryV1List is a set of repositories
type RepositoryV1List map[string]Repository

// ConvertV1toV2 converts the old python course file to the newer golang v2 schema
func ConvertV1toV2(fileName string) (*FileV2, error) {
// convertV1toV2 converts the old python course file to the newer golang v2 schema
func convertV1toV2(fileName string) (*FileV2, error) {
newFile := &FileV2{
SchemaVersion: "v2",
}
Expand Down Expand Up @@ -261,14 +263,25 @@ func OpenCourseV2(fileName string, schema []byte) (*FileV2, error) {
}
if courseFile.SchemaVersion != "v2" {
klog.V(2).Infof("did not detect v2 course file - trying conversion from v1")
fileV2, errConvert := ConvertV1toV2(fileName)
fileV2, errConvert := convertV1toV2(fileName)
if errConvert != nil {
return nil, fmt.Errorf("could not unmarshal file from v1 or v2 schema")
return nil, fmt.Errorf("could not unmarshal file from v1 or v2 schema:\n\t%s", errConvert.Error())
}
color.Yellow("WARNING: this course file was automatically converted from v1 to v2 at runtime - to convert the file permanently, run \"reckoner convert -i %s\"", fileName)
courseFile = fileV2
}

// Marshal back here just so we can populate the env vars without any yaml comments present
data, _ = yaml.Marshal(courseFile)
data, err = parseEnv(data)
if err != nil {
return nil, fmt.Errorf("failed to parse env variables: %v", err)
}
err = yaml.Unmarshal(data, courseFile)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal file after parsing env vars: %s", err.Error())
}

courseFile.populateDefaultNamespace()
courseFile.populateDefaultRepository()
courseFile.populateEmptyChartNames()
Expand All @@ -295,7 +308,6 @@ func OpenCourseV1(fileName string) (*FileV1, error) {
if err != nil {
return nil, err
}

return courseFile, nil
}

Expand Down Expand Up @@ -384,3 +396,20 @@ func (f FileV2) validateJsonSchema(schemaData []byte) error {
}
return nil
}

func parseEnv(data []byte) ([]byte, error) {
dataWithEnv := os.Expand(string(data), envMapper)
if strings.Contains(dataWithEnv, "_ENV_NOT_SET_") {
return nil, fmt.Errorf("course has env variables that are not properly set")
}
return []byte(dataWithEnv), nil
}

func envMapper(key string) string {
v := os.Getenv(key)
if v == "" {
color.Red("ERROR: environment variable %s is not set", key)
return "_ENV_NOT_SET_"
}
return v
}
2 changes: 1 addition & 1 deletion pkg/course/course_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestConvertV1toV2(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ConvertV1toV2(tt.fileName)
got, err := convertV1toV2(tt.fileName)
if tt.wantErr {
assert.Error(t, err)
} else {
Expand Down
3 changes: 1 addition & 2 deletions pkg/reckoner/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ func (c Client) Plot() error {
if !c.DryRun {
out, stdErr, err := c.Helm.Exec(args...)
if err != nil {
klog.Error(stdErr)
continue
return fmt.Errorf("error plotting release %s: %s", release.Name, stdErr)
}
fmt.Println(out)
} else {
Expand Down

0 comments on commit 0acad4c

Please sign in to comment.