Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesfrey committed Apr 12, 2024
1 parent 15f5bca commit 1aedd22
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
6 changes: 4 additions & 2 deletions cmd/vclusterctl/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,11 @@ func (cmd *CreateCmd) Run(ctx context.Context, args []string) error {
defer f.Close()

cfg := &config.Config{}
err = cfg.Validate(f)
err = cfg.DecodeYAML(f)
if err != nil {
cmd.log.Infof("Consider using %q to convert the old values format to the new 0.20 format", "vcluster migrate values")
if errors.Is(err, config.ErrInvalidFileFormat) {
cmd.log.Infof("If you are using the old values format, consider using %q to convert it to the new 0.20 format", "vcluster migrate values")
}
return err
}
}
Expand Down
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
Expand All @@ -17,6 +18,8 @@ import (
//go:embed values.yaml
var Values string

var ErrInvalidFileFormat = errors.New("invalid file format")

// NewDefaultConfig creates a new config based on the values.yaml, including all default values.
func NewDefaultConfig() (*Config, error) {
retConfig := &Config{}
Expand Down Expand Up @@ -76,7 +79,7 @@ type Config struct {
Plugin map[string]Plugin `json:"plugin,omitempty"`
}

func (c *Config) Validate(r io.Reader) error {
func (c *Config) DecodeYAML(r io.Reader) error {
o, err := io.ReadAll(r)
if err != nil {
return err
Expand All @@ -92,7 +95,7 @@ func (c *Config) Validate(r io.Reader) error {

err = dec.Decode(c)
if err != nil {
return fmt.Errorf("invalid values file format: %w", err)
return fmt.Errorf("%w: %w", ErrInvalidFileFormat, err)
}

return nil
Expand Down
40 changes: 35 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
)

func TestConfig_Validate(t *testing.T) {
func TestConfig_DecodeYAML(t *testing.T) {
type args struct {
r io.Reader
}
Expand All @@ -17,7 +17,7 @@ func TestConfig_Validate(t *testing.T) {
wantErr bool
}{
{
name: "Invalid",
name: "Invalid: yaml",
args: args{
r: bytes.NewReader([]byte(`
foo:
Expand All @@ -27,7 +27,20 @@ foo:
wantErr: true,
},
{
name: "Invalid (old values format)",
name: "Invalid: yaml",
args: args{
r: bytes.NewReader([]byte(`
{
"foo": {
"bar": "baz"
}
}
`)),
},
wantErr: true,
},
{
name: "Invalid: Old values format",
args: args{
r: bytes.NewReader([]byte(`
api:
Expand All @@ -51,13 +64,30 @@ telemetry:
wantErr: true,
},
{
name: "Success (new values format)",
name: "Success: New values format",
args: args{
r: bytes.NewReader([]byte(`
controlPlane:
distro:
k8s:
enabled: true
`)),
},
wantErr: false,
},
{
name: "Success: New values format (json)",
args: args{
r: bytes.NewReader([]byte(`
{
"controlPlane": {
"distro": {
"k8s": {
"enabled": true
}
}
}
}
`)),
},
wantErr: false,
Expand All @@ -66,7 +96,7 @@ controlPlane:
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Config{}
if err := c.Validate(tt.args.r); (err != nil) != tt.wantErr {
if err := c.DecodeYAML(tt.args.r); (err != nil) != tt.wantErr {
t.Errorf("Config.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down

0 comments on commit 1aedd22

Please sign in to comment.