Skip to content

Commit

Permalink
Merge branch 'release/0.1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
smallhadroncollider committed Feb 8, 2019
2 parents 55b0ff0 + 3b6225b commit 7166285
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 32 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,29 @@ By default brök waits for 100ms between URL checks. You can change the delay:
# wait for 1 second between checks
brok --interval 1000 test.md links.tex
```

### 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`.

#### `bash`

```bash
#! /bin/bash

# cache for 1 week
# use find to check all *.md files
# only show errors (if there are any)
brok --cache 604800 $(find . -type f -name "*.md") > /dev/null
```

#### `zsh`

```bash
#! /bin/zsh

# cache for 1 week
# using a zsh glob to check all *.md files
# only show errors (if there are any)
brok --cache 604800 */**/*.md > /dev/null
```
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.4.0
version: 0.1.5.0
github: "smallhadroncollider/brok"
license: BSD3
author: "Small Hadron Collider"
Expand Down
10 changes: 6 additions & 4 deletions src/Brok/IO/DB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ linkToText (lnk, int) = concat [lnk, " ", tshow int]
write :: [(URL, Integer)] -> IO ()
write links = writeFile path . encodeUtf8 . unlines $ linkToText <$> links

setCached :: Integer -> [URL] -> IO ()
setCached age links = do
setCached :: Maybe Integer -> [URL] -> IO ()
setCached Nothing _ = return ()
setCached (Just age) links = do
current <- load age
stamped <- sequence (stamp <$> links)
write $ current ++ stamped
Expand All @@ -53,5 +54,6 @@ load age = do
then read age path
else return []

getCached :: Integer -> IO [URL]
getCached age = (fst <$>) <$> load age
getCached :: Maybe Integer -> IO [URL]
getCached Nothing = return []
getCached (Just age) = (fst <$>) <$> load age
14 changes: 6 additions & 8 deletions src/Brok/Parser/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ import Brok.Types.Config
import Brok.Types.Next (Next (..))

data Option
= Cache Integer
= Cache (Maybe Integer)
| Interval Integer
| Ignore [Text]
| Files [Text]

readInt :: String -> String -> Parser Integer
readInt arg value =
maybe (fail $ "Unable to parse " ++ arg ++ " value") return (readMay value :: Maybe Integer)
noCacheP :: Parser Option
noCacheP = lexeme $ string "--no-cache" $> Cache Nothing

cacheP :: Parser Option
cacheP = lexeme $ Cache <$> (string "--cache" *> char '\n' *> many1 digit >>= readInt "cache")
cacheP = lexeme $ Cache . Just <$> (string "--cache" *> char '\n' *> decimal)

intervalP :: Parser Option
intervalP =
lexeme $ Interval <$> (string "--interval" *> char '\n' *> many1 digit >>= readInt "interval")
intervalP = lexeme $ Interval <$> (string "--interval" *> char '\n' *> decimal)

urlP :: Parser Text
urlP = lexeme url
Expand All @@ -50,7 +48,7 @@ optsToConfig = foldl' convert defaultConfig

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

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

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

defaultConfig :: Config
defaultConfig = Config {cache = 84600, ignore = [], interval = 100, files = []}
defaultConfig = Config {cache = Just 84600, ignore = [], interval = 100, files = []}
1 change: 1 addition & 0 deletions taskell.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Features

- Should show line number of found links
- Should detect if internet connection is down
- Cache length option should accept units: s, m, h, d - default to s
- Should be able to detect links without http:// or https:// prefixes
Expand Down
13 changes: 1 addition & 12 deletions test/IO/HttpTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,7 @@ test_http =
"https://code.tutsplus.com/tutorials/stateful-vs-stateless-functional-components-in-react--cms-29541"
(Working 200))
result
, testCase "Random blog (404 on a HEAD request)" $ do
result <-
check 0 $
urlToLink
"https://blog.infinitenegativeutility.com/2017/12/some-notes-about-how-i-write-haskell"
assertEqual
"Returns a 200"
(Link
"https://blog.infinitenegativeutility.com/2017/12/some-notes-about-how-i-write-haskell"
(Working 200))
result
, testCase "Non-existant site" $ do
, testCase "Non-existent site" $ do
result <- check 0 $ urlToLink "http://askdjfhaksjdhfkajsdfh.com"
assertEqual
"Returns a 200"
Expand Down
17 changes: 12 additions & 5 deletions test/OptionsTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,26 @@ test_options =
"gives back files"
(Right (Continue (defaultConfig {files = ["blah.md", "tests/spoon.md"]})))
(parse ["blah.md", "tests/spoon.md"]))
, testCase
"single file with no-cache option"
(assertEqual
"gives back files"
(Right (Continue (defaultConfig {cache = Nothing, files = ["blah.md"]})))
(parse ["--no-cache", "blah.md"]))
, testCase
"single file with cache option"
(assertEqual
"gives back files"
(Right (Continue (defaultConfig {cache = 172800, files = ["blah.md"]})))
(Right (Continue (defaultConfig {cache = Just 172800, files = ["blah.md"]})))
(parse ["--cache", "172800", "blah.md"]))
, testCase
"multiple files with cache option"
(assertEqual
"gives back files"
(Right
(Continue
(defaultConfig {cache = 172800, files = ["blah.md", "tests/spoon.md"]})))
(defaultConfig
{cache = Just 172800, files = ["blah.md", "tests/spoon.md"]})))
(parse ["--cache", "172800", "blah.md", "tests/spoon.md"]))
, testCase
"multiple files with interval option"
Expand Down Expand Up @@ -79,7 +86,7 @@ test_options =
(Right
(Continue
(defaultConfig
{ cache = 172800
{ cache = Just 172800
, interval = 400
, ignore = ["http://www.google.com", "http://facebook.com"]
, files = ["blah.md", "tests/spoon.md"]
Expand All @@ -102,7 +109,7 @@ test_options =
(Right
(Continue
(defaultConfig
{ cache = 172800
{ cache = Just 172800
, interval = 400
, ignore = ["http://www.google.com", "http://facebook.com"]
, files = ["blah.md", "tests/spoon.md"]
Expand All @@ -125,7 +132,7 @@ test_options =
(Right
(Continue
(defaultConfig
{ cache = 172800
{ cache = Just 172800
, interval = 400
, ignore = ["http://www.google.com", "http://facebook.com"]
, files = ["blah.md", "tests/spoon.md"]
Expand Down

0 comments on commit 7166285

Please sign in to comment.