Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
kkrull committed Aug 8, 2024
1 parent 48059bd commit 72782ae
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 27 deletions.
22 changes: 4 additions & 18 deletions src/go/userepository/register_local_repositories_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package userepository
import (
"fmt"
"path/filepath"
"slices"

core "github.com/kkrull/marmot/corerepository"
)
Expand All @@ -14,31 +13,18 @@ type RegisterLocalRepositoriesCommand struct {
}

func (cmd *RegisterLocalRepositoriesCommand) Run(localPaths []string) error {
distinctNormalizedPaths := make([]string, 0)
for _, rawPath := range localPaths {
absolutePaths := make([]string, len(localPaths))
for i, rawPath := range localPaths {
if absPath, absErr := filepath.Abs(rawPath); absErr != nil {
return absErr
} else if isDuplicate := slices.Contains(distinctNormalizedPaths, absPath); isDuplicate {
continue
} else {
distinctNormalizedPaths = append(distinctNormalizedPaths, absPath)
absolutePaths[i] = absPath
}
}

if addErr := cmd.Source.AddLocals(distinctNormalizedPaths); addErr != nil {
if addErr := cmd.Source.AddLocals(absolutePaths); addErr != nil {
return fmt.Errorf("failed to add local repositories; %w", addErr)
} else {
return nil
}
}

func normalizePaths(rawPaths []string) []string {
normalized := make([]string, len(rawPaths))
for i, rawPath := range rawPaths {
if normalPath, pathErr := filepath.Abs(rawPath); pathErr == nil {
normalized[i] = normalPath
}
}

return normalized
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ var _ = Describe("RegisterLocalRepositoriesCommand", func() {
MatchError(ContainSubstring("failed to add local repositories; bang!")))
})

It("ignores distinct paths that resolve to an already-known absolute path", func() {
subject.Run([]string{"somewhere", "./somewhere"})
Expect(source.AddLocalsReceived()).To(HaveLen(1))
Expect(source.AddLocalsReceived()[0]).To(HaveSuffix("somewhere"))
})

It("normalizes paths by replacing redundant/repeated parts with shorter equivalents", func() {
subject.Run([]string{"/path/to/a/../b"})
source.AddLocalsExpected("/path/to/b")
Expand All @@ -67,8 +61,5 @@ var _ = Describe("RegisterLocalRepositoriesCommand", func() {
Expect(source.AddLocalsReceived()[0]).To(
HaveSuffix(string(os.PathSeparator) + "some-name-in-cwd"))
})

It("rejects invalid paths", Pending)
It("rejects paths that do not exist", Pending)
})
})

0 comments on commit 72782ae

Please sign in to comment.