module Vimeta.UI.CommandLine.Config
( Options,
optionsParser,
run,
)
where
import qualified Byline.Exit as B
import Options.Applicative
import Vimeta.Core
data Options = Options
{ optsKey :: Maybe Text,
optsTagger :: Tagger
}
optionsParser :: Parser Options
optionsParser =
Options
<$> optional
( strOption $
mconcat
[ short 'k',
long "key",
metavar "KEY",
help "Set the API key to KEY"
]
)
<*> pure AtomicParsley
run :: Options -> IO ()
run opts = do
let def = defaultConfig (optsTagger opts)
config = case optsKey opts of
Nothing -> def
Just k -> def {configTMDBKey = k}
result <- runExceptT (app opts config)
case result of
Left e -> B.die (B.text $ toText e)
Right (Just msg) -> B.warn (B.text $ toText msg)
Right Nothing -> return ()
app :: Options -> Config -> ExceptT String IO (Maybe String)
app opts config = do
filename <- writeConfig config
return $ case optsKey opts of
Just _ -> Nothing
Nothing -> Just (missingKey filename)
where
missingKey = ("please edit the config file and set the API key: " ++)