buuka/app/Main.hs

48 lines
1.6 KiB
Haskell
Raw Normal View History

2021-01-03 00:39:44 +02:00
{-# LANGUAGE TupleSections #-}
2020-12-30 18:35:47 +02:00
module Main where
2020-12-30 23:29:56 +02:00
import Options.Applicative
import Control.Monad.Buuka
(BuukaM, runBuukaM)
import Data.Environment
2020-12-31 08:31:31 +02:00
import UnliftIO.Directory
(XdgDirectory(XdgData), getXdgDirectory)
2021-01-03 00:39:44 +02:00
import Data.Foldable
(asum)
2021-01-02 23:07:10 +02:00
import System.Environment
(lookupEnv)
2020-12-30 23:29:56 +02:00
import qualified Operations
2021-01-03 00:39:44 +02:00
import Data.Query
(Field(..))
2021-10-27 20:46:23 +03:00
import qualified Options.Applicative.NonEmpty as NE
2021-01-03 00:39:44 +02:00
2020-12-30 23:29:56 +02:00
commands :: Parser (BuukaM ())
commands = subparser
2021-01-03 00:39:44 +02:00
( command "insert" (info (insertOpts Operations.insert <**> helper) (progDesc "Insert a new bookmark"))
<> command "list" (info (pure Operations.list <**> helper) (progDesc "List all the bookmarks"))
<> command "query" (info (queryOpts Operations.query <**> helper) (progDesc "Query the bookmarks"))
2021-01-03 22:05:22 +02:00
<> command "import" (info (pure Operations.importFirefox <**> helper) (progDesc "Import"))
2021-01-01 08:29:24 +02:00
)
2020-12-30 23:29:56 +02:00
where
insertOpts f =
f <$> strOption (long "url" <> short 'u' <> metavar "URL")
<*> optional (strOption (long "title"))
2021-01-03 00:39:44 +02:00
queryOpts f =
2021-10-27 20:46:23 +03:00
f <$> NE.some1 (asum [tagged Title "title", tagged Url "url"])
2021-01-03 00:39:44 +02:00
tagged t x = (t, ) <$> strOption (long x <> metavar "REGEX")
2020-12-30 18:35:47 +02:00
main :: IO ()
main = do
2021-01-02 23:07:10 +02:00
env <- Environment <$> (lookupEnv "BUUKA_HOME" >>= maybe defaultHome pure)
execParser (info (commands <**> helper) (fullDesc <> progDesc description)) >>= runBuukaM env
where
defaultHome = getXdgDirectory XdgData "buuka"
description = "Bookmarks manager. Stores the bookmarks in a yaml file under your xdg directory or in a folder specified by the BUUKA_HOME environment variable"