Skip to content

Commit c2f43a4

Browse files
authored
pkg/scaffold: use afero.Fs instead of os package directly (#1083)
* add an `afero.Fs` field to Scaffold and use it in place of directly calling functions from the `os` package * add `SetFS()` to the `CustomRenderer` interface to inject `Scaffold` fs'
1 parent a378cc5 commit c2f43a4

File tree

7 files changed

+34
-13
lines changed

7 files changed

+34
-13
lines changed

internal/util/fileutil/file_util.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ type FileWriter struct {
4242
once sync.Once
4343
}
4444

45-
func NewFileWriter() *FileWriter {
46-
return NewFileWriterFS(afero.NewOsFs())
47-
}
48-
4945
func NewFileWriterFS(fs afero.Fs) *FileWriter {
5046
fw := &FileWriter{}
5147
fw.once.Do(func() {

pkg/scaffold/ansible/k8s_status.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package ansible
1616

1717
import (
1818
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
19+
"github.com/spf13/afero"
1920
)
2021

2122
const K8sStatusPythonFile = "library/k8s_status.py"
@@ -30,10 +31,11 @@ func (k *K8sStatus) GetInput() (input.Input, error) {
3031
if k.Path == "" {
3132
k.Path = K8sStatusPythonFile
3233
}
33-
k.TemplateBody = k8sStatusTmpl
3434
return k.Input, nil
3535
}
3636

37+
func (s K8sStatus) SetFS(_ afero.Fs) {}
38+
3739
func (k K8sStatus) CustomRender() ([]byte, error) {
3840
return []byte(k8sStatusTmpl), nil
3941
}

pkg/scaffold/crd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ func initCache() {
7676
})
7777
}
7878

79+
func (s *CRD) SetFS(_ afero.Fs) {}
80+
7981
func (s *CRD) CustomRender() ([]byte, error) {
8082
i, _ := s.GetInput()
8183
// controller-tools generates crd file names with no _crd.yaml suffix:

pkg/scaffold/customrender.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@
1414

1515
package scaffold
1616

17+
import "github.com/spf13/afero"
18+
1719
// CustomRenderer is the interface for writing any scaffold file that does
1820
// not use a template.
1921
type CustomRenderer interface {
22+
// SetFS sets the fs in the CustomRenderer's underlying type if it exists.
23+
// SetFS is used to inject the callers' fs into a CustomRenderer, which may
24+
// want to write/read from the same fs.
25+
SetFS(afero.Fs)
26+
// CustomRender performs arbitrary rendering of file data and returns
27+
// bytes to write to a file.
2028
CustomRender() ([]byte, error)
2129
}

pkg/scaffold/olm-catalog/concat_crd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"github.com/operator-framework/operator-sdk/internal/util/yamlutil"
2222
"github.com/operator-framework/operator-sdk/pkg/scaffold"
2323
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
24+
25+
"github.com/spf13/afero"
2426
)
2527

2628
const ConcatCRDYamlFile = "_generated.concat_crd.yaml"
@@ -46,6 +48,8 @@ func (s *ConcatCRD) GetInput() (input.Input, error) {
4648
return s.Input, nil
4749
}
4850

51+
func (s *ConcatCRD) SetFS(_ afero.Fs) {}
52+
4953
// CustomRender returns the bytes of all CRD manifests concatenated into one file.
5054
func (s *ConcatCRD) CustomRender() ([]byte, error) {
5155
cfg, err := getCSVConfig(s.ConfigFilePath)

pkg/scaffold/olm-catalog/csv.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ func (s *CSV) GetInput() (input.Input, error) {
8282
return s.Input, nil
8383
}
8484

85+
func (s *CSV) SetFS(fs afero.Fs) { s.initFS(fs) }
86+
8587
// CustomRender allows a CSV to be written by marshalling
8688
// olmapiv1alpha1.ClusterServiceVersion instead of writing to a template.
8789
func (s *CSV) CustomRender() ([]byte, error) {

pkg/scaffold/scaffold.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ import (
2929
"github.com/operator-framework/operator-sdk/pkg/scaffold/input"
3030

3131
log "github.com/sirupsen/logrus"
32+
"github.com/spf13/afero"
3233
"golang.org/x/tools/imports"
3334
)
3435

3536
// Scaffold writes Templates to scaffold new files
3637
type Scaffold struct {
3738
// Repo is the go project package
3839
Repo string
39-
4040
// AbsProjectPath is the absolute path to the project root, including the project directory.
4141
AbsProjectPath string
42-
4342
// ProjectName is the operator's name, ex. app-operator
4443
ProjectName string
45-
44+
// Fs is the filesystem GetWriter uses to write scaffold files.
45+
Fs afero.Fs
46+
// GetWriter returns a writer for writing scaffold files.
4647
GetWriter func(path string, mode os.FileMode) (io.Writer, error)
4748
}
4849

@@ -74,8 +75,11 @@ func (s *Scaffold) configure(cfg *input.Config) {
7475

7576
// Execute executes scaffolding the Files
7677
func (s *Scaffold) Execute(cfg *input.Config, files ...input.File) error {
78+
if s.Fs == nil {
79+
s.Fs = afero.NewOsFs()
80+
}
7781
if s.GetWriter == nil {
78-
s.GetWriter = fileutil.NewFileWriter().WriteCloser
82+
s.GetWriter = fileutil.NewFileWriterFS(s.Fs).WriteCloser
7983
}
8084

8185
// Configure s using common fields from cfg.
@@ -107,7 +111,7 @@ func (s *Scaffold) doFile(e input.File) error {
107111
absFilePath := filepath.Join(s.AbsProjectPath, i.Path)
108112

109113
// Check if the file to write already exists
110-
if _, err := os.Stat(absFilePath); err == nil || os.IsExist(err) {
114+
if _, err := s.Fs.Stat(absFilePath); err == nil || os.IsExist(err) {
111115
switch i.IfExistsAction {
112116
case input.Overwrite:
113117
case input.Skip:
@@ -141,6 +145,7 @@ func (s *Scaffold) doRender(i input.Input, e input.File, absPath string) error {
141145

142146
var b []byte
143147
if c, ok := e.(CustomRenderer); ok {
148+
c.SetFS(s.Fs)
144149
// CustomRenderers have a non-template method of file rendering.
145150
if b, err = c.CustomRender(); err != nil {
146151
return err
@@ -168,9 +173,11 @@ func (s *Scaffold) doRender(i input.Input, e input.File, absPath string) error {
168173
}
169174

170175
// Files being overwritten must be trucated to len 0 so no old bytes remain.
171-
if _, err = os.Stat(absPath); err == nil && i.IfExistsAction == input.Overwrite {
172-
if err = os.Truncate(absPath, 0); err != nil {
173-
return err
176+
if _, err = s.Fs.Stat(absPath); err == nil && i.IfExistsAction == input.Overwrite {
177+
if file, ok := f.(afero.File); ok {
178+
if err = file.Truncate(0); err != nil {
179+
return err
180+
}
174181
}
175182
}
176183
_, err = f.Write(b)

0 commit comments

Comments
 (0)