Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hack/check-license.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ echo "Checking for license header..."
allfiles=$(listFiles)
licRes=""
for file in $allfiles; do
if ! head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" ; then
if ! head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED|Licensed)" ; then
licRes="${licRes}\n"$(echo -e " ${file}")
fi
done
Expand Down
56 changes: 56 additions & 0 deletions internal/kubebuilder/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2020 The Kubernetes 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 cmdutil

import "sigs.k8s.io/kubebuilder/pkg/plugin/scaffold"

// RunOptions represent the types used to implement the different commands
type RunOptions interface {
// - Step 1: verify that the command can be run (e.g., go version, project version, arguments, ...)
Validate() error
// - Step 2: create the Scaffolder instance
GetScaffolder() (scaffold.Scaffolder, error)
// - Step 3: call the Scaffold method of the Scaffolder instance. Doesn't need any method
// - Step 4: finish the command execution
PostScaffold() error
}

// Run executes a command
func Run(options RunOptions) error {
// Step 1: validate
if err := options.Validate(); err != nil {
return err
}

// Step 2: get scaffolder
scaffolder, err := options.GetScaffolder()
if err != nil {
return err
}
// Step 3: scaffold
if scaffolder != nil {
if err := scaffolder.Scaffold(); err != nil {
return err
}
}
// Step 4: finish
if err := options.PostScaffold(); err != nil {
return err
}

return nil
}
173 changes: 173 additions & 0 deletions internal/kubebuilder/filesystem/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
Copyright 2020 The Kubernetes 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 filesystem

import (
"errors"
"fmt"
)

// This file contains the errors returned by the file system wrapper
// They are not exported as they should not be created outside of this package
// Exported functions are provided to check which kind of error was returned

// fileExistsError is returned if it could not be checked if the file exists
type fileExistsError struct {
path string
err error
}

// Error implements error interface
func (e fileExistsError) Error() string {
return fmt.Sprintf("failed to check if %s exists: %v", e.path, e.err)
}

// Unwrap implements Wrapper interface
func (e fileExistsError) Unwrap() error {
return e.err
}

// IsFileExistsError checks if the returned error is because the file could not be checked for existence
func IsFileExistsError(err error) bool {
return errors.As(err, &fileExistsError{})
}

// openFileError is returned if the file could not be opened
type openFileError struct {
path string
err error
}

// Error implements error interface
func (e openFileError) Error() string {
return fmt.Sprintf("failed to open %s: %v", e.path, e.err)
}

// Unwrap implements Wrapper interface
func (e openFileError) Unwrap() error {
return e.err
}

// IsOpenFileError checks if the returned error is because the file could not be opened
func IsOpenFileError(err error) bool {
return errors.As(err, &openFileError{})
}

// createDirectoryError is returned if the directory could not be created
type createDirectoryError struct {
path string
err error
}

// Error implements error interface
func (e createDirectoryError) Error() string {
return fmt.Sprintf("failed to create directory for %s: %v", e.path, e.err)
}

// Unwrap implements Wrapper interface
func (e createDirectoryError) Unwrap() error {
return e.err
}

// IsCreateDirectoryError checks if the returned error is because the directory could not be created
func IsCreateDirectoryError(err error) bool {
return errors.As(err, &createDirectoryError{})
}

// createFileError is returned if the file could not be created
type createFileError struct {
path string
err error
}

// Error implements error interface
func (e createFileError) Error() string {
return fmt.Sprintf("failed to create %s: %v", e.path, e.err)
}

// Unwrap implements Wrapper interface
func (e createFileError) Unwrap() error {
return e.err
}

// IsCreateFileError checks if the returned error is because the file could not be created
func IsCreateFileError(err error) bool {
return errors.As(err, &createFileError{})
}

// readFileError is returned if the file could not be read
type readFileError struct {
path string
err error
}

// Error implements error interface
func (e readFileError) Error() string {
return fmt.Sprintf("failed to read from %s: %v", e.path, e.err)
}

// Unwrap implements Wrapper interface
func (e readFileError) Unwrap() error {
return e.err
}

// IsReadFileError checks if the returned error is because the file could not be read
func IsReadFileError(err error) bool {
return errors.As(err, &readFileError{})
}

// writeFileError is returned if the file could not be written
type writeFileError struct {
path string
err error
}

// Error implements error interface
func (e writeFileError) Error() string {
return fmt.Sprintf("failed to write to %s: %v", e.path, e.err)
}

// Unwrap implements Wrapper interface
func (e writeFileError) Unwrap() error {
return e.err
}

// IsWriteFileError checks if the returned error is because the file could not be written to
func IsWriteFileError(err error) bool {
return errors.As(err, &writeFileError{})
}

// closeFileError is returned if the file could not be created
type closeFileError struct {
path string
err error
}

// Error implements error interface
func (e closeFileError) Error() string {
return fmt.Sprintf("failed to close %s: %v", e.path, e.err)
}

// Unwrap implements Wrapper interface
func (e closeFileError) Unwrap() error {
return e.err
}

// IsCloseFileError checks if the returned error is because the file could not be closed
func IsCloseFileError(err error) bool {
return errors.As(err, &closeFileError{})
}
83 changes: 83 additions & 0 deletions internal/kubebuilder/filesystem/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2020 The Kubernetes 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 filesystem

import (
"errors"
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)

func TestErrors(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Error suite")
}

var _ = Describe("Errors", func() {
var (
path = filepath.Join("path", "to", "file")
err = errors.New("test error")
fileExistsErr = fileExistsError{path, err}
openFileErr = openFileError{path, err}
createDirectoryErr = createDirectoryError{path, err}
createFileErr = createFileError{path, err}
readFileErr = readFileError{path, err}
writeFileErr = writeFileError{path, err}
closeFileErr = closeFileError{path, err}
)

DescribeTable("IsXxxxError should return true for themselves and false for the rest",
func(f func(error) bool, itself error, rest ...error) {
Expect(f(itself)).To(BeTrue())
for _, err := range rest {
Expect(f(err)).To(BeFalse())
}
},
Entry("file exists", IsFileExistsError, fileExistsErr,
openFileErr, createDirectoryErr, createFileErr, readFileErr, writeFileErr, closeFileErr),
Entry("open file", IsOpenFileError, openFileErr,
fileExistsErr, createDirectoryErr, createFileErr, readFileErr, writeFileErr, closeFileErr),
Entry("create directory", IsCreateDirectoryError, createDirectoryErr,
fileExistsErr, openFileErr, createFileErr, readFileErr, writeFileErr, closeFileErr),
Entry("create file", IsCreateFileError, createFileErr,
fileExistsErr, openFileErr, createDirectoryErr, readFileErr, writeFileErr, closeFileErr),
Entry("read file", IsReadFileError, readFileErr,
fileExistsErr, openFileErr, createDirectoryErr, createFileErr, writeFileErr, closeFileErr),
Entry("write file", IsWriteFileError, writeFileErr,
fileExistsErr, openFileErr, createDirectoryErr, createFileErr, readFileErr, closeFileErr),
Entry("close file", IsCloseFileError, closeFileErr,
fileExistsErr, openFileErr, createDirectoryErr, createFileErr, readFileErr, writeFileErr),
)

DescribeTable("should contain the wrapped error and error message",
func(err error) {
Expect(err).To(MatchError(err))
Expect(err.Error()).To(ContainSubstring(err.Error()))
},
Entry("file exists", fileExistsErr),
Entry("open file", openFileErr),
Entry("create directory", createDirectoryErr),
Entry("create file", createFileErr),
Entry("read file", readFileErr),
Entry("write file", writeFileErr),
Entry("close file", closeFileErr),
)
})
Loading