Skip to content

Commit e5287d2

Browse files
committed
Add support for importing YAML files
1 parent f9c33bf commit e5287d2

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

tasklite-core/source/Cli.hs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ import ImportExport (
161161
importEml,
162162
importFile,
163163
importJson,
164+
importYaml,
164165
ingestDir,
165166
ingestFile,
166167
)
@@ -297,6 +298,7 @@ data Command
297298
| ImportFile FilePath
298299
| ImportDir FilePath
299300
| ImportJson
301+
| ImportYaml
300302
| ImportEml
301303
| IngestFile FilePath
302304
| IngestDir FilePath
@@ -840,26 +842,29 @@ commandParser conf =
840842

841843
<> command "import" (toParserInfo (ImportFile <$> strArgument
842844
(metavar "FILEPATH" <> help "Path to import file"))
843-
"Import a .json or .eml file containing one task")
845+
"Import a .json, .yaml, or .eml file containing one task")
844846

845847
<> command "importdir" (toParserInfo (ImportDir <$> strArgument
846848
(metavar "DIRECTORY_PATH" <> help "Path to directory"))
847-
"Import all .json and .eml files in a directory")
849+
"Import all .json, .yaml, and .eml files in a directory")
848850

849851
<> command "importjson" (toParserInfo (pure ImportJson)
850852
"Import one JSON object from stdin")
851853

854+
<> command "importyaml" (toParserInfo (pure ImportYaml)
855+
"Import one YAML object from stdin")
856+
852857
<> command "importeml" (toParserInfo (pure ImportEml)
853858
"Import one email from stdin")
854859

855860
<> command "ingest" (toParserInfo (IngestFile <$> strArgument
856861
(metavar "FILEPATH" <> help "Path to file"))
857-
("Ingest a .json or .eml file containing one task "
862+
("Ingest a .json, .yaml, or .eml file containing one task "
858863
<> "(import, open in editor, delete the original file)"))
859864

860865
<> command "ingestdir" (toParserInfo (IngestDir <$> strArgument
861866
(metavar "DIRECTORY_PATH" <> help "Path to directory"))
862-
"Ingest all .json and .eml files in a directory")
867+
"Ingest all .json, .yaml, and .eml files in a directory")
863868

864869
<> command "csv" (toParserInfo (pure Csv)
865870
"Show tasks in CSV format")
@@ -1220,6 +1225,7 @@ executeCLiCommand conf now connection progName args availableLinesMb = do
12201225
ImportFile filePath -> importFile conf connection filePath
12211226
ImportDir filePath -> importDir conf connection filePath
12221227
ImportJson -> importJson conf connection
1228+
ImportYaml -> importYaml conf connection
12231229
ImportEml -> importEml conf connection
12241230
IngestFile filePath -> ingestFile conf connection filePath
12251231
IngestDir filePath -> ingestDir conf connection filePath

tasklite-core/source/ImportExport.hs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ importJson _ connection = do
179179
insertImportTask connection importTaskNorm
180180

181181

182+
decodeAndInsertYaml :: Connection -> BSL.LazyByteString -> IO (Doc AnsiStyle)
183+
decodeAndInsertYaml conn content = do
184+
case content & BSL.toStrict & Yaml.decodeEither' of
185+
Left error ->
186+
die $ T.pack $ Yaml.prettyPrintParseException error
187+
Right importTaskRec -> do
188+
importTaskNorm <- importTaskRec & setMissingFields
189+
insertImportTask conn importTaskNorm
190+
191+
192+
importYaml :: Config -> Connection -> IO (Doc AnsiStyle)
193+
importYaml _ conn = do
194+
content <- BSL.getContents
195+
decodeAndInsertYaml conn content
196+
197+
182198
importEml :: Config -> Connection -> IO (Doc AnsiStyle)
183199
importEml _ connection = do
184200
content <- BSL.getContents
@@ -301,6 +317,8 @@ importFile _ conn filePath = do
301317
Right importTaskRec -> do
302318
importTaskNorm <- importTaskRec & setMissingFields
303319
insertImportTask conn importTaskNorm
320+
".yaml" -> decodeAndInsertYaml conn content
321+
".yml" -> decodeAndInsertYaml conn content
304322
".eml" ->
305323
case Parsec.parse Email.message filePath content of
306324
Left error -> die $ show error
@@ -314,6 +332,8 @@ importFile _ conn filePath = do
314332
filterImportable :: FilePath -> Bool
315333
filterImportable filePath =
316334
(".json" `isExtensionOf` filePath)
335+
|| (".yaml" `isExtensionOf` filePath)
336+
|| (".yml" `isExtensionOf` filePath)
317337
|| (".eml" `isExtensionOf` filePath)
318338

319339

@@ -330,6 +350,22 @@ importDir conf connection dirPath = do
330350

331351
ingestFile :: Config -> Connection -> FilePath -> IO (Doc AnsiStyle)
332352
ingestFile conf connection filePath = do
353+
let ingestYaml content = do
354+
let decodeResult = Yaml.decodeEither' (BSL.toStrict content)
355+
case decodeResult of
356+
Left error ->
357+
die $ T.pack $ Yaml.prettyPrintParseException error
358+
Right importTaskRec -> do
359+
importTaskNorm <- importTaskRec & setMissingFields
360+
sequence
361+
[ insertImportTask connection importTaskNorm
362+
, editTaskByTask
363+
conf
364+
OpenEditor
365+
connection
366+
importTaskNorm.task
367+
]
368+
333369
catchAll
334370
( do
335371
content <- BSL.readFile filePath
@@ -349,6 +385,8 @@ ingestFile conf connection filePath = do
349385
connection
350386
importTaskNorm.task
351387
]
388+
".yaml" -> ingestYaml content
389+
".yml" -> ingestYaml content
352390
".eml" ->
353391
case Parsec.parse Email.message filePath content of
354392
Left error -> die $ show error

0 commit comments

Comments
 (0)