Skip to content

Commit

Permalink
Hide afero as an implementation detail
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Mar 12, 2021
1 parent 1761213 commit 5bbf9ad
Show file tree
Hide file tree
Showing 25 changed files with 128 additions and 90 deletions.
5 changes: 3 additions & 2 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
internalconfig "sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
)

Expand Down Expand Up @@ -91,7 +92,7 @@ type CLI struct { //nolint:maligned
cmd *cobra.Command

// Underlying fs
fs afero.Fs
fs machinery.Filesystem
}

// New creates a new CLI instance.
Expand Down Expand Up @@ -135,7 +136,7 @@ func newCLI(options ...Option) (*CLI, error) {
defaultProjectVersion: cfgv3.Version,
defaultPlugins: make(map[config.Version][]string),
plugins: make(map[string]plugin.Plugin),
fs: afero.NewOsFs(),
fs: machinery.Filesystem{FS: afero.NewOsFs()},
}

// Apply provided options.
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/config"
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
)

Expand Down Expand Up @@ -586,7 +587,7 @@ var _ = Describe("CLI", func() {
defaultPlugins: map[config.Version][]string{
projectVersion: pluginKeys,
},
fs: afero.NewMemMapFs(),
fs: machinery.Filesystem{FS: afero.NewMemMapFs()},
}
c.cmd = c.newRootCmd()
Expect(c.getInfo()).To(Succeed())
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cmd_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package cli
import (
"fmt"

"github.com/spf13/afero"
"github.com/spf13/cobra"

"sigs.k8s.io/kubebuilder/v3/pkg/cli/internal/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
)

Expand All @@ -49,7 +49,7 @@ func errCmdFunc(err error) func(*cobra.Command, []string) error {
// runECmdFunc returns a cobra RunE function that runs subcommand and saves the
// config, which may have been modified by subcommand.
func runECmdFunc(
fs afero.Fs,
fs machinery.Filesystem,
c *config.Config,
subcommand plugin.Subcommand,
msg string,
Expand Down
23 changes: 12 additions & 11 deletions pkg/cli/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"os"

"github.com/spf13/afero"
"sigs.k8s.io/yaml"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/yaml"
)

const (
Expand Down Expand Up @@ -81,13 +82,13 @@ func readFrom(fs afero.Fs, path string) (config.Config, error) {
}

// Read obtains the configuration from the default path but doesn't allow to persist changes
func Read(fs afero.Fs) (config.Config, error) {
func Read(fs machinery.Filesystem) (config.Config, error) {
return ReadFrom(fs, DefaultPath)
}

// ReadFrom obtains the configuration from the provided path but doesn't allow to persist changes
func ReadFrom(fs afero.Fs, path string) (config.Config, error) {
return readFrom(fs, path)
func ReadFrom(fs machinery.Filesystem, path string) (config.Config, error) {
return readFrom(fs.FS, path)
}

// Config extends model/config.Config allowing to persist changes
Expand All @@ -105,7 +106,7 @@ type Config struct {
}

// New creates a new configuration that will be stored at the provided path
func New(fs afero.Fs, version config.Version, path string) (*Config, error) {
func New(fs machinery.Filesystem, version config.Version, path string) (*Config, error) {
cfg, err := config.New(version)
if err != nil {
return nil, err
Expand All @@ -115,18 +116,18 @@ func New(fs afero.Fs, version config.Version, path string) (*Config, error) {
Config: cfg,
path: path,
mustNotExist: true,
fs: fs,
fs: fs.FS,
}, nil
}

// Load obtains the configuration from the default path allowing to persist changes (Save method)
func Load(fs afero.Fs) (*Config, error) {
func Load(fs machinery.Filesystem) (*Config, error) {
return LoadFrom(fs, DefaultPath)
}

// LoadInitialized calls Load() but returns helpful error messages if the config
// does not exist.
func LoadInitialized(fs afero.Fs) (*Config, error) {
func LoadInitialized(fs machinery.Filesystem) (*Config, error) {
c, err := Load(fs)
if os.IsNotExist(err) {
return nil, errors.New("unable to find configuration file, project must be initialized")
Expand All @@ -135,9 +136,9 @@ func LoadInitialized(fs afero.Fs) (*Config, error) {
}

// LoadFrom obtains the configuration from the provided path allowing to persist changes (Save method)
func LoadFrom(fs afero.Fs, path string) (*Config, error) {
c, err := readFrom(fs, path)
return &Config{Config: c, path: path, fs: fs}, err
func LoadFrom(fs machinery.Filesystem, path string) (*Config, error) {
c, err := readFrom(fs.FS, path)
return &Config{Config: c, path: path, fs: fs.FS}, err
}

// Save saves the configuration information
Expand Down
25 changes: 25 additions & 0 deletions pkg/machinery/filesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2021 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 machinery

import (
"github.com/spf13/afero"
)

type Filesystem struct {
FS afero.Fs
}
4 changes: 2 additions & 2 deletions pkg/plugin/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package plugin

import (
"github.com/spf13/afero"
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
)

// Plugin is an interface that defines the common base for all plugins
Expand Down Expand Up @@ -54,7 +54,7 @@ type Subcommand interface {
// command line flags.
BindFlags(*pflag.FlagSet)
// Run runs the subcommand.
Run(fs afero.Fs) error
Run(fs machinery.Filesystem) error
// InjectConfig passes a config to a plugin. The plugin may modify the config.
// Initializing, loading, and saving the config is managed by the cli package.
InjectConfig(config.Config)
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/golang/v2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
"os"
"strings"

"github.com/spf13/afero"
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
Expand Down Expand Up @@ -125,7 +125,7 @@ func (p *createAPISubcommand) InjectConfig(c config.Config) {
p.config = c
}

func (p *createAPISubcommand) Run(fs afero.Fs) error {
func (p *createAPISubcommand) Run(fs machinery.Filesystem) error {
// Ask for API and Controller if not specified
reader := bufio.NewReader(os.Stdin)
if !p.resourceFlag.Changed {
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/golang/v2/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package v2
import (
"fmt"

"github.com/spf13/afero"
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/internal/cmdutil"
Expand Down Expand Up @@ -58,7 +58,7 @@ func (p *editSubcommand) InjectConfig(c config.Config) {
p.config = c
}

func (p *editSubcommand) Run(fs afero.Fs) error {
func (p *editSubcommand) Run(fs machinery.Filesystem) error {
return cmdutil.Run(p, fs)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/golang/v2/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
"path/filepath"
"strings"

"github.com/spf13/afero"
"github.com/spf13/pflag"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
"sigs.k8s.io/kubebuilder/v3/pkg/internal/validation"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang"
Expand Down Expand Up @@ -111,7 +111,7 @@ func (p *initSubcommand) InjectConfig(c config.Config) {
p.config = c
}

func (p *initSubcommand) Run(fs afero.Fs) error {
func (p *initSubcommand) Run(fs machinery.Filesystem) error {
return cmdutil.Run(p, fs)
}

Expand Down
17 changes: 9 additions & 8 deletions pkg/plugins/golang/v2/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"sigs.k8s.io/kubebuilder/v3/pkg/config"
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/model"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates"
Expand All @@ -34,7 +35,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/controllers"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2/scaffolds/internal/templates/hack"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/internal/cmdutil"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/internal/machinery"
internalmachinery "sigs.k8s.io/kubebuilder/v3/pkg/plugins/internal/machinery"
)

// KbDeclarativePattern is the sigs.k8s.io/kubebuilder-declarative-pattern version
Expand All @@ -51,7 +52,7 @@ type apiScaffolder struct {
resource resource.Resource

// fs is the filesystem that will be used by the scaffolder
fs afero.Fs
fs machinery.Filesystem

// plugins is the list of plugins we should allow to transform our generated scaffolding
plugins []model.Plugin
Expand All @@ -76,7 +77,7 @@ func NewAPIScaffolder(
}

// InjectFS implements cmdutil.Scaffolder
func (s *apiScaffolder) InjectFS(fs afero.Fs) {
func (s *apiScaffolder) InjectFS(fs machinery.Filesystem) {
s.fs = fs
}

Expand All @@ -93,7 +94,7 @@ func (s *apiScaffolder) Scaffold() error {
fmt.Println("Writing scaffold for you to edit...")

// Load the boilerplate
bp, err := afero.ReadFile(s.fs, hack.DefaultBoilerplatePath)
bp, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath)
if err != nil {
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)
}
Expand All @@ -117,7 +118,7 @@ func (s *apiScaffolder) Scaffold() error {

if doAPI {

if err := machinery.NewScaffold(s.fs, s.plugins...).Execute(
if err := internalmachinery.NewScaffold(s.fs, s.plugins...).Execute(
s.newUniverse(),
&api.Types{Force: s.force},
&api.Group{},
Expand All @@ -130,7 +131,7 @@ func (s *apiScaffolder) Scaffold() error {
return fmt.Errorf("error scaffolding APIs: %w", err)
}

if err := machinery.NewScaffold(s.fs).Execute(
if err := internalmachinery.NewScaffold(s.fs).Execute(
s.newUniverse(),
&crd.Kustomization{},
&crd.KustomizeConfig{},
Expand All @@ -141,7 +142,7 @@ func (s *apiScaffolder) Scaffold() error {
}

if doController {
if err := machinery.NewScaffold(s.fs, s.plugins...).Execute(
if err := internalmachinery.NewScaffold(s.fs, s.plugins...).Execute(
s.newUniverse(),
&controllers.SuiteTest{Force: s.force},
&controllers.Controller{ControllerRuntimeVersion: ControllerRuntimeVersion, Force: s.force},
Expand All @@ -150,7 +151,7 @@ func (s *apiScaffolder) Scaffold() error {
}
}

if err := machinery.NewScaffold(s.fs, s.plugins...).Execute(
if err := internalmachinery.NewScaffold(s.fs, s.plugins...).Execute(
s.newUniverse(),
&templates.MainUpdater{WireResource: doAPI, WireController: doController},
); err != nil {
Expand Down
9 changes: 5 additions & 4 deletions pkg/plugins/golang/v2/scaffolds/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/spf13/afero"

"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/internal/cmdutil"
)

Expand All @@ -33,7 +34,7 @@ type editScaffolder struct {
multigroup bool

// fs is the filesystem that will be used by the scaffolder
fs afero.Fs
fs machinery.Filesystem
}

// NewEditScaffolder returns a new Scaffolder for configuration edit operations
Expand All @@ -45,14 +46,14 @@ func NewEditScaffolder(config config.Config, multigroup bool) cmdutil.Scaffolder
}

// InjectFS implements cmdutil.Scaffolder
func (s *editScaffolder) InjectFS(fs afero.Fs) {
func (s *editScaffolder) InjectFS(fs machinery.Filesystem) {
s.fs = fs
}

// Scaffold implements cmdutil.Scaffolder
func (s *editScaffolder) Scaffold() error {
filename := "Dockerfile"
bs, err := afero.ReadFile(s.fs, filename)
bs, err := afero.ReadFile(s.fs.FS, filename)
if err != nil {
return err
}
Expand Down Expand Up @@ -86,7 +87,7 @@ func (s *editScaffolder) Scaffold() error {
// because there is nothing to replace.
if str != "" {
// TODO: instead of writing it directly, we should use the scaffolding machinery for consistency
return afero.WriteFile(s.fs, filename, []byte(str), 0644)
return afero.WriteFile(s.fs.FS, filename, []byte(str), 0644)
}

return nil
Expand Down
Loading

0 comments on commit 5bbf9ad

Please sign in to comment.