From 60f717b97ce6c375679080472750e60aab9dcd8f Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 18 Apr 2026 08:47:27 +0300 Subject: refactor: move Gitter interface to consumer package internal/cli (task p6) Per Go best practice (100 Go Mistakes #6): interfaces belong where they are used, not where they are implemented. Move Gitter from internal/git to internal/cli/git.go. Compile-time assertions (var _ Gitter = ...) kept in the new location. internal/git retains concrete *Git and *NoOp types. Co-Authored-By: Claude Sonnet 4.6 --- internal/git/git.go | 34 ++++------------------------------ internal/git/git_test.go | 13 ++++++++----- internal/git/noop.go | 9 +++++---- 3 files changed, 17 insertions(+), 39 deletions(-) (limited to 'internal/git') diff --git a/internal/git/git.go b/internal/git/git.go index 72d55af..e9a1b9b 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -2,10 +2,10 @@ // It mirrors the Git module from the original Ruby implementation (geheim.rb lines 79-123), // running real git subprocesses rather than using a Go git library. // -// The package exposes a Gitter interface so that callers can accept either a -// real *Git (backed by a git repository) or a *NoOp stub (for directories that -// are not git repositories). This avoids nil-pointer panics in the CLI dispatch -// loop and keeps git-related decisions local to this package. +// The package exposes concrete types (*Git and *NoOp) but does NOT define a +// Gitter interface. Per Go best practices (100 Go Mistakes #6), interfaces +// belong in the consumer package — the Gitter interface lives in internal/cli, +// which is the only package that needs to abstract over *Git vs *NoOp. package git import ( @@ -18,37 +18,11 @@ import ( "strings" ) -// Gitter is the interface that both *Git (real git operations) and *NoOp -// (informational no-ops) implement. The CLI holds a Gitter so that it can be -// freely swapped without changing any dispatch logic. -type Gitter interface { - // Add stages a single file for the next commit. - Add(ctx context.Context, filePath string) error - - // Remove stages a file deletion for the next commit. - Remove(ctx context.Context, filePath string) error - - // Status prints the current git status of the working directory. - Status(ctx context.Context) error - - // Commit records all staged changes with a generic commit message. - Commit(ctx context.Context) error - - // Reset discards all uncommitted changes in the working directory. - Reset(ctx context.Context) error - - // Sync pulls from and pushes to each configured remote repository. - Sync(ctx context.Context, syncRepos []string) error -} - // Git provides git operations scoped to the secret store's data directory. type Git struct { dataDir string } -// Compile-time assertion: *Git must satisfy Gitter. -var _ Gitter = (*Git)(nil) - // New creates a Git helper for the given data directory. func New(dataDir string) *Git { return &Git{dataDir: dataDir} diff --git a/internal/git/git_test.go b/internal/git/git_test.go index 462c747..4c22fec 100644 --- a/internal/git/git_test.go +++ b/internal/git/git_test.go @@ -283,11 +283,14 @@ func TestIsGitRepo_outside(t *testing.T) { } } -// TestNoOp_satisfies_Gitter verifies that *git.NoOp compiles as a Gitter and -// that all its methods return nil (no-op, no error) so they are safe to call -// unconditionally from CLI dispatch. -func TestNoOp_satisfies_Gitter(t *testing.T) { - var g git.Gitter = git.NewNoOp() +// TestNoOp_methods verifies that all *git.NoOp methods return nil (no-op, no +// error) so they are safe to call unconditionally from CLI dispatch. +// +// Note: the Gitter interface is defined in internal/cli (the consumer), not +// here in internal/git (the producer), per Go best practice #6. The +// compile-time assertion that *NoOp satisfies Gitter lives in internal/cli/git.go. +func TestNoOp_methods(t *testing.T) { + g := git.NewNoOp() ctx := context.Background() table := []struct { diff --git a/internal/git/noop.go b/internal/git/noop.go index 44dc68c..f125644 100644 --- a/internal/git/noop.go +++ b/internal/git/noop.go @@ -9,7 +9,7 @@ import ( // because the kdbx file is not inside a git repository. const noOpMessage = "kdbx file is not in a git repo; skipping" -// NoOp is a Gitter implementation whose every method prints an informational +// NoOp is a no-op git client whose every method prints an informational // message and returns nil. It is used when the KeePass database file lives // outside of a git repository so that sync/status/commit/reset commands remain // functional and transparent rather than crashing or returning errors. @@ -17,11 +17,12 @@ const noOpMessage = "kdbx file is not in a git repo; skipping" // Keeping the no-op behaviour in its own type (rather than nil-checking in the // CLI dispatch) respects the Open/Closed Principle: the CLI is open for // extension (new backends, new git behaviours) without modification. +// +// NoOp satisfies the Gitter interface defined in internal/cli (the consumer), +// not here in the producer — per Go best practice #6 from 100 Go Mistakes. +// The compile-time assertion lives in internal/cli/git.go. type NoOp struct{} -// Compile-time assertion: *NoOp must satisfy Gitter. -var _ Gitter = (*NoOp)(nil) - // NewNoOp returns a *NoOp that satisfies Gitter with all operations being // informational no-ops. func NewNoOp() *NoOp { -- cgit v1.2.3