From 274688d8a23aeb996a19fefd230cd51ca4be8146 Mon Sep 17 00:00:00 2001 From: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com> Date: Tue, 18 Jun 2024 19:44:27 +0530 Subject: [PATCH] Clean up unused code (#1502) ## Changes 1. Removes `DefaultMutatorsForTarget` which is no longer used anywhere 2. Makes SnapshotPath a private field. It's no longer needed by data structures outside its package. FYI, I also tried finding other instances of dead code but I could not find anything else that was safe to remove. I used https://go.dev/blog/deadcode to search for them, and the other instances either implemented an interface, increased test coverage for some of our other code paths or there was some other reason I could not remove them (like autogenerated functions or used in tests). Good sign our codebase is mostly clean (at least superficially). --- bundle/config/mutator/mutator.go | 7 ------- libs/sync/snapshot.go | 14 +++++++------- libs/sync/sync.go | 4 ---- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/bundle/config/mutator/mutator.go b/bundle/config/mutator/mutator.go index ae0d7e5fb..7d7711118 100644 --- a/bundle/config/mutator/mutator.go +++ b/bundle/config/mutator/mutator.go @@ -26,10 +26,3 @@ func DefaultMutators() []bundle.Mutator { LoadGitDetails(), } } - -func DefaultMutatorsForTarget(target string) []bundle.Mutator { - return append( - DefaultMutators(), - SelectTarget(target), - ) -} diff --git a/libs/sync/snapshot.go b/libs/sync/snapshot.go index b46bd19f4..f2920d8c2 100644 --- a/libs/sync/snapshot.go +++ b/libs/sync/snapshot.go @@ -35,7 +35,7 @@ const LatestSnapshotVersion = "v1" type Snapshot struct { // Path where this snapshot was loaded from and will be saved to. // Intentionally not part of the snapshot state because it may be moved by the user. - SnapshotPath string `json:"-"` + snapshotPath string // New indicates if this is a fresh snapshot or if it was loaded from disk. New bool `json:"-"` @@ -70,7 +70,7 @@ func NewSnapshot(localFiles []fileset.File, opts *SyncOptions) (*Snapshot, error snapshotState.ResetLastModifiedTimes() return &Snapshot{ - SnapshotPath: snapshotPath, + snapshotPath: snapshotPath, New: true, Version: LatestSnapshotVersion, Host: opts.Host, @@ -107,7 +107,7 @@ func newSnapshot(ctx context.Context, opts *SyncOptions) (*Snapshot, error) { } return &Snapshot{ - SnapshotPath: path, + snapshotPath: path, New: true, Version: LatestSnapshotVersion, @@ -122,7 +122,7 @@ func newSnapshot(ctx context.Context, opts *SyncOptions) (*Snapshot, error) { } func (s *Snapshot) Save(ctx context.Context) error { - f, err := os.OpenFile(s.SnapshotPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) + f, err := os.OpenFile(s.snapshotPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) if err != nil { return fmt.Errorf("failed to create/open persisted sync snapshot file: %s", err) } @@ -147,11 +147,11 @@ func loadOrNewSnapshot(ctx context.Context, opts *SyncOptions) (*Snapshot, error } // Snapshot file not found. We return the new copy. - if _, err := os.Stat(snapshot.SnapshotPath); errors.Is(err, fs.ErrNotExist) { + if _, err := os.Stat(snapshot.snapshotPath); errors.Is(err, fs.ErrNotExist) { return snapshot, nil } - bytes, err := os.ReadFile(snapshot.SnapshotPath) + bytes, err := os.ReadFile(snapshot.snapshotPath) if err != nil { return nil, fmt.Errorf("failed to read sync snapshot from disk: %s", err) } @@ -191,7 +191,7 @@ func (s *Snapshot) diff(ctx context.Context, all []fileset.File) (diff, error) { currentState := s.SnapshotState if err := currentState.validate(); err != nil { - return diff{}, fmt.Errorf("error parsing existing sync state. Please delete your existing sync snapshot file (%s) and retry: %w", s.SnapshotPath, err) + return diff{}, fmt.Errorf("error parsing existing sync state. Please delete your existing sync snapshot file (%s) and retry: %w", s.snapshotPath, err) } // Compute diff to apply to get from current state to new target state. diff --git a/libs/sync/sync.go b/libs/sync/sync.go index 12b1f1d05..3d5bc61ec 100644 --- a/libs/sync/sync.go +++ b/libs/sync/sync.go @@ -223,10 +223,6 @@ func (s *Sync) GetFileList(ctx context.Context) ([]fileset.File, error) { return all.Iter(), nil } -func (s *Sync) SnapshotPath() string { - return s.snapshot.SnapshotPath -} - func (s *Sync) RunContinuous(ctx context.Context) error { ticker := time.NewTicker(s.PollInterval) defer ticker.Stop()