Skip to content

Commit 79dc6de

Browse files
refactor: replace os calls with afero.Fs ones in nerdctlCommand (#213)
## Summary PR fixes #158 (comment). Besides that, it also adds a new test case, namely `"with --env-file flag, but the specified file does not exist"`, which becomes easier to add after the refactoring. ## Notes `gosec` is removed from `//nolint:errcheck,gosec` because `gosec` checks things like `os.Open` but not `fs.Open`, where `fs` is of type `afero.Fs`. Updated the `noling` comment accordingly. ## License Acceptance By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. Signed-off-by: Hsing-Yu (David) Chen <[email protected]>
1 parent e138f10 commit 79dc6de

File tree

3 files changed

+184
-92
lines changed

3 files changed

+184
-92
lines changed

cmd/finch/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ var newApp = func(logger flog.Logger, fp path.Finch, fs afero.Fs, fc *config.Fin
9090
)
9191

9292
// append nerdctl commands
93-
allCommands := initializeNerdctlCommands(lcc, logger)
93+
allCommands := initializeNerdctlCommands(lcc, logger, fs)
9494
// append finch specific commands
9595
allCommands = append(allCommands,
9696
newVersionCommand(lcc, logger, stdOut),
@@ -124,8 +124,8 @@ func virtualMachineCommands(
124124
)
125125
}
126126

127-
func initializeNerdctlCommands(lcc command.LimaCmdCreator, logger flog.Logger) []*cobra.Command {
128-
nerdctlCommandCreator := newNerdctlCommandCreator(lcc, system.NewStdLib(), logger)
127+
func initializeNerdctlCommands(lcc command.LimaCmdCreator, logger flog.Logger, fs afero.Fs) []*cobra.Command {
128+
nerdctlCommandCreator := newNerdctlCommandCreator(lcc, system.NewStdLib(), logger, fs)
129129
var allNerdctlCommands []*cobra.Command
130130
for cmdName, cmdDescription := range nerdctlCmds {
131131
allNerdctlCommands = append(allNerdctlCommands, nerdctlCommandCreator.create(cmdName, cmdDescription))

cmd/finch/nerdctl.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ package main
66
import (
77
"bufio"
88
"fmt"
9-
"os"
109
"path/filepath"
1110
"strings"
1211

12+
"github.com/spf13/afero"
1313
"github.com/spf13/cobra"
1414

1515
"github.com/runfinch/finch/pkg/command"
@@ -33,12 +33,16 @@ type nerdctlCommandCreator struct {
3333
creator command.LimaCmdCreator
3434
systemDeps NerdctlCommandSystemDeps
3535
logger flog.Logger
36+
fs afero.Fs
3637
}
3738

38-
func newNerdctlCommandCreator(creator command.LimaCmdCreator, systemDeps NerdctlCommandSystemDeps,
39+
func newNerdctlCommandCreator(
40+
creator command.LimaCmdCreator,
41+
systemDeps NerdctlCommandSystemDeps,
3942
logger flog.Logger,
43+
fs afero.Fs,
4044
) *nerdctlCommandCreator {
41-
return &nerdctlCommandCreator{creator: creator, systemDeps: systemDeps, logger: logger}
45+
return &nerdctlCommandCreator{creator: creator, systemDeps: systemDeps, logger: logger, fs: fs}
4246
}
4347

4448
func (ncc *nerdctlCommandCreator) create(cmdName string, cmdDesc string) *cobra.Command {
@@ -49,7 +53,7 @@ func (ncc *nerdctlCommandCreator) create(cmdName string, cmdDesc string) *cobra.
4953
// the args passed to nerdctlCommand.run will be empty because
5054
// cobra will try to parse `-d alpine` as if alpine is the value of the `-d` flag.
5155
DisableFlagParsing: true,
52-
RunE: newNerdctlCommand(ncc.creator, ncc.systemDeps, ncc.logger).runAdapter,
56+
RunE: newNerdctlCommand(ncc.creator, ncc.systemDeps, ncc.logger, ncc.fs).runAdapter,
5357
}
5458

5559
return command
@@ -59,10 +63,11 @@ type nerdctlCommand struct {
5963
creator command.LimaCmdCreator
6064
systemDeps NerdctlCommandSystemDeps
6165
logger flog.Logger
66+
fs afero.Fs
6267
}
6368

64-
func newNerdctlCommand(creator command.LimaCmdCreator, systemDeps NerdctlCommandSystemDeps, logger flog.Logger) *nerdctlCommand {
65-
return &nerdctlCommand{creator: creator, systemDeps: systemDeps, logger: logger}
69+
func newNerdctlCommand(creator command.LimaCmdCreator, systemDeps NerdctlCommandSystemDeps, logger flog.Logger, fs afero.Fs) *nerdctlCommand {
70+
return &nerdctlCommand{creator: creator, systemDeps: systemDeps, logger: logger, fs: fs}
6671
}
6772

6873
func (nc *nerdctlCommand) runAdapter(cmd *cobra.Command, args []string) error {
@@ -99,7 +104,7 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
99104
}
100105

101106
case strings.HasPrefix(arg, "--env-file"):
102-
shouldSkip, addEnvs, err := handleEnvFile(nc.systemDeps, arg, args[i+1])
107+
shouldSkip, addEnvs, err := handleEnvFile(nc.fs, nc.systemDeps, arg, args[i+1])
103108
if err != nil {
104109
return err
105110
}
@@ -220,7 +225,7 @@ func handleEnv(systemDeps NerdctlCommandSystemDeps, arg, arg2 string) (bool, str
220225
return skip, ""
221226
}
222227

223-
func handleEnvFile(systemDeps NerdctlCommandSystemDeps, arg, arg2 string) (bool, []string, error) {
228+
func handleEnvFile(fs afero.Fs, systemDeps NerdctlCommandSystemDeps, arg, arg2 string) (bool, []string, error) {
224229
var (
225230
filename string
226231
skip bool
@@ -234,11 +239,11 @@ func handleEnvFile(systemDeps NerdctlCommandSystemDeps, arg, arg2 string) (bool,
234239
filename = arg[11:]
235240
}
236241

237-
file, err := os.Open(filepath.Clean(filename))
242+
file, err := fs.Open(filepath.Clean(filename))
238243
if err != nil {
239244
return false, []string{}, err
240245
}
241-
defer file.Close() //nolint:errcheck,gosec // close of a file in O_RDONLY mode has no gosec issue
246+
defer file.Close() //nolint:errcheck // We did not write to the file, and the file will be closed when the CLI process exits anyway.
242247

243248
scanner := bufio.NewScanner(file)
244249

0 commit comments

Comments
 (0)