Skip to content

Commit

Permalink
feat(cliutils): add filesystem abstraction to Environment (#17)
Browse files Browse the repository at this point in the history
This commit adds the fsx.FS filesystem asbtraction to the Environment,
which allows users of the cliutils package to virtualise the filesystem
being used.

The default Environment uses the fsx.OsFS{} file system, which is
equivalent to directly using the stdlib.
  • Loading branch information
bassosimone authored Dec 10, 2024
1 parent 4ab111b commit f9f284f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions cliutils/cliutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (f fakecmd) Main(ctx context.Context, env cliutils.Environment, argv ...str

func TestStandardEnvironment(t *testing.T) {
env := cliutils.StandardEnvironment{}
_ = env.FS() // unclear how to test this
if env.Stdin() != os.Stdin {
t.Fatal("expected os.Stdin")
}
Expand Down
10 changes: 10 additions & 0 deletions cliutils/clutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import (
"fmt"
"io"
"os"

"github.com/rbmk-project/common/fsx"
)

// Environment is the environment for executing a [Command].
type Environment interface {
// FS returns the virtual filesystem to use.
FS() fsx.FS

// Stdin returns the stdin reader to use.
Stdin() io.Reader

Expand All @@ -29,6 +34,11 @@ type StandardEnvironment struct{}
// Ensure that [StandardEnvironment] implements [Environment].
var _ Environment = StandardEnvironment{}

// FS implements [Environment].
func (se StandardEnvironment) FS() fsx.FS {
return fsx.OsFS{}
}

// Stdin implements [Environment].
func (se StandardEnvironment) Stdin() io.Reader {
return os.Stdin
Expand Down

0 comments on commit f9f284f

Please sign in to comment.