From c4034a291fb67eb3613a1b84cf40f7af77cde78d Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 30 Jun 2018 21:02:09 +0100 Subject: [PATCH] InitGitDBRepo return Result values --- .../net/kemitix/gitdb/impl/InitGitDBRepo.java | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java index ae39f7b..b2cc831 100644 --- a/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java +++ b/src/main/java/net/kemitix/gitdb/impl/InitGitDBRepo.java @@ -51,32 +51,26 @@ class InitGitDBRepo { * Initialise a new GitDB repo. * * @param dbDir the directory to initialise the repo in - * @throws IOException if there is an error in creating the repo files */ static Result create(final Path dbDir) { final InitGitDBRepo initRepo = new InitGitDBRepo(); - final File validDbDir; - try { - validDbDir = initRepo.validDbDir(dbDir.toFile()); - } catch (IOException e) { - return Result.error(e); - } - validDbDir.mkdirs(); - try (Repository repository = RepositoryCache.FileKey.exact(validDbDir, FS.DETECTED).open(false)) { - repository.create(true); - initRepo.createInitialBranchOnMaster(repository); - } catch (IOException e) { - return Result.error(e); - } - return Result.ok(null); + return initRepo.validDbDir(dbDir.toFile()) + .peek(File::mkdirs) + .flatMap(dir -> { + try (Repository repository = RepositoryCache.FileKey.exact(dir, FS.DETECTED).open(false)) { + repository.create(true); + initRepo.createInitialBranchOnMaster(repository); + } catch (IOException e) { + return Result.error(e); + } + return Result.ok(null); + }); } - private File validDbDir(final File dbDir) throws IOException { - verifyIsNotAFile(dbDir); - if (dbDir.exists()) { - verifyIsEmpty(dbDir); - } - return dbDir; + private Result validDbDir(final File dbDir) { + return Result.ok(dbDir) + .flatMap(this::verifyIsNotAFile) + .flatMap(this::isEmptyIfExists); } private Result createInitialBranchOnMaster(final Repository repository) { @@ -91,18 +85,26 @@ class InitGitDBRepo { })); } - private void verifyIsNotAFile(final File dbDir) throws NotDirectoryException { + private Result verifyIsNotAFile(final File dbDir) { if (dbDir.isFile()) { - throw new NotDirectoryException(dbDir.toString()); + return Result.error(new NotDirectoryException(dbDir.toString())); } + return Result.ok(dbDir); } - private void verifyIsEmpty(final File dbDir) throws IOException { - try (DirectoryStream directoryStream = Files.newDirectoryStream(dbDir.toPath())) { - if (directoryStream.iterator().hasNext()) { - throw new DirectoryNotEmptyException(dbDir.toString()); - } + private Result isEmptyIfExists(final File dbDir) { + if (dbDir.exists()) { + return Result.of(() -> { + try (DirectoryStream directoryStream = Files.newDirectoryStream(dbDir.toPath())) { + if (directoryStream.iterator().hasNext()) { + throw new DirectoryNotEmptyException(dbDir.toString()); + } + } + return dbDir; + } + ); } + return Result.ok(dbDir); } private Result createBranch(