Skip to content

Commit

Permalink
Merge branch 'release/0.1.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
smallhadroncollider committed Feb 8, 2019
2 parents 7166285 + c123049 commit c3a8511
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 27 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ brok text.md links.tex > /dev/null

#### Cache

By default brök will cache successes for a day. It will always recheck errors.
By default brök will cache successes for a day in a `.brokdb` file. It will always recheck errors.

If you want to adjust the cache length, you can enter the number of seconds after which the cache invalidates:

Expand All @@ -67,6 +67,14 @@ If you want to adjust the cache length, you can enter the number of seconds afte
brok --cache 604800 test.md links.tex
```

If you want to avoid creating the `.brokdb` file or ignore the cache entirely you can use the `--no-cache` option:

```bash
# do not cache results
# and don't use previously generated cache
brok --no-cache test.md links.tex
```

#### Ignore URLs

You can tell brök to ignore URLs with specified prefixes:
Expand All @@ -85,6 +93,18 @@ By default brök waits for 100ms between URL checks. You can change the delay:
brok --interval 1000 test.md links.tex
```

#### Only Show Failures

If you want to see what's going on, but you're not interested in successes, then you can use the `--only-failures` option:

```bash
# see what's going on, but only show failures
brok --only-failures test.md links.tex
```

If you're using brök as part of a script then you should [redirect `stdout`](#basic-usage).


### Git Pre-Commit Hook

If you want to check all the links in your Git repo are valid before being able to commit then add something like the following to `.git/hooks/pre-commit`.
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: brok
version: 0.1.5.0
version: 0.1.6.0
github: "smallhadroncollider/brok"
license: BSD3
author: "Small Hadron Collider"
Expand Down
6 changes: 3 additions & 3 deletions src/Brok.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Brok.IO.Http (check)
import Brok.IO.Output (output)
import Brok.Options (parse)
import Brok.Parser.Links (links)
import qualified Brok.Types.Config as C (Config, cache, files, ignore, interval)
import qualified Brok.Types.Config as C (Config, cache, files, ignore, interval, onlyFailures)
import Brok.Types.Link (getURL, isSuccess)
import Brok.Types.Next (Next (..))
import Brok.Types.Result (cachedLinks, ignoredLinks, justLinks, linkIOMap, parseLinks,
Expand All @@ -42,11 +42,11 @@ go config
-- display results
putStrLn ""
header "Results"
anyErrors <- sequence $ output <$> checked
anyErrors <- output (C.onlyFailures config) checked
-- cache successes
setCached (C.cache config) $ getURL <$> filter isSuccess (concat (justLinks <$> checked))
-- exit with appropriate status code
if foldl' (||) False anyErrors
if anyErrors
then void exitFailure
else void exitSuccess

Expand Down
6 changes: 6 additions & 0 deletions src/Brok/IO/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ header msg = do
hPutStrLn stdout $ "*** " ++ msg ++ " ***"
hSetSGR stdout [Reset]

successMessage :: Text -> IO ()
successMessage msg = do
hSetSGR stdout [SetColor Foreground Dull Green]
hPutStrLn stdout msg
hSetSGR stdout [Reset]

errorMessage :: Text -> IO ()
errorMessage msg = do
hSetSGR stderr [SetColor Foreground Dull Red]
Expand Down
44 changes: 28 additions & 16 deletions src/Brok/IO/Output.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,41 @@ statusError (Link _ Cached) = False
statusError (Link _ Ignored) = False
statusError _ = True

countErrors :: [Link] -> Int
countErrors statuses = length $ filter statusError statuses

outputPath :: TFilePath -> Text
outputPath path = concat ["\n", "[", path, "]"]

output :: Result -> IO Bool
output (Result path NotFound) = do
outputMap :: Bool -> Result -> IO Bool
outputMap _ (Result path NotFound) = do
errorMessage $ outputPath path
errorMessage " - File not found"
return True
output (Result path (ParseError err)) = do
outputMap _ (Result path (ParseError err)) = do
errorMessage $ outputPath path
errorMessage " - Parse error:"
errorMessage err
return True
output (Result path (Links links)) = do
let errs = countErrors links /= 0
if errs
then errorMessage $ outputPath path
else message $ outputPath path
if not (null links)
then sequence_ $ linkOutput <$> links
else putStrLn "- No links found in file"
return errs
output _ = return False
outputMap onlyFailures (Result path (Links links)) = do
let errs = filter statusError links
let anyErrs = not (null errs)
if anyErrs
then do
errorMessage $ outputPath path
sequence_ $
linkOutput <$>
(if onlyFailures
then errs
else links)
else unless onlyFailures $ do
message $ outputPath path
if not (null links)
then sequence_ $ linkOutput <$> links
else putStrLn "- No links found in file"
return anyErrs
outputMap _ _ = return False

output :: Bool -> [Result] -> IO Bool
output onlyFailures results = do
errs <- sequence $ outputMap onlyFailures <$> results
let anyErrs = foldl' (||) False errs
when (not anyErrs && onlyFailures) $ successMessage "All links working"
return anyErrs
7 changes: 6 additions & 1 deletion src/Brok/Parser/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ data Option
| Interval Integer
| Ignore [Text]
| Files [Text]
| OnlyFailures

onlyFailuresP :: Parser Option
onlyFailuresP = lexeme $ string "--only-failures" $> OnlyFailures

noCacheP :: Parser Option
noCacheP = lexeme $ string "--no-cache" $> Cache Nothing
Expand All @@ -45,10 +49,11 @@ optsToConfig = foldl' convert defaultConfig
convert dc (Ignore i) = dc {ignore = i}
convert dc (Interval i) = dc {interval = i}
convert dc (Files i) = dc {files = i}
convert dc OnlyFailures = dc {onlyFailures = True}

arguments :: Parser Config
arguments = do
opts <- many' (noCacheP <|> cacheP <|> intervalP <|> ignoreP)
opts <- many' (noCacheP <|> cacheP <|> intervalP <|> ignoreP <|> onlyFailuresP)
fls <- many1 fileP
return . optsToConfig $ opts ++ [Files fls]

Expand Down
12 changes: 7 additions & 5 deletions src/Brok/Types/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import ClassyPrelude
import Brok.Types.Link (URL)

data Config = Config
{ cache :: Maybe Integer
, ignore :: [URL]
, interval :: Integer
, files :: [Text]
{ cache :: Maybe Integer
, ignore :: [URL]
, interval :: Integer
, files :: [Text]
, onlyFailures :: Bool
} deriving (Show, Eq)

defaultConfig :: Config
defaultConfig = Config {cache = Just 84600, ignore = [], interval = 100, files = []}
defaultConfig =
Config {cache = Just 84600, ignore = [], interval = 100, files = [], onlyFailures = False}
6 changes: 6 additions & 0 deletions test/OptionsTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ test_options =
"gives back files"
(Right (Continue (defaultConfig {files = ["blah.md", "tests/spoon.md"]})))
(parse ["blah.md", "tests/spoon.md"]))
, testCase
"single file with only-failures option"
(assertEqual
"gives back files"
(Right (Continue (defaultConfig {onlyFailures = True, files = ["blah.md"]})))
(parse ["--only-failures", "blah.md"]))
, testCase
"single file with no-cache option"
(assertEqual
Expand Down

0 comments on commit c3a8511

Please sign in to comment.