From 37a07b33471e06fc93acce7b736697b6e4a14544 Mon Sep 17 00:00:00 2001 From: Fraser Tweedale Date: Fri, 2 Aug 2024 13:26:32 +1000 Subject: [PATCH] tools: refactor: extract repo checks to utility method --- code/hsec-tools/app/Command/NextID.hs | 24 +++++------------------- code/hsec-tools/app/Command/Reserve.hs | 16 ++++------------ code/hsec-tools/app/Util.hs | 21 +++++++++++++++++++++ code/hsec-tools/hsec-tools.cabal | 1 + 4 files changed, 31 insertions(+), 31 deletions(-) create mode 100644 code/hsec-tools/app/Util.hs diff --git a/code/hsec-tools/app/Command/NextID.hs b/code/hsec-tools/app/Command/NextID.hs index a80bda99..9c581401 100644 --- a/code/hsec-tools/app/Command/NextID.hs +++ b/code/hsec-tools/app/Command/NextID.hs @@ -1,24 +1,10 @@ -{-# LANGUAGE LambdaCase #-} - module Command.NextID where -import Control.Monad (unless) -import Data.Maybe (fromMaybe) -import System.Exit (die) - -import Security.Advisories.Git (getRepoRoot) import Security.Advisories.Core.HsecId (printHsecId, getNextHsecId) -import Security.Advisories.Filesystem (isSecurityAdvisoriesRepo, getGreatestId) +import Security.Advisories.Filesystem (getGreatestId) -runNextIDCommand :: Maybe FilePath -> IO () -runNextIDCommand mPath = do - let - path = fromMaybe "." mPath - repoPath <- getRepoRoot path >>= \case - Left _ -> die "Not a git repo" - Right a -> pure a - isRepo <- isSecurityAdvisoriesRepo repoPath - unless isRepo $ - die "Not a security-advisories repo" +import Util (ensureRepo) - getGreatestId repoPath >>= getNextHsecId >>= putStrLn . printHsecId +runNextIDCommand :: Maybe FilePath -> IO () +runNextIDCommand mPath = + ensureRepo mPath >>= getGreatestId >>= getNextHsecId >>= putStrLn . printHsecId diff --git a/code/hsec-tools/app/Command/Reserve.hs b/code/hsec-tools/app/Command/Reserve.hs index 2e937d9c..1545d810 100644 --- a/code/hsec-tools/app/Command/Reserve.hs +++ b/code/hsec-tools/app/Command/Reserve.hs @@ -2,8 +2,7 @@ module Command.Reserve where -import Control.Monad (unless, when) -import Data.Maybe (fromMaybe) +import Control.Monad (when) import System.Exit (die) import System.FilePath ((), (<.>)) @@ -11,7 +10,6 @@ import Security.Advisories.Git ( add , commit , explainGitError - , getRepoRoot ) import Security.Advisories.Core.HsecId ( placeholder @@ -21,10 +19,11 @@ import Security.Advisories.Core.HsecId import Security.Advisories.Filesystem ( dirNameAdvisories , dirNameReserved - , isSecurityAdvisoriesRepo , getGreatestId ) +import Util (ensureRepo) + -- | How to choose IDs when creating advisories or -- reservations. data IdMode @@ -40,14 +39,7 @@ data CommitFlag = Commit | DoNotCommit runReserveCommand :: Maybe FilePath -> IdMode -> CommitFlag -> IO () runReserveCommand mPath idMode commitFlag = do - let - path = fromMaybe "." mPath - repoPath <- getRepoRoot path >>= \case - Left _ -> die "Not a git repo" - Right a -> pure a - isRepo <- isSecurityAdvisoriesRepo repoPath - unless isRepo $ - die "Not a security-advisories repo" + repoPath <- ensureRepo mPath hsid <- case idMode of IdModePlaceholder -> pure placeholder diff --git a/code/hsec-tools/app/Util.hs b/code/hsec-tools/app/Util.hs new file mode 100644 index 00000000..b072606a --- /dev/null +++ b/code/hsec-tools/app/Util.hs @@ -0,0 +1,21 @@ +{-# LANGUAGE LambdaCase #-} + +module Util where + +import Data.Maybe (fromMaybe) +import System.Exit (die) + +import Security.Advisories.Filesystem (isSecurityAdvisoriesRepo) +import Security.Advisories.Git (getRepoRoot) + +-- | Ensure the given path (or current directory "." if @Nothing@) +-- is an advisory Git repo. Return the (valid) repo root, or die +-- with an error message. +-- +ensureRepo :: Maybe FilePath -> IO FilePath +ensureRepo mPath = + getRepoRoot (fromMaybe "." mPath) >>= \case + Left _ -> die "Not a git repo" + Right repoPath -> isSecurityAdvisoriesRepo repoPath >>= \case + False -> die "Not a security-advisories repo" + True -> pure repoPath diff --git a/code/hsec-tools/hsec-tools.cabal b/code/hsec-tools/hsec-tools.cabal index 18bf890a..d8b2442d 100644 --- a/code/hsec-tools/hsec-tools.cabal +++ b/code/hsec-tools/hsec-tools.cabal @@ -92,6 +92,7 @@ executable hsec-tools main-is: Main.hs other-modules: Command.Reserve , Command.NextID + , Util -- Modules included in this executable, other than Main. -- other-modules: