Skip to content

Commit

Permalink
make the preflight check filename unique
Browse files Browse the repository at this point in the history
  • Loading branch information
spatten committed Jan 16, 2025
1 parent 2b33d1f commit f23a11b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/App/Fossa/PreflightChecks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import Control.Monad (void, when)
import Data.Error (createErrataWithHeaderOnly)
import Data.Text
import Data.Text.IO qualified as TIO
import Data.UUID qualified as UUID (toString)
import Data.UUID.V4 qualified as UUID (nextRandom)
import Diag.Diagnostic (ToDiagnostic (..))
import Effect.Logger (renderIt)
import Errata (Errata (..))
Expand All @@ -30,7 +32,7 @@ import Path (
Path,
Rel,
fromAbsFile,
mkRelFile,
parseRelFile,
(</>),
)
import Path.IO (getTempDir, removeFile)
Expand Down Expand Up @@ -62,8 +64,10 @@ preflightChecks ::
preflightChecks cmd = context "preflight-checks" $ do
-- Check for writing to temp dir
tmpDir <- sendIO getTempDir
fatalOnIOException "Failed to write to temp directory" . sendIO $ TIO.writeFile (fromAbsFile $ tmpDir </> preflightCheckFileName) "Writing to temp dir"
sendIO $ removeFile (tmpDir </> preflightCheckFileName)
fileName <- preflightCheckFileName
let file = tmpDir </> fileName
fatalOnIOException "Failed to write to temp directory" . sendIO $ TIO.writeFile (fromAbsFile file) "Writing to temp dir"
sendIO $ removeFile file

-- Check for valid API Key and if user can connect to fossa app
org <- errHelp InvalidApiKeyErr $ errDoc apiKeyUrl getOrganization
Expand Down Expand Up @@ -127,8 +131,13 @@ fullAccessTokenCheck TokenTypeResponse{..} = case tokenType of
$ fatal TokenTypeErr
_ -> pure ()

preflightCheckFileName :: Path Rel File
preflightCheckFileName = $(mkRelFile "preflight-check.txt")
-- | Generate a unique filename for a preflight check file.
--
-- The filename needs to be unique so that we don't clobber the file when multiple instances of fossa run on the same machine.
preflightCheckFileName :: Has (Lift IO) sig m => m (Path Rel File)
preflightCheckFileName = do
suffix <- sendIO $ UUID.toString <$> UUID.nextRandom
sendIO $ parseRelFile $ "preflight-check-" <> suffix <> ".txt"

data InvalidApiKeyErr = InvalidApiKeyErr
instance ToDiagnostic InvalidApiKeyErr where
Expand Down

0 comments on commit f23a11b

Please sign in to comment.