-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move scaffold machinery to its own internal package
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
- Loading branch information
Showing
14 changed files
with
1,421 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
Copyright 2018 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 model | ||
|
||
// Plugin is the interface that a plugin must implement | ||
// We will (later) have an ExecPlugin that implements this by exec-ing a binary | ||
type Plugin interface { | ||
// Pipe is the core plugin interface, that transforms a UniverseModel | ||
Pipe(*Universe) error | ||
} |
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,93 @@ | ||
/* | ||
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 ( | ||
"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 | ||
|
||
// createDirectoryError is returned if the directory could not be created | ||
type createDirectoryError struct { | ||
path string | ||
err error | ||
} | ||
|
||
func (e createDirectoryError) Error() string { | ||
return fmt.Sprintf("failed to create directory for %s: %v", e.path, e.err) | ||
} | ||
|
||
// IsCreateDirectoryError checks if the returned error is because the directory | ||
// could not be created | ||
func IsCreateDirectoryError(e error) bool { | ||
_, ok := e.(createDirectoryError) | ||
return ok | ||
} | ||
|
||
// createFileError is returned if the file could not be created | ||
type createFileError struct { | ||
path string | ||
err error | ||
} | ||
|
||
func (e createFileError) Error() string { | ||
return fmt.Sprintf("failed to create %s: %v", e.path, e.err) | ||
} | ||
|
||
// IsCreateFileError checks if the returned error is because the file could not | ||
// be created | ||
func IsCreateFileError(e error) bool { | ||
_, ok := e.(createFileError) | ||
return ok | ||
} | ||
|
||
// writeFileError is returned if the filed could not be written to | ||
type writeFileError struct { | ||
path string | ||
err error | ||
} | ||
|
||
func (e writeFileError) Error() string { | ||
return fmt.Sprintf("failed to write to %s: %v", e.path, e.err) | ||
} | ||
|
||
// IsWriteFileError checks if the returned error is because the file could not | ||
// be written to | ||
func IsWriteFileError(e error) bool { | ||
_, ok := e.(writeFileError) | ||
return ok | ||
} | ||
|
||
// closeFileError is returned if the file could not be created | ||
type closeFileError struct { | ||
path string | ||
err error | ||
} | ||
|
||
func (e closeFileError) Error() string { | ||
return fmt.Sprintf("failed to close %s: %v", e.path, e.err) | ||
} | ||
|
||
// IsCloseFileError checks if the returned error is because the file could not | ||
// be closed | ||
func IsCloseFileError(e error) bool { | ||
_, ok := e.(closeFileError) | ||
return ok | ||
} |
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,131 @@ | ||
/* | ||
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 ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/spf13/afero" | ||
) | ||
|
||
const ( | ||
createOrUpdate = os.O_WRONLY | os.O_CREATE | os.O_TRUNC | ||
|
||
defaultDirectoryPermission os.FileMode = 0700 | ||
defaultFilePermission os.FileMode = 0600 | ||
) | ||
|
||
// FileSystem is an IO wrapper to create files | ||
type FileSystem interface { | ||
// Exists checks if the file exists | ||
Exists(path string) (bool, error) | ||
|
||
// Create creates the directory and file and returns a self-closing | ||
// io.Writer pointing to that file. If the file exists, it truncates it. | ||
Create(path string) (io.Writer, error) | ||
} | ||
|
||
// fileSystem implements FileSystem | ||
type fileSystem struct { | ||
fs afero.Fs | ||
dirPerm os.FileMode | ||
filePerm os.FileMode | ||
fileMode int | ||
} | ||
|
||
// New returns a new FileSystem | ||
func New(options ...Options) FileSystem { | ||
// Default values | ||
fs := fileSystem{ | ||
fs: afero.NewOsFs(), | ||
dirPerm: defaultDirectoryPermission, | ||
filePerm: defaultFilePermission, | ||
fileMode: createOrUpdate, | ||
} | ||
|
||
// Apply options | ||
for _, option := range options { | ||
option(&fs) | ||
} | ||
|
||
return fs | ||
} | ||
|
||
// Options configure FileSystem | ||
type Options func(system *fileSystem) | ||
|
||
// DirectoryPermissions makes FileSystem.Create use the provided directory | ||
// permissions | ||
func DirectoryPermissions(dirPerm os.FileMode) Options { | ||
return func(fs *fileSystem) { | ||
fs.dirPerm = dirPerm | ||
} | ||
} | ||
|
||
// FilePermissions makes FileSystem.Create use the provided file permissions | ||
func FilePermissions(filePerm os.FileMode) Options { | ||
return func(fs *fileSystem) { | ||
fs.filePerm = filePerm | ||
} | ||
} | ||
|
||
// Exists implements FileSystem.Exists | ||
func (fs fileSystem) Exists(path string) (bool, error) { | ||
return afero.Exists(fs.fs, path) | ||
} | ||
|
||
// Create implements FileSystem.Create | ||
func (fs fileSystem) Create(path string) (io.Writer, error) { | ||
// Create the directory if needed | ||
if err := fs.fs.MkdirAll(filepath.Dir(path), fs.dirPerm); err != nil { | ||
return nil, createDirectoryError{path, err} | ||
} | ||
|
||
// Create or truncate the file | ||
wc, err := fs.fs.OpenFile(path, fs.fileMode, fs.filePerm) | ||
if err != nil { | ||
return nil, createFileError{path, err} | ||
} | ||
|
||
return &file{path, wc}, nil | ||
} | ||
|
||
// file implements io.Writer | ||
type file struct { | ||
path string | ||
io.WriteCloser | ||
} | ||
|
||
// Write implements io.Writer.Write | ||
func (f *file) Write(content []byte) (n int, err error) { | ||
// Close the file when we end writing | ||
defer func() { | ||
if closeErr := f.Close(); err == nil && closeErr != nil { | ||
err = closeFileError{f.path, err} | ||
} | ||
}() | ||
|
||
// Write the content | ||
n, err = f.WriteCloser.Write(content) | ||
if err != nil { | ||
return n, writeFileError{f.path, err} | ||
} | ||
|
||
return n, nil | ||
} |
Oops, something went wrong.