Skip to content

Commit ed48647

Browse files
authored
Merge pull request #55 from vdye/cli-bugs
Make `init` more proactive in reporting errors
2 parents 5911f5d + 7bec250 commit ed48647

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

cmd/git-bundle-server/init.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,28 @@ func (i *initCmd) Run(ctx context.Context, args []string) error {
5353
bundleProvider := utils.GetDependency[bundles.BundleProvider](ctx, i.container)
5454
gitHelper := utils.GetDependency[git.GitHelper](ctx, i.container)
5555

56+
// First, check whether route already exists (enabled or not). If it does,
57+
// exit with an error.
58+
allRepos, err := repoProvider.ReadRepositoryStorage(ctx)
59+
if err != nil {
60+
return i.logger.Errorf(ctx, "could not load existing routes: %w", err)
61+
} else if _, ok := allRepos[*route]; ok {
62+
return i.logger.Errorf(ctx, "route '%s' already exists; if you want to "+
63+
"overwrite the route, delete it with 'git-bundle-server delete' "+
64+
"then re-run this command", *route)
65+
}
66+
67+
// Create the new route
5668
repo, err := repoProvider.CreateRepository(ctx, *route)
5769
if err != nil {
5870
return i.logger.Error(ctx, err)
5971
}
6072

6173
fmt.Printf("Cloning repository from %s\n", *url)
62-
gitHelper.CloneBareRepo(ctx, *url, repo.RepoDir)
74+
err = gitHelper.CloneBareRepo(ctx, *url, repo.RepoDir)
75+
if err != nil {
76+
return i.logger.Errorf(ctx, "failed to clone repository: %w", err)
77+
}
6378

6479
bundle := bundleProvider.CreateInitialBundle(ctx, repo)
6580
fmt.Printf("Constructing base bundle file at %s\n", bundle.Filename)

internal/common/filesystem.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,13 @@ func (f *fileSystem) ReadDirRecursive(path string, depth int, strictDepth bool)
215215

216216
dirEntries, err := os.ReadDir(path)
217217
if err != nil {
218-
return nil, err
218+
if errors.Is(err, os.ErrNotExist) {
219+
// We tried to read the directory, but it doesn't exist - return
220+
// empty result.
221+
return []ReadDirEntry{}, nil
222+
} else {
223+
return nil, err
224+
}
219225
}
220226

221227
entries := utils.Map(dirEntries, mapDirEntry(path))

0 commit comments

Comments
 (0)