Opt in to use schema

This commit is contained in:
Phani Teja Marupaka
2020-02-04 09:34:10 -08:00
parent 4f49d2883b
commit 79d591e2b0
3 changed files with 103 additions and 22 deletions

View File

@@ -31,6 +31,8 @@ formatting substitution verbs {'%n': 'metadata.name', '%s': 'metadata.namespace'
`if true, keep index and filename annotations set on Resources.`) `if true, keep index and filename annotations set on Resources.`)
c.Flags().BoolVar(&r.Override, "override", false, c.Flags().BoolVar(&r.Override, "override", false,
`if true, override existing filepath annotations.`) `if true, override existing filepath annotations.`)
c.Flags().BoolVar(&r.UseSchema, "use-schema", false,
`if true, uses openapi resource schema to format resources.`)
r.Command = c r.Command = c
return r return r
} }
@@ -46,6 +48,7 @@ type FmtRunner struct {
SetFilenames bool SetFilenames bool
KeepAnnotations bool KeepAnnotations bool
Override bool Override bool
UseSchema bool
} }
func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error { func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error {
@@ -56,7 +59,9 @@ func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error {
} }
func (r *FmtRunner) runE(c *cobra.Command, args []string) error { func (r *FmtRunner) runE(c *cobra.Command, args []string) error {
f := []kio.Filter{filters.FormatFilter{}} f := []kio.Filter{filters.FormatFilter{
UseSchema: r.UseSchema,
}}
// format with file names // format with file names
if r.SetFilenames { if r.SetFilenames {

View File

@@ -56,7 +56,8 @@ func FormatFileOrDirectory(path string) error {
} }
type FormatFilter struct { type FormatFilter struct {
Process func(n *yaml.Node) error Process func(n *yaml.Node) error
UseSchema bool
} }
var _ kio.Filter = FormatFilter{} var _ kio.Filter = FormatFilter{}
@@ -78,7 +79,12 @@ func (f FormatFilter) Filter(slice []*yaml.RNode) ([]*yaml.RNode, error) {
continue continue
} }
kind, apiVersion := kindNode.YNode().Value, apiVersionNode.YNode().Value kind, apiVersion := kindNode.YNode().Value, apiVersionNode.YNode().Value
s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: apiVersion, Kind: kind}) var s *openapi.ResourceSchema
if f.UseSchema {
s = openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: apiVersion, Kind: kind})
} else {
s = nil
}
err = (&formatter{apiVersion: apiVersion, kind: kind, process: f.Process}). err = (&formatter{apiVersion: apiVersion, kind: kind, process: f.Process}).
fmtNode(slice[i].YNode(), "", s) fmtNode(slice[i].YNode(), "", s)
if err != nil { if err != nil {
@@ -151,7 +157,6 @@ func (f *formatter) fmtNode(n *yaml.Node, path string, schema *openapi.ResourceS
s = schema.Elements() s = schema.Elements()
} }
} }
// format the node using the schema // format the node using the schema
err := f.fmtNode(n.Content[i], p, s) err := f.fmtNode(n.Content[i], p, s)
if err != nil { if err != nil {

View File

@@ -74,6 +74,73 @@ spec:
containerPort: 80 containerPort: 80
` `
buff := &bytes.Buffer{}
err := kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
Filters: []kio.Filter{FormatFilter{
UseSchema: true,
}},
Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}},
}.Execute()
assert.NoError(t, err)
assert.Equal(t, expected, buff.String())
}
func TestFormat_UnsortedInput_No_Schema(t *testing.T) {
y := `
apiVersion: apps/v1
spec:
template:
spec:
containers:
- name: nginx
image: nginx:1.0.0
args:
- on
- 1
- hello
ports:
- name: http
targetPort: 80
containerPort: 80
kind: Deployment
metadata:
name: foo
labels:
foo: on
foo2: hello1
annotations:
bar: 1
bar2: hello2
`
// keep the style on values that parse as non-string types
expected := `apiVersion: apps/v1
kind: Deployment
metadata:
name: foo
labels:
foo: on
foo2: hello1
annotations:
bar: 1
bar2: hello2
spec:
template:
spec:
containers:
- name: nginx
image: nginx:1.0.0
args:
- on
- 1
- hello
ports:
- name: http
targetPort: 80
containerPort: 80
`
buff := &bytes.Buffer{} buff := &bytes.Buffer{}
err := kio.Pipeline{ err := kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}}, Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
@@ -120,16 +187,18 @@ spec:
buff := &bytes.Buffer{} buff := &bytes.Buffer{}
err := kio.Pipeline{ err := kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}}, Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error { Filters: []kio.Filter{FormatFilter{
if yaml.IsYaml1_1NonString(n) { UseSchema: true,
// don't change these styles, they are important for backwards compatibility Process: func(n *yaml.Node) error {
// e.g. "on" must remain quoted, on must remain unquoted if yaml.IsYaml1_1NonString(n) {
// don't change these styles, they are important for backwards compatibility
// e.g. "on" must remain quoted, on must remain unquoted
return nil
}
// style does not have semantic meaning
n.Style = 0
return nil return nil
} }}},
// style does not have semantic meaning
n.Style = 0
return nil
}}},
Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}}, Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}},
}.Execute() }.Execute()
assert.NoError(t, err) assert.NoError(t, err)
@@ -186,16 +255,18 @@ spec:
buff = &bytes.Buffer{} buff = &bytes.Buffer{}
err = kio.Pipeline{ err = kio.Pipeline{
Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}}, Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}},
Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error { Filters: []kio.Filter{FormatFilter{
if yaml.IsYaml1_1NonString(n) { UseSchema: true,
// don't change these styles, they are important for backwards compatibility Process: func(n *yaml.Node) error {
// e.g. "on" must remain quoted, on must remain unquoted if yaml.IsYaml1_1NonString(n) {
// don't change these styles, they are important for backwards compatibility
// e.g. "on" must remain quoted, on must remain unquoted
return nil
}
// style does not have semantic meaning
n.Style = yaml.SingleQuotedStyle
return nil return nil
} }}},
// style does not have semantic meaning
n.Style = yaml.SingleQuotedStyle
return nil
}}},
Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}}, Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}},
}.Execute() }.Execute()
assert.NoError(t, err) assert.NoError(t, err)