diff options
author | Benjamin Barenblat <benjamin@barenblat.name> | 2012-12-26 21:56:42 -0600 |
---|---|---|
committer | Benjamin Barenblat <benjamin@barenblat.name> | 2013-01-28 15:03:56 -0500 |
commit | f9c68cad1273d6d7b2373600b8d6d11080b7863a (patch) | |
tree | 0ac6144d129c441cb93522cfcfa8ef4037a494ab /Main.hs |
Diffstat (limited to 'Main.hs')
-rw-r--r-- | Main.hs | 58 |
1 files changed, 58 insertions, 0 deletions
@@ -0,0 +1,58 @@ +{- Main.hs -- main entry point +Copyright (C) 2012 Benjamin Barenblat <benjamin@barenblat.name> + +This module is a part of ageOf. + +ageOf is free software: you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +ageOf is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +ageOf. If not, see <http://www.gnu.org/licenses/>. -} + +module Main where + +import Control.Monad (foldM) +import System.Environment (getArgs, getProgName) +import System.Exit (ExitCode(..), exitWith) +import Text.Printf (printf) + +import RequestMethod (RequestMethod, getAge) +import DBpedia (dbpedia) +import SamplePlugin (samplePlugin) + +-- The plugins we're going to query, in order of preference. +plugins :: [RequestMethod] +plugins = [ samplePlugin + , dbpedia ] + +main :: IO () +main = getArgs >>= go >>= exitWith + +go :: [String] -> IO ExitCode +go (name : []) = do + age <- getAgeWith plugins name + case age of + Nothing -> return $ ExitFailure 1 + Just n -> print n >> return ExitSuccess +go _ = usage >> return (ExitFailure 1) + +{- The workhorse function. Given a list of 'RequestMethod's, 'getAgeWith' +tries to look up 'name' in each one. As soon as a match succeeds, it returns +the age. -} +getAgeWith :: [RequestMethod] -> String -> IO (Maybe Integer) +getAgeWith methods name = + foldM maybeTryNextMethod Nothing methods + where maybeTryNextMethod :: Maybe Integer -> RequestMethod -> IO (Maybe Integer) + maybeTryNextMethod v@(Just _) _ = return v + maybeTryNextMethod Nothing method = getAge method name + +usage :: IO () +usage = do + programName <- getProgName + printf "usage: %s <name>\n" programName |