@@ -13,8 +13,7 @@ import System.Environment (getArgs, getProgName, lookupEnv)
1313-- Third-party library imports
1414import Control.Lens (Identity (runIdentity ), (^?) )
1515import Data.Aeson (FromJSON (parseJSON ), ToJSON ,
16- Value (Object ), decodeStrict , encode ,
17- (.:) )
16+ Value (Object ), decodeStrict , (.:) )
1817import Data.Aeson.Lens (AsNumber (_Integer ), key , nth )
1918import Data.ByteString (ByteString )
2019import qualified Data.ByteString.Char8 as BS
@@ -28,6 +27,7 @@ import System.FilePath (takeDirectory)
2827import Text.Ginger (IncludeResolver , SourcePos , Template ,
2928 ToGVal (.. ), dict , easyRender ,
3029 parseGinger )
30+ import Control.Exception (try , SomeException )
3131
3232-- Data type definitions
3333data MainRelease = MainRelease {
@@ -123,25 +123,36 @@ getMainRelease releaseId = do
123123nullResolver :: IncludeResolver Identity
124124nullResolver = const $ return Nothing
125125
126- -- | This is our template. Because 'parseGinger' wants a monad (as loading
127- -- includes would normally go through some sort of monadic API like 'IO'), we
128- -- use 'Identity' here.
129126getTemplate :: String -> Template SourcePos
130127getTemplate content = either (error . show ) id . runIdentity $
131128 parseGinger nullResolver Nothing content
132129
130+ templatePath :: IO String
131+ templatePath = do
132+ progName <- getProgName
133+ return $ takeDirectory progName ++ " /app/templates/post.md"
134+
135+ runGenAlbumPost :: String -> String -> IO String
136+ runGenAlbumPost artistName albumName = do
137+ -- Get the MainRelease of the album
138+ release <- getMasterReleaseId artistName albumName
139+ >>= getMainReleaseId
140+ >>= getMainRelease
141+ content <- templatePath >>= readFile
142+ return $ T. unpack . easyRender release $ getTemplate content
143+
133144-- Main function
134145main :: IO ()
135146main = do
136147 args <- getArgs
137148 case args of
138- [artistName, albumName] -> do
139- release <- getMasterReleaseId artistName albumName
140- >>= getMainReleaseId
141- >>= getMainRelease
142- putStrLn $ BS. unpack $ BS. toStrict $ encode release
143- content <- getProgName >>= readFile . ( ++ " /app/templates/post.md " ) . takeDirectory
144- let template = getTemplate content
145- let output = T. unpack $ easyRender release template
146- putStrLn output
147- _ -> putStrLn " Usage: pull_album_info <artist_name> <album_name>"
149+ [artistName, albumName, branchName ] -> do
150+ result <- try $ runGenAlbumPost artistName albumName :: IO ( Either SomeException String )
151+ post <- case result of
152+ Left _ -> do
153+ _ <- putStrLn " Cannot get album info from Discog, falling back to default post template "
154+ templatePath >>= readFile
155+ Right output -> return output
156+ writeFile branchName post
157+ putStrLn " done "
158+ _ -> putStrLn " Usage: pull_album_info <artist_name> <album_name> <branch_name> "
0 commit comments