Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
smallhadroncollider committed Apr 19, 2020
2 parents 3545249 + fbb3b0f commit 4c99842
Show file tree
Hide file tree
Showing 23 changed files with 221 additions and 76 deletions.
2 changes: 0 additions & 2 deletions .bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ stack build --work-dir "$workDir" --docker --ghc-options -O3
LINUX_FULL_PATH=$(find "$workDir" -path "*linux*" -and -path "*bin/brok")
LINUX_PATH=${LINUX_FULL_PATH%"brok"}

strip "$LINUX_FULL_PATH" # remove tokens

tar -czvf "releases/$1/brok-$1_x86-64-linux.tar.gz" --directory="$LINUX_PATH" "brok"

mkdir -p "releases/$1/brok/DEBIAN"
Expand Down
25 changes: 25 additions & 0 deletions .cmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# input parts
{
"Type" = [
"feat",
"fix",
"docs",
"style",
"refactor",
"test",
"chore"
]
"Body" = !@
}

# predefined messages
{
r = "chore: updates Stack resolver"
vb = "chore: version bump"
readme = "docs: updates readme"
}

# output format
${Type}: ${*}

${Body}
30 changes: 30 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Use new container infrastructure to enable caching
sudo: false

# Do not choose a language; we provide our own build tools.
language: generic

# Caching so the next build will be fast too.
cache:
directories:
- $HOME/.stack

# Ensure necessary system libraries are present
addons:
apt:
packages:
- libgmp-dev

before_install:
# Download and unpack the stack executable
- mkdir -p ~/.local/bin
- export PATH=$HOME/.local/bin:$PATH
- travis_retry curl -L https://www.stackage.org/stack/linux-x86_64 | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'

install:
# Build dependencies
- stack --no-terminal --install-ghc test --only-dependencies

script:
# Build the package, its tests, and its docs and run the tests
- stack --no-terminal test
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ If you want to avoid creating the `.brokdb` file or ignore the cache entirely yo
brok --no-cache test.md links.tex
```

#### Check Certificates

Most browsers will display a website even if it has certificate issues (such as an incomplete certificate chain). By default Brök will not check certificates, so replicate this behaviour.

If you would like to enforce certificate checking, you can turn this on:

```bash
brok --check-certs test.md
```

Any sites with certificate issues will then return a `Could not connect` error.

#### Ignore URLs

You can tell brök to ignore URLs with specified prefixes:
Expand Down
5 changes: 4 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: brok
version: 0.2.0.0
version: 1.0.0
github: "smallhadroncollider/brok"
license: BSD3
author: "Small Hadron Collider"
Expand Down Expand Up @@ -33,9 +33,12 @@ library:
dependencies:
- ansi-terminal
- attoparsec
- connection
- directory
- http-conduit
- http-client
- http-client-tls
- template-haskell
- text
- time

Expand Down
30 changes: 20 additions & 10 deletions src/Brok.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ import Data.FileEmbed (embedFile)
import Data.Text.IO (hPutStrLn)
import System.Exit (exitFailure, exitSuccess)

import Data.Version (showVersion)
import Language.Haskell.TH.Syntax (liftString)
import qualified Paths_brok (version)

import Brok.IO.CLI (header, replace)
import Brok.IO.DB (getCached, setCached)
import Brok.IO.Document (readContent)
import Brok.IO.Http (check)
import Brok.IO.Http (check, mkManager)
import Brok.IO.Output (output)
import Brok.Options (parse)
import Brok.Parser.Links (links)
import Brok.Types.App (App)
import qualified Brok.Types.Config as C (files, ignore, interval, onlyFailures)
import Brok.Types.Brok (Brok, appConfig, mkApp)
import qualified Brok.Types.Config as C (checkCerts, files, ignore, interval, onlyFailures)
import Brok.Types.Link (getURL, isSuccess)
import Brok.Types.Next (Next (..))
import Brok.Types.Result (cachedLinks, ignoredLinks, justLinks, linkIOMap, parseLinks,
pathToResult)

go :: App ()
go :: Brok ()
go = do
config <- ask
config <- asks appConfig
-- read files
content <- traverse (readContent . pathToResult) (C.files config)
-- find links in each file
Expand All @@ -53,17 +57,23 @@ go = do
then void exitFailure
else void exitSuccess

showHelp :: IO ()
showHelp = putStr $ decodeUtf8 $(embedFile "template/usage.txt")
putHelp :: IO ()
putHelp = putStr $ decodeUtf8 $(embedFile "template/usage.txt")

putVersion :: IO ()
putVersion = putStrLn $ "brök " <> $(liftString $ showVersion Paths_brok.version)

-- entry point
brok :: IO ()
brok = do
config <- parse <$> getArgs
case config of
Right (Continue cnf) -> runReaderT go cnf
Right Help -> showHelp
Right (Continue cnf) -> do
manager <- mkManager (C.checkCerts cnf)
runReaderT go (mkApp cnf manager)
Right Help -> putHelp
Right Version -> putVersion
Left _ -> do
hPutStrLn stderr "Invalid format"
showHelp
putHelp
void exitFailure
30 changes: 15 additions & 15 deletions src/Brok/IO/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,76 @@ module Brok.IO.CLI where

import ClassyPrelude

import Brok.Types.App (App)
import Brok.Types.Config (noColor)
import Brok.Types.Brok (Brok, appConfig)
import Data.Text.IO (hPutStr, hPutStrLn)
import System.Console.ANSI (Color (Blue, Green, Magenta, Red, Yellow), ColorIntensity (Dull),
ConsoleLayer (Foreground), SGR (Reset, SetColor), hClearLine,
hCursorUpLine, hSetSGR)

setSGR :: Handle -> [SGR] -> App ()
setSGR :: Handle -> [SGR] -> Brok ()
setSGR hndl settings = do
colourize <- not <$> asks noColor
colourize <- not . noColor <$> asks appConfig
when colourize $ lift (hSetSGR hndl settings)

blank :: App ()
blank :: Brok ()
blank = putStrLn ""

message :: Text -> App ()
message :: Text -> Brok ()
message msg = do
setSGR stdout [SetColor Foreground Dull Blue]
putStrLn msg
setSGR stdout [Reset]

mehssage :: Text -> App ()
mehssage :: Text -> Brok ()
mehssage msg = do
setSGR stdout [SetColor Foreground Dull Yellow]
putStrLn msg
setSGR stdout [Reset]

header :: Text -> App ()
header :: Text -> Brok ()
header msg = do
setSGR stdout [SetColor Foreground Dull Magenta]
putStrLn $ "*** " ++ msg ++ " ***"
setSGR stdout [Reset]

successMessage :: Text -> App ()
successMessage :: Text -> Brok ()
successMessage msg = do
setSGR stdout [SetColor Foreground Dull Green]
putStrLn msg
setSGR stdout [Reset]

errorMessage :: Text -> App ()
errorMessage :: Text -> Brok ()
errorMessage msg = do
setSGR stderr [SetColor Foreground Dull Red]
lift $ hPutStrLn stderr msg
setSGR stderr [Reset]

errors :: Text -> [Text] -> App ()
errors :: Text -> [Text] -> Brok ()
errors _ [] = return ()
errors msg missing = do
errorMessage msg
lift $ hPutStrLn stderr ""
errorMessage (unlines $ ("- " ++) <$> missing)

split :: Handle -> Color -> Text -> Text -> App ()
split :: Handle -> Color -> Text -> Text -> Brok ()
split hdl color left right = do
setSGR hdl [SetColor Foreground Dull color]
lift $ hPutStr hdl left
setSGR hdl [Reset]
lift $ hPutStr hdl $ ": " ++ right
lift $ hPutStrLn hdl ""

splitErr :: Text -> Text -> App ()
splitErr :: Text -> Text -> Brok ()
splitErr = split stderr Red

splitOut :: Text -> Text -> App ()
splitOut :: Text -> Text -> Brok ()
splitOut = split stdout Green

splitMeh :: Text -> Text -> App ()
splitMeh :: Text -> Text -> Brok ()
splitMeh = split stdout Yellow

replace :: Text -> App ()
replace :: Text -> Brok ()
replace msg = do
lift $ hCursorUpLine stdout 1
lift $ hClearLine stdout
Expand Down
20 changes: 10 additions & 10 deletions src/Brok/IO/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import Data.Time.Clock.POSIX (getPOSIXTime)
import System.Directory (doesFileExist)

import Brok.Parser.DB (db)
import Brok.Types.App (App)
import Brok.Types.Config (cache)
import Brok.Types.Brok (Brok, appConfig)
import Brok.Types.URL (URL)

path :: String
path = ".brokdb"

-- time stuff
removeOld :: Integer -> [(URL, Integer)] -> App [(URL, Integer)]
removeOld :: Integer -> [(URL, Integer)] -> Brok [(URL, Integer)]
removeOld age cached = do
timestamp <- lift getPOSIXTime
return $ filter ((\val -> timestamp - val < fromInteger age) . fromInteger . snd) cached

stamp :: URL -> App (URL, Integer)
stamp :: URL -> Brok (URL, Integer)
stamp lnk = do
timestamp <- lift $ round <$> getPOSIXTime
return (lnk, timestamp)
Expand All @@ -35,12 +35,12 @@ stamp lnk = do
linkToText :: (URL, Integer) -> Text
linkToText (lnk, int) = concat [lnk, " ", tshow int]

write :: [(URL, Integer)] -> App ()
write :: [(URL, Integer)] -> Brok ()
write links = writeFile path . encodeUtf8 . unlines $ linkToText <$> links

setCached :: [URL] -> App ()
setCached :: [URL] -> Brok ()
setCached links = do
mAge <- asks cache
mAge <- cache <$> asks appConfig
case mAge of
Nothing -> pure ()
Just age -> do
Expand All @@ -49,19 +49,19 @@ setCached links = do
write $ current ++ stamped

-- read db
read :: Integer -> FilePath -> App [(URL, Integer)]
read :: Integer -> FilePath -> Brok [(URL, Integer)]
read age filepath = removeOld age =<< fromRight [] . db . decodeUtf8 <$> readFile filepath

load :: Integer -> App [(URL, Integer)]
load :: Integer -> Brok [(URL, Integer)]
load age = do
exists <- lift $ doesFileExist path
if exists
then read age path
else return []

getCached :: App [URL]
getCached :: Brok [URL]
getCached = do
mAge <- asks cache
mAge <- cache <$> asks appConfig
case mAge of
Nothing -> pure []
(Just age) -> (fst <$>) <$> load age
4 changes: 2 additions & 2 deletions src/Brok/IO/Document.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import ClassyPrelude

import System.Directory (doesFileExist)

import Brok.Types.App (App)
import Brok.Types.Brok (Brok)
import Brok.Types.Result

readContent :: Result -> App Result
readContent :: Result -> Brok Result
readContent result = do
let path = getPath result
let filepath = unpack path
Expand Down
Loading

0 comments on commit 4c99842

Please sign in to comment.