Skip to content
Permalink
Browse files

Automatically create a config file if it doesn't exist

  • Loading branch information...
ad-si committed Sep 25, 2019
1 parent 9fb80e1 commit 7407f87284d22ef4a3dfafae7e998ce2f3e9de87
Showing with 68 additions and 39 deletions.
  1. +1 −1 .gitignore
  2. +58 −37 tasklite-core/app/Main.hs
  3. +5 −1 tasklite-core/example-config.yaml
  4. +4 −0 tasklite-core/package.yaml
@@ -1,4 +1,4 @@
.stack-work
.cabal
*.cabal
/docs
/docs-source/images/pattern.jpg
@@ -17,7 +17,13 @@ import Data.Version (showVersion)
import Data.Yaml (decodeFileEither, prettyPrintParseException)
import GHC.IO.Encoding (setLocaleEncoding, utf8)
import Options.Applicative
import System.Directory (XdgDirectory(..), getXdgDirectory, getHomeDirectory)
import Paths_tasklite_core
import System.Directory
( copyFile
, getHomeDirectory
, getXdgDirectory
, XdgDirectory(..)
)
import System.FilePath ((</>))
import Time.System
import Database.SQLite.Simple (close, Connection(..))
@@ -786,52 +792,67 @@ executeCLiCommand conf now connection cmd =
UlidToUtc ulid -> pure $ prettyUlid ulid


main :: IO ()
main = do
-- Necessary for Docker image
setLocaleEncoding utf8
printOutput :: [Char] -> Config -> IO ()
printOutput appName configUser = do
configUserNorm <-
if (dataDir configUser /= "")
then pure $ configUser
else do
xdgDataDir <- getXdgDirectory XdgData appName
pure $ configUser {dataDir = xdgDataDir}

let appName = "tasklite"
config <- case (T.stripPrefix "~/" $ T.pack $ dataDir configUserNorm) of
Nothing ->
pure $ configUser {dataDir = dataDir configUserNorm}
Just rest -> do
homeDir <- getHomeDirectory
pure $ configUser { dataDir = homeDir </> T.unpack rest }

configDirectory <- getXdgDirectory XdgConfig appName
let configPath = configDirectory </> "config.yaml"
cliCommand <- execParser $ commandParserInfo config

connection <- setupConnection config
-- TODO: Integrate into migrations
tableStatus <- createTables config connection
migrationsStatus <- runMigrations config connection
nowElapsed <- timeCurrentP

configUserEither <- decodeFileEither configPath
let
now = timeFromElapsedP nowElapsed :: DateTime

case configUserEither of
Left error -> die $ T.pack $ prettyPrintParseException error
Right configUser -> do
configUserNorm <-
if (dataDir configUser /= "")
then pure $ configUser
else do
xdgDataDir <- getXdgDirectory XdgData appName
pure $ configUser {dataDir = xdgDataDir}
doc <- executeCLiCommand config now connection cliCommand

config <- case (T.stripPrefix "~/" $ T.pack $ dataDir configUserNorm) of
Nothing ->
pure $ configUser {dataDir = dataDir configUserNorm}
Just rest -> do
homeDir <- getHomeDirectory
pure $ configUser { dataDir = homeDir </> T.unpack rest }
-- TODO: Use withConnection instead
close connection

cliCommand <- execParser $ commandParserInfo config
-- TODO: Remove color when piping into other command
putDoc $ tableStatus <> migrationsStatus <> doc <> hardline

connection <- setupConnection config
-- TODO: Integrate into migrations
tableStatus <- createTables config connection
migrationsStatus <- runMigrations config connection
nowElapsed <- timeCurrentP

let
now = timeFromElapsedP nowElapsed :: DateTime
main :: IO ()
main = do
-- Necessary for Docker image
setLocaleEncoding utf8

let appName = "tasklite"

configDirectory <- getXdgDirectory XdgConfig appName
let configPath = configDirectory </> "config.yaml"

doc <- executeCLiCommand config now connection cliCommand
configResult <- decodeFileEither configPath

-- TODO: Use withConnection instead
close connection
case configResult of
Left error -> do
if "not found" `T.isInfixOf` (T.pack $ prettyPrintParseException error)
then do
exampleConfigPath <- getDataFileName "example-config.yaml"
copyFile exampleConfigPath configPath
configResult2 <- decodeFileEither configPath

-- TODO: Remove color when piping into other command
putDoc $ tableStatus <> migrationsStatus <> doc <> hardline
case configResult2 of
Left error2 -> die $ T.pack $ prettyPrintParseException error2
Right configUser -> printOutput appName configUser
else
die $ T.pack $ prettyPrintParseException error

Right configUser ->
printOutput appName configUser
@@ -10,7 +10,11 @@ dueStyle: yellow
overdueStyle: red
tagStyle: blue
utcFormat: YYYY-MM-DD H:MI:S
dataDir: /root/.local/share/tasklite

#| Optional, uses the XDG directory per default
#| https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
# dataDir: /custom/path/tasklite

dbName: main.db
dateWidth: 10
bodyWidth: 10
@@ -10,6 +10,9 @@ extra-source-files:
- README.md
- ChangeLog.md

data-files:
- example-config.yaml

synopsis: CLI task / todo list manager with SQLite backend
category: Cli

@@ -58,6 +61,7 @@ default-extensions:
- NoImplicitPrelude
- OverloadedStrings
- RecordWildCards
- ScopedTypeVariables
- StandaloneDeriving
- TypeApplications
- TypeFamilies

0 comments on commit 7407f87

Please sign in to comment.
You can’t perform that action at this time.