Skip to content

Commit

Permalink
Automatically create a config file if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Sep 25, 2019
1 parent 9fb80e1 commit 7407f87
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.stack-work
.cabal
*.cabal
/docs
/docs-source/images/pattern.jpg
95 changes: 58 additions & 37 deletions tasklite-core/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..))
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion tasklite-core/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tasklite-core/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

0 comments on commit 7407f87

Please sign in to comment.