Skip to content

Commit

Permalink
Merge pull request #11 from tchoutri/verbose-mode
Browse files Browse the repository at this point in the history
Implement a verbose mode
  • Loading branch information
tchoutri committed Jun 29, 2024
2 parents 3329eb2 + 4af944d commit be582a2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
18 changes: 10 additions & 8 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Confer.Effect.Symlink

data Options = Options
{ dryRun :: Bool
, verbose :: Bool
, configurationFile :: Maybe OsPath
, cliCommand :: Command
}
Expand Down Expand Up @@ -50,6 +51,7 @@ parseOptions =
Options
<$> switch
(long "dry-run" <> help "Do not perform actual file system operations")
<*> switch (long "verbose" <> help "Make the program more talkative")
<*> optional (option osPathOption (long "deployments-file" <> metavar "FILENAME" <> help "Use the specified the deployments.lua file"))
<*> parseCommand

Expand All @@ -72,23 +74,23 @@ runOptions
)
=> Options
-> Eff es ()
runOptions (Options dryRun configurationFile Check) = do
deployments <- processConfiguration configurationFile
runOptions (Options dryRun verbose configurationFile Check) = do
deployments <- processConfiguration verbose configurationFile
if dryRun
then
Cmd.check deployments
Cmd.check verbose deployments
& runSymlinkPure Map.empty
else
Cmd.check deployments
Cmd.check verbose deployments
& runSymlinkIO
runOptions (Options dryRun configurationFile Deploy) = do
deployments <- processConfiguration configurationFile
runOptions (Options dryRun verbose configurationFile Deploy) = do
deployments <- processConfiguration verbose configurationFile
if dryRun
then
Cmd.deploy deployments
Cmd.deploy verbose deployments
& runSymlinkPure Map.empty
else
Cmd.deploy deployments
Cmd.deploy verbose deployments
& runSymlinkIO

withInfo :: Parser a -> String -> ParserInfo a
Expand Down
4 changes: 3 additions & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ profiling: True

library-profiling: True

semaphore: True
-- semaphore: True

-- multi-repl: True

jobs: $ncpus

Expand Down
20 changes: 4 additions & 16 deletions src/Confer/CLI/Cmd/Check.hs
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
module Confer.CLI.Cmd.Check (check) where

import Control.Monad
import Control.Placeholder
import Data.Foldable
import Data.Function
import Data.List.NonEmpty
import Data.List.NonEmpty qualified as NE
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.Display
import Data.Text.IO qualified as Text
import Data.Vector (Vector)
import Data.Vector qualified as Vector
import Effectful
import Effectful.Error.Static (Error)
import Effectful.Error.Static qualified as Error
import Effectful.FileSystem (FileSystem)
import Effectful.FileSystem qualified as FileSystem
import System.Exit qualified as System
import System.Info qualified as System
import System.OsPath (OsPath)
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 Symlink
Expand All @@ -35,14 +22,15 @@ check
, Symlink :> es
, Error CLIError :> es
)
=> Vector Deployment
=> Bool
-> Vector Deployment
-> Eff es ()
check deployments = do
check verbose deployments = do
result <-
mconcat . Vector.toList <$> do
let facts :: Vector Fact = foldMap (.facts) deployments
forM facts $ \fact -> do
liftIO $ Text.putStrLn $ "[+] Checking " <> display fact
liftIO $ Text.putStrLn $ "Checking " <> display fact
validateSymlink fact
case result of
Failure errors -> do
Expand Down
5 changes: 3 additions & 2 deletions src/Confer/CLI/Cmd/Deploy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ deploy
, Symlink :> es
, IOE :> es
)
=> Vector Deployment
=> Bool
-> Vector Deployment
-> Eff es ()
deploy deployments = do
deploy verbose deployments = do
forM_ deployments $ \d ->
forM_ d.facts $ \fact -> do
filepath <- liftIO $ OsPath.decodeFS fact.destination
Expand Down
14 changes: 8 additions & 6 deletions src/Confer/Config/ConfigFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ processConfiguration
, FileSystem :> es
, Error CLIError :> es
)
=> Maybe OsPath
=> Bool
-> Maybe OsPath
-> Eff es (Vector Deployment)
processConfiguration mConfigurationFilePath = do
processConfiguration verbose mConfigurationFilePath = do
pathToConfigFile <- determineConfigurationFilePath mConfigurationFilePath
loadConfiguration pathToConfigFile >>= \case
loadConfiguration verbose pathToConfigFile >>= \case
Right allDeployments -> do
let currentOS = OS (Text.pack System.os)
let currentArch = Arch (Text.pack System.arch)
currentHost <- Text.pack <$> liftIO getHostName
liftIO $ Text.putStrLn $ "Hostname: " <> currentHost <> " (detected)"
liftIO $ Text.putStrLn $ "OS: " <> display currentOS <> " (detected)"
liftIO $ Text.putStrLn $ "Architecture: " <> display currentArch <> " (detected)"
when verbose $ do
liftIO $ Text.putStrLn $ "Hostname: " <> currentHost <> " (detected)"
liftIO $ Text.putStrLn $ "OS: " <> display currentOS <> " (detected)"
liftIO $ Text.putStrLn $ "Architecture: " <> display currentArch <> " (detected)"
let deployments =
adjustConfiguration
currentHost
Expand Down
17 changes: 12 additions & 5 deletions src/Confer/Config/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Confer.Config.Evaluator
, adjustConfiguration
) where

import Control.Monad (void)
import Control.Monad (void, when)
import Control.Placeholder
import Data.Maybe (isNothing)
import Data.Text (Text)
Expand Down Expand Up @@ -52,22 +52,29 @@ loadConfiguration
:: ( IOE :> es
, FileSystem :> es
)
=> OsPath
=> Bool
-> OsPath
-> Eff es (Either String (Vector Deployment))
loadConfiguration pathToConfigFile = do
loadConfiguration verbose pathToConfigFile = do
userModule <- API.mkUserModule
hostModule <- API.mkHostModule
liftIO $ Lua.run $ do
Lua.openlibs -- load the default Lua packages
conferLuaFilePath <- liftIO $ getDataFileName "runtime/lua/confer.lua"
liftIO $ Text.putStrLn $ "Loading " <> Text.pack conferLuaFilePath
when verbose $
liftIO $
Text.putStrLn $
"Loading " <> Text.pack conferLuaFilePath
Lua.dofile (Just conferLuaFilePath)
Lua.setglobal "confer"
Lua.registerModule Lua.System.documentedModule
Lua.registerModule userModule
Lua.registerModule hostModule
configFilePath <- liftIO $ OsPath.decodeFS pathToConfigFile
liftIO $ Text.putStrLn $ "Loading " <> Text.pack (show configFilePath)
when verbose $
liftIO $
Text.putStrLn $
"Loading " <> Text.pack configFilePath
Lua.dofile (Just configFilePath)
>>= \case Lua.OK -> pure (); _ -> Lua.throwErrorAsException
Lua.resultToEither <$> Lua.runPeeker peekConfig Lua.top
Expand Down

0 comments on commit be582a2

Please sign in to comment.