mirror of https://github.com/databricks/cli.git
added a map for validators, and moved init under bundle
This commit is contained in:
parent
9a8205b3df
commit
f5384fc5ed
|
@ -1,11 +1,10 @@
|
||||||
package init
|
package bundle
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/databricks/cli/cmd/root"
|
|
||||||
"github.com/databricks/cli/libs/template"
|
"github.com/databricks/cli/libs/template"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -66,5 +65,5 @@ var targetDir string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
initCmd.Flags().StringVar(&targetDir, "target-dir", ".", "path to directory template will be initialized in")
|
initCmd.Flags().StringVar(&targetDir, "target-dir", ".", "path to directory template will be initialized in")
|
||||||
root.RootCmd.AddCommand(initCmd)
|
AddCommand(initCmd)
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
_ "github.com/databricks/cli/cmd/init"
|
_ "github.com/databricks/cli/cmd/bundle"
|
||||||
"github.com/databricks/cli/cmd/root"
|
"github.com/databricks/cli/cmd/root"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -36,7 +36,7 @@ func TestTemplateInitializationForDevConfig(t *testing.T) {
|
||||||
|
|
||||||
// materialize the template
|
// materialize the template
|
||||||
cmd := root.RootCmd
|
cmd := root.RootCmd
|
||||||
cmd.SetArgs([]string{"init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp})
|
cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp})
|
||||||
err = cmd.Execute()
|
err = cmd.Execute()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ func TestTemplateInitializationForProdConfig(t *testing.T) {
|
||||||
cmd := root.RootCmd
|
cmd := root.RootCmd
|
||||||
childCommands := cmd.Commands()
|
childCommands := cmd.Commands()
|
||||||
fmt.Println(childCommands)
|
fmt.Println(childCommands)
|
||||||
cmd.SetArgs([]string{"init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp})
|
cmd.SetArgs([]string{"bundle", "init", filepath.FromSlash("testdata/init/templateDefinition"), "--target-dir", tmp})
|
||||||
err = cmd.Execute()
|
err = cmd.Execute()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,6 @@ package template
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"golang.org/x/exp/slices"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Schema map[string]FieldInfo
|
type Schema map[string]FieldInfo
|
||||||
|
@ -67,27 +65,11 @@ func (schema Schema) CastFloatToInt(config map[string]any) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateType(v any, fieldType FieldType) error {
|
func validateType(v any, fieldType FieldType) error {
|
||||||
switch fieldType {
|
validateFunc, ok := validators[fieldType]
|
||||||
case FieldTypeString:
|
if !ok {
|
||||||
if _, ok := v.(string); !ok {
|
return nil
|
||||||
return fmt.Errorf("expected type string, but value is %#v", v)
|
|
||||||
}
|
|
||||||
case FieldTypeInt:
|
|
||||||
if !slices.Contains([]reflect.Kind{reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64},
|
|
||||||
reflect.TypeOf(v).Kind()) {
|
|
||||||
return fmt.Errorf("expected type integer, but value is %#v", v)
|
|
||||||
}
|
|
||||||
case FieldTypeFloat:
|
|
||||||
if !slices.Contains([]reflect.Kind{reflect.Float32, reflect.Float64},
|
|
||||||
reflect.TypeOf(v).Kind()) {
|
|
||||||
return fmt.Errorf("expected type float, but value is %#v", v)
|
|
||||||
}
|
|
||||||
case FieldTypeBoolean:
|
|
||||||
if _, ok := v.(bool); !ok {
|
|
||||||
return fmt.Errorf("expected type boolean, but value is %#v", v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return validateFunc(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: add validation check for regex for string types
|
// TODO: add validation check for regex for string types
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
package template
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Validator func(v any) error
|
||||||
|
|
||||||
|
// TODO: refactor tests into individual tests for individual validators
|
||||||
|
func validateString(v any) error {
|
||||||
|
if _, ok := v.(string); !ok {
|
||||||
|
return fmt.Errorf("expected type string, but value is %#v", v)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateBoolean(v any) error {
|
||||||
|
if _, ok := v.(bool); !ok {
|
||||||
|
return fmt.Errorf("expected type boolean, but value is %#v", v)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateFloat(v any) error {
|
||||||
|
if !slices.Contains([]reflect.Kind{reflect.Float32, reflect.Float64},
|
||||||
|
reflect.TypeOf(v).Kind()) {
|
||||||
|
return fmt.Errorf("expected type float, but value is %#v", v)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateInteger(v any) error {
|
||||||
|
if !slices.Contains([]reflect.Kind{reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64},
|
||||||
|
reflect.TypeOf(v).Kind()) {
|
||||||
|
return fmt.Errorf("expected type integer, but value is %#v", v)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var validators map[FieldType]Validator = map[FieldType]Validator{
|
||||||
|
FieldTypeString: validateString,
|
||||||
|
FieldTypeBoolean: validateBoolean,
|
||||||
|
FieldTypeInt: validateInteger,
|
||||||
|
FieldTypeFloat: validateFloat,
|
||||||
|
}
|
Loading…
Reference in New Issue