-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
[#48] Add configurations, read from the file #59
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
{-# LANGUAGE ApplicativeDo #-} | ||
{-# LANGUAGE QuasiQuotes #-} | ||
{-# LANGUAGE TupleSections #-} | ||
|
||
-- | This module contains functions and data types to parse CLI inputs. | ||
|
||
module Summoner.CLI | ||
( summon | ||
) where | ||
|
||
import Data.Foldable (fold) | ||
import Data.Foldable (fold, for_) | ||
import Data.Semigroup (Semigroup (..)) | ||
import Data.Text (Text) | ||
import NeatInterpolation (text) | ||
import Options.Applicative (Parser, ParserInfo, command, execParser, flag, fullDesc, help, helper, | ||
info, infoFooter, infoHeader, long, metavar, optional, progDesc, short, | ||
strArgument, strOption, subparser) | ||
import Options.Applicative.Help.Chunk (stringChunk) | ||
import System.Directory (doesFileExist, getHomeDirectory) | ||
import System.FilePath ((</>)) | ||
import System.Directory (doesFileExist) | ||
import System.Exit (exitFailure) | ||
|
||
import Summoner.Ansi (boldText) | ||
import Summoner.Ansi (boldText, errorMessage, infoMessage, warningMessage) | ||
import Summoner.Config (ConfigP (..), PartialConfig, defaultConfig, finalise, loadFileConfig) | ||
import Summoner.Default (endLine) | ||
import Summoner.Default (defaultConfigFile, endLine) | ||
import Summoner.Project (generateProject) | ||
import Summoner.ProjectData (Decision (..)) | ||
import Summoner.Validation (Validation (..)) | ||
|
@@ -37,20 +38,30 @@ summon = execParser prsr >>= runWithOptions | |
-- | Run 'hs-init' with cli options | ||
runWithOptions :: InitOpts -> IO () | ||
runWithOptions (InitOpts projectName cliConfig maybeFile) = do | ||
file <- case maybeFile of | ||
Nothing -> (</> "summoner.toml") <$> getHomeDirectory | ||
Just x -> pure x | ||
(isDefault, file) <- case maybeFile of | ||
Nothing -> (True,) <$> defaultConfigFile | ||
Just x -> pure (False, x) | ||
isFile <- doesFileExist file | ||
fileConfig <- | ||
if isFile | ||
then loadFileConfig file | ||
else pure mempty | ||
then do | ||
infoMessage $ "Configurations from " <> T.pack file <> " will be used." | ||
loadFileConfig file | ||
else if isDefault | ||
then do | ||
warningMessage "Default config file is missing." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can add path |
||
pure mempty | ||
else do | ||
errorMessage $ "Specified configuration file " <> T.pack file <> " is not found." | ||
exitFailure | ||
-- union all possible configs | ||
let unionConfig = defaultConfig <> fileConfig <> cliConfig | ||
-- get the final config | ||
let finalConfig = case finalise unionConfig of | ||
Failure msgs -> error $ T.unpack $ T.intercalate "\n" msgs | ||
Success c -> c | ||
finalConfig <- case finalise unionConfig of | ||
Failure msgs -> do | ||
for_ msgs $ \msg -> errorMessage msg | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can eta-reduce lambda here |
||
exitFailure | ||
Success c -> pure c | ||
-- Generate the project. | ||
generateProject projectName finalConfig | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ import Data.Text (Text) | |
import System.Directory (doesPathExist, getCurrentDirectory) | ||
import System.FilePath ((</>)) | ||
|
||
import Summoner.Ansi (boldDefault, errorMessage, prompt, putStrFlush) | ||
import Summoner.Ansi (boldDefault, errorMessage, prompt, putStrFlush, warningMessage) | ||
|
||
import qualified Data.Text as T | ||
import qualified Data.Text.IO as T | ||
|
@@ -63,9 +63,8 @@ queryDef question defAnswer = do | |
if | T.null answer -> pure defAnswer | ||
| otherwise -> pure answer | ||
|
||
queryManyRepeatOnFail :: forall a . (Text -> Maybe a) -> Text -> IO [a] | ||
queryManyRepeatOnFail parser question = do | ||
T.putStrLn question | ||
queryManyRepeatOnFail :: forall a . (Text -> Maybe a) -> IO [a] | ||
queryManyRepeatOnFail parser = do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need |
||
promptLoop | ||
where | ||
promptLoop :: IO [a] | ||
|
@@ -97,7 +96,7 @@ checkUniqueName nm = do | |
curPath <- getCurrentDirectory | ||
exist <- doesPathExist $ curPath </> T.unpack nm | ||
if exist then do | ||
errorMessage "Project with this name is already exist. Please choose another one" | ||
warningMessage "Project with this name is already exist. Please choose another one" | ||
newNm <- query "Project name: " | ||
checkUniqueName newNm | ||
else | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra space here..