Skip to content

Commit

Permalink
Implement variable overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
tchoutri committed Jun 29, 2024
1 parent e157d70 commit e3658a9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
19 changes: 15 additions & 4 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Main where

import Data.Function ((&))
import Data.Map.Strict qualified as Map
import Data.Text (Text)
import Data.Version (showVersion)
import Effectful
import Effectful.Error.Static
Expand All @@ -24,6 +25,9 @@ data Options = Options
{ dryRun :: Bool
, verbose :: Bool
, configurationFile :: Maybe OsPath
, mDeploymentArch :: Maybe Text
, mDeploymentOs :: Maybe Text
, mDeploymentHostname :: Maybe Text
, cliCommand :: Command
}
deriving stock (Show, Eq)
Expand Down Expand Up @@ -52,7 +56,14 @@ parseOptions =
<$> 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 deployments.lua file"))
<*> optional
(option osPathOption (long "deployments-file" <> metavar "FILENAME" <> help "Use the specified deployments.lua file"))
<*> optional
(option str (long "arch" <> metavar "ARCH" <> help "Override the detected architecture in the configuration file"))
<*> optional
(option str (long "os" <> metavar "OS" <> help "Override the operating system detected in the configuration file"))
<*> optional
(option str (long "hostname" <> metavar "HOSTNAME" <> help "Override the host name detected in the configuration file"))
<*> parseCommand

parseCommand :: Parser Command
Expand All @@ -74,16 +85,16 @@ runOptions
)
=> Options
-> Eff es ()
runOptions (Options dryRun verbose configurationFile Check) = do
deployments <- processConfiguration verbose configurationFile
runOptions (Options dryRun verbose configurationFile mArch mOs mHostname Check) = do
deployments <- processConfiguration verbose configurationFile mArch mOs mHostname
if dryRun
then
Cmd.check verbose deployments
& runSymlinkPure Map.empty
else
Cmd.check verbose deployments
& runSymlinkIO
runOptions (Options dryRun verbose configurationFile Deploy) = do
runOptions (Options dryRun verbose configurationFile mArch mOs mHostname Deploy) = do
deployments <- processConfiguration verbose configurationFile
if dryRun
then
Expand Down
4 changes: 0 additions & 4 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ documentation: True

tests: True

profiling: True

library-profiling: True

-- semaphore: True

-- multi-repl: True
Expand Down
6 changes: 3 additions & 3 deletions confer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ common ghc-options
-Wno-unticked-promoted-constructors

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

common rts-options
ghc-options: -rtsopts -threaded "-with-rtsopts=-N -T"
Expand Down Expand Up @@ -82,6 +80,7 @@ library
, directory
, effectful
, effectful-core
, extra
, filepath
, hostname
, hslua-aeson
Expand Down Expand Up @@ -117,6 +116,7 @@ executable confer
, effectful-core
, filepath
, optparse-applicative
, text

if flag(development)
build-depends: placeholder
Expand Down
2 changes: 1 addition & 1 deletion doc/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Display the help message.
### Configuration overrides

#### `--arch=<arch>`
Override the detected architecturein the configuration file.
Override the detected architecture in the configuration file.

With `arch` as:

Expand Down
61 changes: 48 additions & 13 deletions src/Confer/Config/ConfigFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import System.OsPath qualified as OsPath
import Confer.CLI.Errors
import Confer.Config.Evaluator
import Confer.Config.Types (Deployment, DeploymentArchitecture (..), DeploymentOS (..))
import Data.Text (Text)

-- | This function looks up the configuration file in the following places
-- (ordered by position):
Expand All @@ -34,18 +35,56 @@ processConfiguration
)
=> Bool
-> Maybe OsPath
-> Maybe DeploymentArchitecture
-> Maybe DeploymentOS
-> Maybe Text
-> Eff es (Vector Deployment)
processConfiguration verbose mConfigurationFilePath = do
processConfiguration verbose mConfigurationFilePath mDeploymentArch mOS mHostname = do
pathToConfigFile <- determineConfigurationFilePath mConfigurationFilePath
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
when verbose $ do
liftIO $ Text.putStrLn $ "Hostname: " <> currentHost <> " (detected)"
liftIO $ Text.putStrLn $ "OS: " <> display currentOS <> " (detected)"
liftIO $ Text.putStrLn $ "Architecture: " <> display currentArch <> " (detected)"
currentOS <- case mOS of
Nothing -> do
let inferredOS = OS (Text.pack System.os)
when verbose $ do
liftIO $ Text.putStrLn $ "OS: " <> display inferredOS <> " (detected)"
pure inferredOS
Just overridenOS -> do
when verbose $
liftIO $
Text.putStrLn $
"OS: " <> display overridenOS <> " (overriden)"
pure overridenOS

currentArch <- case mDeploymentArch of
Nothing -> do
let inferredArch = Arch (Text.pack System.arch)
when verbose $
liftIO $
Text.putStrLn $
"Architecture: " <> display inferredOS <> " (detected)"

Check failure on line 65 in src/Confer/Config/ConfigFile.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

Variable not in scope: inferredOS
pure inferredArch
Just overridenArch -> do
when verbose $
liftIO $
Text.putStrLn $
"Architecture: " <> display inferredOS <> " (overriden)"

Check failure on line 71 in src/Confer/Config/ConfigFile.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

Variable not in scope: inferredOS
pure overridenArch

currentHost <- case mHostname of
Nothing -> do
when verbose $
liftIO $
Text.putStrLn $
"Hostname: " <> display inferredOS <> " (detected)"

Check failure on line 79 in src/Confer/Config/ConfigFile.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

Variable not in scope: inferredOS
Text.pack <$> liftIO getHostName
Just overridenHostname -> do
when verbose $
liftIO $
Text.putStrLn $
"Hostname: " <> display inferredOS <> " (overriden)"

Check failure on line 85 in src/Confer/Config/ConfigFile.hs

View workflow job for this annotation

GitHub Actions / 9.8.2 on ubuntu-latest

Variable not in scope: inferredOS
pure overridenHostname

let deployments =
adjustConfiguration
currentHost
Expand All @@ -63,7 +102,7 @@ determineConfigurationFilePath
=> Maybe OsPath
-> Eff es OsPath
determineConfigurationFilePath mCLIConfigFilePath =
case checkCLIOptions mCLIConfigFilePath of
case mCLIConfigFilePath of
Just osPath -> do
filePath <- liftIO $ OsPath.decodeFS osPath
FileSystem.doesFileExist filePath
Expand All @@ -78,7 +117,3 @@ determineConfigurationFilePath mCLIConfigFilePath =
True ->
FileSystem.makeAbsolute "deployments.lua"
>>= (liftIO . OsPath.encodeFS)

checkCLIOptions :: Maybe OsPath -> Maybe OsPath
checkCLIOptions Nothing = Nothing
checkCLIOptions (Just osPath) = Just osPath

0 comments on commit e3658a9

Please sign in to comment.