forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commands to list/print json schemas (GoogleContainerTools#3355)
Signed-off-by: David Gageot <[email protected]>
- Loading branch information
Showing
13 changed files
with
395 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/schema" | ||
) | ||
|
||
func NewCmdSchema() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "schema", | ||
Short: "List and print json schemas used to validate skaffold.yaml configuration", | ||
} | ||
|
||
cmd.AddCommand(NewCmdSchemaGet()) | ||
cmd.AddCommand(NewCmdSchemaList()) | ||
return cmd | ||
} | ||
|
||
func NewCmdSchemaList() *cobra.Command { | ||
return NewCmd("list"). | ||
WithDescription("List skaffold.yaml's json schema versions"). | ||
WithExample("List all the versions", "schema list"). | ||
NoArgs(schema.List) | ||
} | ||
|
||
func NewCmdSchemaGet() *cobra.Command { | ||
return NewCmd("get"). | ||
WithDescription("Print a given skaffold.yaml's json schema"). | ||
WithExample("Print the schema in version `skaffold/v1`", "schema get skaffold/v1"). | ||
ExactArgs(1, func(out io.Writer, args []string) error { | ||
version := args[0] | ||
return schema.Print(out, version) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +build release | ||
|
||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"github.com/rakyll/statik/fs" | ||
|
||
//required for rakyll/statik embedded content | ||
_ "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/schema/statik" | ||
) | ||
|
||
var statikFS = fs.New |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +build !release | ||
|
||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import "net/http" | ||
|
||
// statikFS with !release build tag is just here for compilation purposes | ||
// this file does not depend on the generated statik.go file that is not checked | ||
// in by default to git | ||
var statikFS = func() (http.FileSystem, error) { | ||
panic("not implemented, skaffold should be built with make ('release' build tag)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema" | ||
) | ||
|
||
// List prints to `out` all supported schema versions. | ||
func List(out io.Writer) error { | ||
for _, version := range schema.SchemaVersions { | ||
fmt.Fprintln(out, version.APIVersion) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" | ||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
func TestList(t *testing.T) { | ||
testutil.Run(t, "", func(t *testutil.T) { | ||
var out bytes.Buffer | ||
|
||
err := List(&out) | ||
|
||
versions := out.String() | ||
t.CheckNoError(err) | ||
t.CheckTrue(strings.HasSuffix(versions, latest.Version+"\n")) | ||
t.CheckTrue(strings.HasPrefix(versions, `skaffold/v1alpha1 | ||
skaffold/v1alpha2 | ||
skaffold/v1alpha3 | ||
skaffold/v1alpha4 | ||
skaffold/v1alpha5 | ||
skaffold/v1beta1 | ||
skaffold/v1beta2 | ||
skaffold/v1beta3 | ||
skaffold/v1beta4 | ||
skaffold/v1beta5 | ||
skaffold/v1beta6 | ||
skaffold/v1beta7 | ||
skaffold/v1beta8 | ||
skaffold/v1beta9 | ||
skaffold/v1beta10 | ||
skaffold/v1beta11 | ||
skaffold/v1beta12 | ||
skaffold/v1beta13 | ||
skaffold/v1beta14 | ||
skaffold/v1beta15 | ||
skaffold/v1beta16 | ||
skaffold/v1beta17 | ||
skaffold/v1 | ||
skaffold/v2alpha1`)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/rakyll/statik/fs" | ||
) | ||
|
||
// Print prints the json schema for a given version. | ||
func Print(out io.Writer, version string) error { | ||
statikFS, err := statikFS() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
path := "/" + strings.TrimPrefix(version, "skaffold/") + ".json" | ||
content, err := fs.ReadFile(statikFS, path) | ||
if err != nil { | ||
return errors.Wrapf(err, "schema %q not found", version) | ||
} | ||
|
||
out.Write(content) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
type fakeFileSystem struct { | ||
Files map[string][]byte | ||
} | ||
|
||
type fakeFile struct { | ||
http.File | ||
content io.Reader | ||
} | ||
|
||
func (f *fakeFileSystem) Open(name string) (http.File, error) { | ||
content, found := f.Files[name] | ||
if !found { | ||
return nil, os.ErrNotExist | ||
} | ||
|
||
return &fakeFile{ | ||
content: bytes.NewBuffer(content), | ||
}, nil | ||
} | ||
|
||
func (f *fakeFile) Read(p []byte) (n int, err error) { | ||
return f.content.Read(p) | ||
} | ||
|
||
func (f *fakeFile) Close() error { | ||
return nil | ||
} | ||
|
||
func TestPrint(t *testing.T) { | ||
fs := &fakeFileSystem{ | ||
Files: map[string][]byte{ | ||
"/v1.json": []byte("{SCHEMA}"), | ||
}, | ||
} | ||
|
||
testutil.Run(t, "found", func(t *testutil.T) { | ||
t.Override(&statikFS, func() (http.FileSystem, error) { return fs, nil }) | ||
|
||
var out bytes.Buffer | ||
err := Print(&out, "skaffold/v1") | ||
|
||
t.CheckNoError(err) | ||
t.CheckDeepEqual("{SCHEMA}", out.String()) | ||
}) | ||
|
||
testutil.Run(t, "not found", func(t *testutil.T) { | ||
t.Override(&statikFS, func() (http.FileSystem, error) { return fs, nil }) | ||
|
||
var out bytes.Buffer | ||
err := Print(&out, "skaffold/v0") | ||
|
||
t.CheckErrorContains("schema \"skaffold/v0\" not found", err) | ||
t.CheckEmpty(out.String()) | ||
}) | ||
} |
Oops, something went wrong.