Skip to content

Commit

Permalink
refactor(pkg/source): rename all Filesystem types to Source
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Jul 31, 2023
1 parent b63c307 commit 166ba29
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion cmd/cupd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func serve(ctx *cli.Context) error {
return fmt.Errorf("scm type not supported: %q", src.Git.SCM)
}

fs, err = git.NewFilesystem(ctx.Context, scm, src.Git.URL.String(), git.WithAuth(
fs, err = git.NewSource(ctx.Context, scm, src.Git.URL.String(), git.WithAuth(
&githttp.BasicAuth{
Username: user,
Password: pass,
Expand Down
4 changes: 2 additions & 2 deletions docs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The goal is to provide users with a way to manage the logical resources represen
│ │ │ └───────▲─────────┬────────┘ │ │ │ │ │ │
│ A ◀────────────┤ │ │ │ │ │ │ │ │
│ P │ │ ┌───────┴─────────▼────────┐ │ │ │ └─────────────────────┘ │
│ I │ │ │ Filesystem ◀─┼──┘ ┌───▶ │
│ I │ │ │ Source ◀─┼──┘ ┌───▶ │
│ │ │ │ │ │ │ │ │
│ S │ │ └──────────────────────────┘ ├─────┘ └──────────────────────────────────┘
│ e │ └──────────────────────────────┘ ┌──────────────────────────────────┐
Expand Down Expand Up @@ -329,7 +329,7 @@ This diagram gives an overview of the flow of a successsful `PUT` request:
sequenceDiagram
participant A as Actor
participant S as API Server
participant F as git.FilesystemStore
participant F as git.Source
participant C as Controller
participant R as Runtime (WASM)
participant G as Git
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
"golang.org/x/exp/slog"
)

// ViewFunc is a function provided to FilesystemStore.View.
// It is provided with a read-only view of the target filesystem.
// ViewFunc is a function provided to Source.View.
// It is provided with a read-only view of the target source.
type ViewFunc func(fs.FS) error

// UpdateFunc is a function passed to a FilesystemStore implementation to be invoked
// UpdateFunc is a function passed to a Source implementation to be invoked
// over a provided FSConfig in a call to Update.
type UpdateFunc func(controllers.FSConfig) error

// Result is the result of performing an update on a target FilesystemStore.
// Result is the result of performing an update on a target Source.
type Result struct {
ID ulid.ULID
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/source/git/filesystem_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// WaitForUpdate is used by locally packaged tests to block until either the provided
// deadline is exceeded or the filesystem gets an update from a fetch call
func WaitForUpdate(t *testing.T, f *Filesystem, d time.Duration) error {
func WaitForUpdate(t *testing.T, f *Source, d time.Duration) error {
select {
case <-time.After(d):
return context.DeadlineExceeded
Expand Down
28 changes: 14 additions & 14 deletions pkg/source/git/filesystem.go → pkg/source/git/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"golang.org/x/exp/slog"
)

var _ api.Source = (*Filesystem)(nil)
var _ api.Source = (*Source)(nil)

// Proposal is the internal representation of what becomes a pull or merge request
// on a target SCM.
Expand All @@ -45,11 +45,11 @@ type SCM interface {
Propose(context.Context, Proposal) (ProposalResponse, error)
}

// FilesystemStore is an implementation of api.FilesystemStore
// Source is an implementation of api.Source
// This implementation is backed by a Git repository and it tracks an upstream reference.
// When subscribing to this source, the upstream reference is tracked
// by polling the upstream on a configurable interval.
type Filesystem struct {
type Source struct {
logger *slog.Logger
repo *git.Repository
storage *memory.Storage
Expand All @@ -67,25 +67,25 @@ type Filesystem struct {

// WithPollInterval configures the interval in which origin is polled to
// discover any updates to the target reference.
func WithPollInterval(tick time.Duration) containers.Option[Filesystem] {
return func(s *Filesystem) {
func WithPollInterval(tick time.Duration) containers.Option[Source] {
return func(s *Source) {
s.interval = tick
}
}

// WithAuth returns an option which configures the auth method used
// by the provided source.
func WithAuth(auth transport.AuthMethod) containers.Option[Filesystem] {
return func(s *Filesystem) {
func WithAuth(auth transport.AuthMethod) containers.Option[Source] {
return func(s *Source) {
s.auth = auth
}
}

// NewFilesystem constructs and configures a Git backend Filesystem.
// NewSource constructs and configures a Git backend Source.
// The implementation uses the connection and credential details provided to support
// view and update requests for use in the api server.
func NewFilesystem(ctx context.Context, scm SCM, url string, opts ...containers.Option[Filesystem]) (_ *Filesystem, err error) {
fs := &Filesystem{
func NewSource(ctx context.Context, scm SCM, url string, opts ...containers.Option[Source]) (_ *Source, err error) {
fs := &Source{
logger: slog.With(slog.String("repository", url)),
url: url,
scm: scm,
Expand All @@ -110,7 +110,7 @@ func NewFilesystem(ctx context.Context, scm SCM, url string, opts ...containers.

// View builds a new fs.FS based on the configure Git remote and reference.
// It call the provided function with the derived fs.FS.
func (s *Filesystem) View(ctx context.Context, rev string, fn api.ViewFunc) error {
func (s *Source) View(ctx context.Context, rev string, fn api.ViewFunc) error {
hash, err := s.resolve(rev)
if err != nil {
return err
Expand All @@ -129,7 +129,7 @@ func (s *Filesystem) View(ctx context.Context, rev string, fn api.ViewFunc) erro
// Any changes made during the function call to the underlying worktree are added commit and pushed to the
// target Git repository.
// Once pushed a proposal is made on the configured SCM.
func (s *Filesystem) Update(ctx context.Context, rev, message string, fn api.UpdateFunc) (*api.Result, error) {
func (s *Source) Update(ctx context.Context, rev, message string, fn api.UpdateFunc) (*api.Result, error) {
hash, err := s.resolve(rev)
if err != nil {
return nil, err
Expand Down Expand Up @@ -233,7 +233,7 @@ func (s *Filesystem) Update(ctx context.Context, rev, message string, fn api.Upd
return result, nil
}

func (s *Filesystem) resolve(r string) (plumbing.Hash, error) {
func (s *Source) resolve(r string) (plumbing.Hash, error) {
if plumbing.IsHash(r) {
return plumbing.NewHash(r), nil
}
Expand All @@ -246,7 +246,7 @@ func (s *Filesystem) resolve(r string) (plumbing.Hash, error) {
return ref.Hash(), nil
}

func (s *Filesystem) pollRefs(ctx context.Context) {
func (s *Source) pollRefs(ctx context.Context) {
ticker := time.NewTicker(s.interval)
for {
select {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (

var gitRepoURL = os.Getenv("TEST_GIT_REPO_URL")

func Test_Filesystem_View(t *testing.T) {
func Test_Source_View(t *testing.T) {
ctx := context.Background()
fss, _, skipped := testFilesystem(t, ctx)
fss, _, skipped := testSource(t, ctx)
if skipped {
return
}
Expand Down Expand Up @@ -65,10 +65,10 @@ func Test_Filesystem_View(t *testing.T) {
assert.Equal(t, testdataContents, files)
}

func Test_Filesystem_Update(t *testing.T) {
func Test_Source_Update(t *testing.T) {
ctx := context.Background()

fss, scm, skipped := testFilesystem(t, ctx)
fss, scm, skipped := testSource(t, ctx)
if skipped {
return
}
Expand All @@ -91,7 +91,7 @@ func Test_Filesystem_Update(t *testing.T) {

require.NoError(t, scm.Merge(ctx, result.ID))

// attempt to block until the Filesystem gets an update
// attempt to block until the Source gets an update
git.WaitForUpdate(t, fss, 30*time.Second)

files := map[string][]byte{}
Expand Down Expand Up @@ -134,7 +134,7 @@ type scm interface {
Merge(context.Context, ulid.ULID) error
}

func testFilesystem(t *testing.T, ctx context.Context, opts ...containers.Option[git.Filesystem]) (*git.Filesystem, scm, bool) {
func testSource(t *testing.T, ctx context.Context, opts ...containers.Option[git.Source]) (*git.Source, scm, bool) {
t.Helper()

if gitRepoURL == "" {
Expand All @@ -161,8 +161,8 @@ func testFilesystem(t *testing.T, ctx context.Context, opts ...containers.Option

scm := giteascm.New(client, owner, strings.TrimSuffix(repo, ".git"))

fs, err := git.NewFilesystem(ctx, scm, gitRepoURL,
append([]containers.Option[git.Filesystem]{
fs, err := git.NewSource(ctx, scm, gitRepoURL,
append([]containers.Option[git.Source]{
git.WithPollInterval(5 * time.Second),
git.WithAuth(&http.BasicAuth{
Username: "root",
Expand Down
14 changes: 7 additions & 7 deletions pkg/source/local/filesystem.go → pkg/source/local/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import (
"go.flipt.io/cup/pkg/controllers"
)

// Filesystem implements the abstraction required by an *api.Server
// Source implements the abstraction required by an *api.Server
// to read and update a target source filesystem.
// This implementation works directly over the host.
type Filesystem struct {
type Source struct {
path string
}

// New constructs and configures a new instance of *Filesystem
// New constructs and configures a new instance of *Source
// for the provided path.
func New(path string) *Filesystem {
return &Filesystem{path: path}
func New(path string) *Source {
return &Source{path: path}
}

// View invokes the provided function with an fs.FS which should enforce
// a read-only view for the requested source and revision.
func (f *Filesystem) View(_ context.Context, revision string, fn api.ViewFunc) error {
func (f *Source) View(_ context.Context, revision string, fn api.ViewFunc) error {
return fn(billyfs.New(osfs.New(f.path)))
}

// Update invokes the provided function with an FSConfig which can be written to
// Any writes performed to the target during the execution of fn will be added,
// comitted, pushed and proposed for review on a target SCM.
func (f *Filesystem) Update(_ context.Context, revision string, message string, fn api.UpdateFunc) (*api.Result, error) {
func (f *Source) Update(_ context.Context, revision string, message string, fn api.UpdateFunc) (*api.Result, error) {
return &api.Result{}, fn(controllers.FSConfig{
FS: osfs.New(f.path),
Dir: &f.path,
Expand Down
18 changes: 9 additions & 9 deletions pkg/source/mem/store.go → pkg/source/mem/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ import (
"go.flipt.io/cup/pkg/controllers"
)

var _ api.Source = (*Filesystem)(nil)
var _ api.Source = (*Source)(nil)

// Filesystem is primarily used for testing.
// Source is primarily used for testing.
// The implementations are indexed by revision internally.
// It supports writes through the billyfs abstraction.
// However, instead of proposals, these are direct writes to the underlying filesystem.
type Filesystem struct {
type Source struct {
revs containers.MapStore[string, billy.Filesystem]
}

// New constructs a new instance of FilesystemStore
func New() *Filesystem {
return &Filesystem{revs: containers.MapStore[string, billy.Filesystem]{}}
// New constructs a new instance of a Source
func New() *Source {
return &Source{revs: containers.MapStore[string, billy.Filesystem]{}}
}

// AddFS registers a new fs.FS to be supplied on calls to View and Update
func (f *Filesystem) AddFS(revision string, ffs billy.Filesystem) {
func (f *Source) AddFS(revision string, ffs billy.Filesystem) {
f.revs[revision] = ffs
}

// View invokes the provided function with an FSConfig which should enforce
// a read-only view for the requested source and revision
func (f *Filesystem) View(_ context.Context, revision string, fn api.ViewFunc) error {
func (f *Source) View(_ context.Context, revision string, fn api.ViewFunc) error {
fs, err := f.revs.Get(revision)
if err != nil {
return fmt.Errorf("view: %w", err)
Expand All @@ -45,7 +45,7 @@ func (f *Filesystem) View(_ context.Context, revision string, fn api.ViewFunc) e
// Update invokes the provided function with an FSConfig which can be written to
// Any writes performed to the target during the execution of fn will be added,
// comitted, pushed and proposed for review on a target SCM
func (f *Filesystem) Update(_ context.Context, revision, _ string, fn api.UpdateFunc) (*api.Result, error) {
func (f *Source) Update(_ context.Context, revision, _ string, fn api.UpdateFunc) (*api.Result, error) {
fs, err := f.revs.Get(revision)
if err != nil {
return nil, fmt.Errorf("update: %w", err)
Expand Down

0 comments on commit 166ba29

Please sign in to comment.