@@ -22,9 +22,10 @@ import (
22
22
"os"
23
23
24
24
"github.com/spf13/afero"
25
- "sigs.k8s.io/yaml"
26
25
27
26
"sigs.k8s.io/kubebuilder/v3/pkg/config"
27
+ "sigs.k8s.io/kubebuilder/v3/pkg/machinery"
28
+ "sigs.k8s.io/yaml"
28
29
)
29
30
30
31
const (
@@ -81,13 +82,13 @@ func readFrom(fs afero.Fs, path string) (config.Config, error) {
81
82
}
82
83
83
84
// Read obtains the configuration from the default path but doesn't allow to persist changes
84
- func Read () (config.Config , error ) {
85
- return ReadFrom (DefaultPath )
85
+ func Read (fs machinery. Filesystem ) (config.Config , error ) {
86
+ return ReadFrom (fs , DefaultPath )
86
87
}
87
88
88
89
// ReadFrom obtains the configuration from the provided path but doesn't allow to persist changes
89
- func ReadFrom (path string ) (config.Config , error ) {
90
- return readFrom (afero . NewOsFs () , path )
90
+ func ReadFrom (fs machinery. Filesystem , path string ) (config.Config , error ) {
91
+ return readFrom (fs . FS , path )
91
92
}
92
93
93
94
// Config extends model/config.Config allowing to persist changes
@@ -105,7 +106,7 @@ type Config struct {
105
106
}
106
107
107
108
// New creates a new configuration that will be stored at the provided path
108
- func New (version config.Version , path string ) (* Config , error ) {
109
+ func New (fs machinery. Filesystem , version config.Version , path string ) (* Config , error ) {
109
110
cfg , err := config .New (version )
110
111
if err != nil {
111
112
return nil , err
@@ -115,37 +116,33 @@ func New(version config.Version, path string) (*Config, error) {
115
116
Config : cfg ,
116
117
path : path ,
117
118
mustNotExist : true ,
118
- fs : afero . NewOsFs () ,
119
+ fs : fs . FS ,
119
120
}, nil
120
121
}
121
122
122
123
// Load obtains the configuration from the default path allowing to persist changes (Save method)
123
- func Load () (* Config , error ) {
124
- return LoadFrom (DefaultPath )
124
+ func Load (fs machinery. Filesystem ) (* Config , error ) {
125
+ return LoadFrom (fs , DefaultPath )
125
126
}
126
127
127
128
// LoadInitialized calls Load() but returns helpful error messages if the config
128
129
// does not exist.
129
- func LoadInitialized () (* Config , error ) {
130
- c , err := Load ()
130
+ func LoadInitialized (fs machinery. Filesystem ) (* Config , error ) {
131
+ c , err := Load (fs )
131
132
if os .IsNotExist (err ) {
132
133
return nil , errors .New ("unable to find configuration file, project must be initialized" )
133
134
}
134
135
return c , err
135
136
}
136
137
137
138
// LoadFrom obtains the configuration from the provided path allowing to persist changes (Save method)
138
- func LoadFrom (path string ) (* Config , error ) {
139
- fs := afero .NewOsFs ()
140
- c , err := readFrom (fs , path )
141
- return & Config {Config : c , path : path , fs : fs }, err
139
+ func LoadFrom (fs machinery.Filesystem , path string ) (* Config , error ) {
140
+ c , err := readFrom (fs .FS , path )
141
+ return & Config {Config : c , path : path , fs : fs .FS }, err
142
142
}
143
143
144
144
// Save saves the configuration information
145
145
func (c Config ) Save () error {
146
- if c .fs == nil {
147
- c .fs = afero .NewOsFs ()
148
- }
149
146
// If path is unset, it was created directly with `Config{}`
150
147
if c .path == "" {
151
148
return saveError {errors .New ("no information where it should be stored, " +
0 commit comments