Skip to content
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

Create error codes for confer #10

Merged
merged 1 commit into from
Jun 29, 2024
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
11 changes: 8 additions & 3 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ documentation: True

tests: True

-- package confer
-- ghc-options: -fhpc
profiling: True

library-profiling: True

semaphore: True

jobs: $ncpus

package *
ghc-options: +RTS -A32m -RTS -j -haddock
ghc-options: +RTS -A32m -RTS -haddock

allow-newer:
hslua-aeson:unordered-containers
Expand Down
4 changes: 3 additions & 1 deletion confer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ common ghc-options
-Wno-unticked-promoted-constructors

if flag(development)
ghc-options: -finfo-table-map -Wno-unused-imports -Wno-unused-packages
ghc-options:
-finfo-table-map -Wno-unused-imports -Wno-unused-packages -prof
-fprof-auto -with-rtsopts=-p

common rts-options
ghc-options: -rtsopts -threaded "-with-rtsopts=-N -T"
Expand Down
5 changes: 1 addition & 4 deletions src/Confer/CLI/Cmd/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import System.OsPath qualified as OsPath
import Validation

import Confer.CLI.Errors (CLIError (..))
import Confer.CLI.Errors qualified as Errors
import Confer.Config.Evaluator
import Confer.Config.Types
import Confer.Effect.Symlink (Symlink, SymlinkError (..))
import Confer.Effect.Symlink qualified as Errors
import Confer.Effect.Symlink qualified as Symlink

check
Expand All @@ -46,9 +46,6 @@ check deployments = do
validateSymlink fact
case result of
Failure errors -> do
forM_ errors $
\e ->
liftIO $ Text.putStrLn $ Errors.formatSymlinkError e
Error.throwError (SymlinkErrors errors)
Success _ -> pure ()

Expand Down
53 changes: 46 additions & 7 deletions src/Confer/CLI/Errors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Confer.Effect.Symlink (SymlinkError (..))
import Confer.Effect.Symlink qualified as Symlink
import Control.Monad.IO.Class (liftIO)
import Data.Foldable
import Data.Word (Word8)

data CLIError
= NoDefaultConfigurationFile
Expand All @@ -22,15 +23,35 @@ data CLIError
| SymlinkErrors (NonEmpty SymlinkError)
deriving stock (Eq, Show)

newtype ErrorCode = ErrorCode Word8
deriving newtype (Eq, Show, Ord)

instance Display ErrorCode where
displayBuilder (ErrorCode c) = "[CONFER-" <> displayBuilder c <> "]"

cliErrorToCode :: CLIError -> ErrorCode
cliErrorToCode = \case
NoDefaultConfigurationFile -> ErrorCode 156
NoUserProvidedConfigurationFile{} -> ErrorCode 169
NoDeploymentsAvailable{} -> ErrorCode 123
SymlinkErrors{} -> ErrorCode 192

symlinkErrorToCode :: SymlinkError -> ErrorCode
symlinkErrorToCode = \case
DoesNotExist{} -> ErrorCode 234
IsNotSymlink{} -> ErrorCode 142
WrongTarget{} -> ErrorCode 102

reportError :: CLIError -> IO ()
reportError NoDefaultConfigurationFile =
System.die "[!] Could not find configuration file at ./deployments.lua"
reportError (NoUserProvidedConfigurationFile osPath) = do
System.die $ Text.unpack $ display (cliErrorToCode NoDefaultConfigurationFile) <> " Could not find configuration file at ./deployments.lua"
reportError e@(NoUserProvidedConfigurationFile osPath) = do
filePath <- OsPath.decodeFS osPath
System.die $ "[!] Could not find configuration file at" <> filePath
reportError (NoDeploymentsAvailable os arch hostname) = do
System.die $ Text.unpack $ display (cliErrorToCode e) <> " Could not find configuration file at" <> Text.pack filePath
reportError e@(NoDeploymentsAvailable os arch hostname) = do
let message =
"[!] Could not find deployments to run on "
display (cliErrorToCode e)
<> " Could not find deployments to run on "
<> display arch
<> "-"
<> display os
Expand All @@ -39,6 +60,24 @@ reportError (NoDeploymentsAvailable os arch hostname) = do
System.die $ Text.unpack message
reportError (SymlinkErrors errors) = do
forM_ errors $
\e ->
liftIO $ Text.putStrLn $ Symlink.formatSymlinkError e
\err ->
liftIO $ Text.putStrLn $ formatSymlinkError err
System.exitFailure

formatSymlinkError :: SymlinkError -> Text
formatSymlinkError e@(DoesNotExist path) =
display (symlinkErrorToCode e)
<> " "
<> display (Text.pack . show $ path)
<> " does not exist"
formatSymlinkError e@(IsNotSymlink path) =
display (symlinkErrorToCode e)
<> display (Text.pack . show $ path)
<> " is not a symbolic link"
formatSymlinkError e@(WrongTarget linkPath expectedTarget actualTarget) =
display (symlinkErrorToCode e)
<> display (Text.pack . show $ linkPath)
<> " points to "
<> display (Text.pack . show $ actualTarget)
<> " instead of pointing to "
<> display (Text.pack . show $ expectedTarget)
18 changes: 0 additions & 18 deletions src/Confer/Effect/Symlink.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@
, runSymlinkPure
, Symlink (..)
, SymlinkError (..)
, formatSymlinkError
) where

import Control.Exception
import Control.Monad
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Text (Text)

Check warning on line 15 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on macos-latest

The import of ‘Data.Text’ is redundant

Check warning on line 15 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

The import of ‘Data.Text’ is redundant

Check warning on line 15 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / Build statically linked using alpine

The import of ‘Data.Text’ is redundant
import Data.Text qualified as Text

Check warning on line 16 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on macos-latest

The qualified import of ‘Data.Text’ is redundant

Check warning on line 16 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

The qualified import of ‘Data.Text’ is redundant

Check warning on line 16 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / Build statically linked using alpine

The qualified import of ‘Data.Text’ is redundant
import Data.Text.Display

Check warning on line 17 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on macos-latest

The import of ‘Data.Text.Display’ is redundant

Check warning on line 17 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

The import of ‘Data.Text.Display’ is redundant

Check warning on line 17 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / Build statically linked using alpine

The import of ‘Data.Text.Display’ is redundant
import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.Error.Static (Error)

Check warning on line 20 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on macos-latest

The import of ‘Effectful.Error.Static’ is redundant

Check warning on line 20 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

The import of ‘Effectful.Error.Static’ is redundant

Check warning on line 20 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / Build statically linked using alpine

The import of ‘Effectful.Error.Static’ is redundant
import Effectful.Error.Static qualified as Error
import Effectful.FileSystem
import Effectful.FileSystem qualified as FileSystem
import Effectful.State.Static.Local qualified as State
import System.Directory qualified as Directory

Check warning on line 25 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on macos-latest

The qualified import of ‘System.Directory’ is redundant

Check warning on line 25 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

The qualified import of ‘System.Directory’ is redundant

Check warning on line 25 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / Build statically linked using alpine

The qualified import of ‘System.Directory’ is redundant
import System.Directory.Internal (FileType (..), OsPath)
import System.Directory.Internal qualified as Directory
import System.IO.Error
Expand Down Expand Up @@ -74,7 +73,7 @@
sourceFilePath <- liftIO $ OsPath.decodeFS source
sourcePath <- FileSystem.makeAbsolute sourceFilePath
destinationPath <- liftIO $ OsPath.decodeFS destination
case sourceType of

Check warning on line 76 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on macos-latest

Pattern match(es) are non-exhaustive

Check warning on line 76 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

Pattern match(es) are non-exhaustive

Check warning on line 76 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / Build statically linked using alpine

Pattern match(es) are non-exhaustive
File ->
createFileLink sourcePath destinationPath
Directory ->
Expand All @@ -84,7 +83,7 @@
sourceType <- liftIO $ do
metadata <- Directory.getFileMetadata linkOsPath
pure $ Directory.fileTypeFromMetadata metadata
case sourceType of

Check warning on line 86 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on macos-latest

Pattern match(es) are non-exhaustive

Check warning on line 86 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

Pattern match(es) are non-exhaustive

Check warning on line 86 in src/Confer/Effect/Symlink.hs

View workflow job for this annotation

GitHub Actions / Build statically linked using alpine

Pattern match(es) are non-exhaustive
File ->
FileSystem.removeFile linkFilePath
Directory ->
Expand Down Expand Up @@ -140,20 +139,3 @@
actualLinkTarget
)
Nothing -> pure $ Left (DoesNotExist linkPath)

formatSymlinkError :: SymlinkError -> Text
formatSymlinkError (DoesNotExist path) =
"[!] "
<> display (Text.pack . show $ path)
<> " does not exist"
formatSymlinkError (IsNotSymlink path) =
"[!] "
<> display (Text.pack . show $ path)
<> " is not a symbolic link"
formatSymlinkError (WrongTarget linkPath expectedTarget actualTarget) =
"[!] "
<> display (Text.pack . show $ linkPath)
<> " points to "
<> display (Text.pack . show $ actualTarget)
<> " instead of pointing to "
<> display (Text.pack . show $ expectedTarget)
Loading