Skip to content

Make init more proactive in reporting errors #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cmd/git-bundle-server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,28 @@ func (i *initCmd) Run(ctx context.Context, args []string) error {
bundleProvider := utils.GetDependency[bundles.BundleProvider](ctx, i.container)
gitHelper := utils.GetDependency[git.GitHelper](ctx, i.container)

// First, check whether route already exists (enabled or not). If it does,
// exit with an error.
allRepos, err := repoProvider.ReadRepositoryStorage(ctx)
if err != nil {
return i.logger.Errorf(ctx, "could not load existing routes: %w", err)
} else if _, ok := allRepos[*route]; ok {
return i.logger.Errorf(ctx, "route '%s' already exists; if you want to "+
"overwrite the route, delete it with 'git-bundle-server delete' "+
"then re-run this command", *route)
}

// Create the new route
repo, err := repoProvider.CreateRepository(ctx, *route)
if err != nil {
return i.logger.Error(ctx, err)
}

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

bundle := bundleProvider.CreateInitialBundle(ctx, repo)
fmt.Printf("Constructing base bundle file at %s\n", bundle.Filename)
Expand Down
8 changes: 7 additions & 1 deletion internal/common/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ func (f *fileSystem) ReadDirRecursive(path string, depth int, strictDepth bool)

dirEntries, err := os.ReadDir(path)
if err != nil {
return nil, err
if errors.Is(err, os.ErrNotExist) {
// We tried to read the directory, but it doesn't exist - return
// empty result.
return []ReadDirEntry{}, nil
} else {
return nil, err
}
}

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