Skip to content

Commit

Permalink
Merge pull request #94 from serokell/sancho20021/#44-merge-filter-fil…
Browse files Browse the repository at this point in the history
…ter-field

[#44] Merge `--filter` and `--filter-field` options
  • Loading branch information
sancho20021 authored May 3, 2022
2 parents 61d77cd + a01366c commit 05844ec
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
8 changes: 4 additions & 4 deletions lib/Backend/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ deleteFieldCmd config (DeleteFieldOptions qPath@(QualifiedPath backendNameMb pat
findCmd
:: (Members '[BackendEffect, Error CofferError] r)
=> Config -> FindOptions -> Sem r (Maybe Directory)
findCmd config (FindOptions qPathMb textMb sortMb filters filterFields) = do
findCmd config (FindOptions qPathMb textMb sortMb filters) = do
let backendNameMb = qPathMb >>= qpBackendName
backend <- getBackend config backendNameMb
let
Expand All @@ -196,9 +196,10 @@ findCmd config (FindOptions qPathMb textMb sortMb filters filterFields) = do
applyFilter e = \case
FilterByName substr -> substr `T.isInfixOf` (e ^. E.path . to entryPathName)
FilterByDate op date -> matchDate op date (e ^. dateModified)
FilterByField name field -> applyFilterField e name field

applyFilterField :: Entry -> (FieldName, FilterField) -> Bool
applyFilterField e (fieldName, filter) =
applyFilterField :: Entry -> FieldName -> FilterField -> Bool
applyFilterField e fieldName filter =
case e ^? fields . ix fieldName of
Nothing -> False
Just field ->
Expand All @@ -217,7 +218,6 @@ findCmd config (FindOptions qPathMb textMb sortMb filters filterFields) = do
& Dir.filterEntries (\e ->
(filterByPath e || filterByTag e)
&& all (applyFilter e) filters
&& all (applyFilterField e) filterFields
)
& Dir.mapDir (\entries ->
case sortMb of
Expand Down
45 changes: 11 additions & 34 deletions lib/CLI/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import Entry
import Fmt (pretty)
import Options.Applicative
import Options.Applicative.Help.Pretty qualified as Pretty
import Text.Megaparsec (try)
import Text.Megaparsec qualified as P
import Text.Megaparsec.Char qualified as P
import Text.Megaparsec.Char.Lexer qualified as P
Expand Down Expand Up @@ -233,14 +234,6 @@ findOptions =
, expectedFilterFormat
]
])
<*> many (option readFilterField $ mconcat
[ metavar "FILTERFIELD"
, long "filter-field"
, helpDoc $ Just $ Pretty.vsep
[ "Filter entries based on a field."
, expectedFilterFieldFormat
]
])

renameOptions :: Parser RenameOptions
renameOptions =
Expand Down Expand Up @@ -505,29 +498,14 @@ expectedQualifiedPathFormat = Pretty.vsep

expectedFilterFormat :: Pretty.Doc
expectedFilterFormat = Pretty.vsep
[ "Expected format is: 'name~<substring>' or `date<op><date>`."
, "<op> can be '<=', '>=', '<', '>', or '='."
, "<date> can be 'YYYY', 'YYYY-MM', 'YYYY-MM-DD', or 'YYYY-MM-DD HH:MM:SS'."
, "Examples: 'name~vault', 'date<2020-02'."
]

readFilterField :: ReadM (FieldName, FilterField)
readFilterField = do
eitherReader \input ->
P.parse (parseFilterField <* P.eof) "" (T.pack input) & first \err -> unlines
[ "Invalid filter-field format: " <> show input <> "."
, show expectedFilterFieldFormat
, ""
, "Parser error:"
, P.errorBundlePretty err
]

expectedFilterFieldFormat :: Pretty.Doc
expectedFilterFieldFormat = Pretty.vsep
[ "Expected format is: '<fieldname>:contents~<substring>' or `<fieldname>:date<op><date>`."
[ "Expected format is:"
, " * to filter by entry name: 'name~<substring>',"
, " * to filter by entry's last modified date: 'date<op><date>',"
, " * to filter by a field's contents: '<fieldname>:contents~<substring>',"
, " * to filter by a field's last modified date: '<fieldname>:date<op><date>'."
, "<op> can be '<=', '>=', '<', '>', or '='."
, "<date> can be 'YYYY', 'YYYY-MM', 'YYYY-MM-DD', or 'YYYY-MM-DD HH:MM:SS'."
, "Examples: 'url:contents~google.com', 'pw:date<2020-02'."
, "Examples: 'name~vault', 'date<2020-02', 'url:contents~google.com', 'pw:date<2020-02'."
]

----------------------------------------------------------------------------
Expand Down Expand Up @@ -588,7 +566,7 @@ parseFilterOp =

parseFilter :: MParser Filter
parseFilter =
parseFilterByName <|> parseFilterByDate
try parseFilterByName <|> try parseFilterByDate <|> parseFilterByField
where
parseFilterByName = do
void $ P.string "name" >> P.char '~'
Expand All @@ -601,13 +579,12 @@ parseFilter =
localTime <- parseFilterDate
pure $ FilterByDate op localTime

parseFilterField :: MParser (FieldName, FilterField)
parseFilterField = do
parseFilterByField :: MParser Filter
parseFilterByField = do
fieldName <- parseFieldNameWhile (/= ':')
void $ P.char ':'
filterField <- parseFilterFieldByContents <|> parseFilterFieldByDate

pure (fieldName, filterField)
pure $ FilterByField fieldName filterField
where
parseFilterFieldByContents = do
void $ P.string "contents" >> P.char '~'
Expand Down
2 changes: 1 addition & 1 deletion lib/CLI/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ data FindOptions = FindOptions
, foText :: Maybe Text
, foSort :: Maybe (Sort, Direction)
, foFilters :: [Filter]
, foFilterFields :: [(FieldName, FilterField)]
}
deriving stock Show

Expand Down Expand Up @@ -193,6 +192,7 @@ data FilterDate
data Filter
= FilterByDate FilterOp FilterDate
| FilterByName Text
| FilterByField FieldName FilterField
deriving stock Show

data FilterField
Expand Down
4 changes: 2 additions & 2 deletions tests/golden/common/common.bats
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ EOF
}

@test "bad filter field" {
run coffer find --filter-field "$(echo -e "name:bad\n\n~str")"
run coffer find --filter "$(echo -e "name:bad\n\n~str")"

assert_failure
assert_output --partial - <<EOF
option --filter-field: Invalid filter-field format: "name:bad\n\n~str".
option --filter: Invalid filter format: "name:bad\n\n~str".
EOF
}
37 changes: 33 additions & 4 deletions tests/golden/find-command/find-command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ EOF
coffer create /cd --field filtering=kek
coffer create /abcd --field filtering=kek

run cleanOutput coffer find / --filter name~ab --filter-field filtering:contents~kek
run cleanOutput coffer find / --filter name~ab --filter filtering:contents~kek

assert_success
assert_output - <<EOF
Expand Down Expand Up @@ -263,7 +263,7 @@ EOF
coffer create /secrets/b --field filtering=cab
coffer create /secrets/c --field filtering=zzz

run cleanOutput coffer find / --filter-field filtering:contents~bb
run cleanOutput coffer find / --filter filtering:contents~bb

assert_success
assert_output - <<EOF
Expand All @@ -273,7 +273,7 @@ EOF
filtering: abbacaba [2000-01-01 01:01:01]
EOF

run cleanOutput coffer find / --filter-field filtering:contents~ab
run cleanOutput coffer find / --filter filtering:contents~ab

assert_success
assert_output - <<EOF
Expand All @@ -285,7 +285,7 @@ EOF
filtering: cab [2000-01-01 01:01:01]
EOF

run cleanOutput coffer find / --filter-field filtering:contents~zz
run cleanOutput coffer find / --filter filtering:contents~zz

assert_success
assert_output - <<EOF
Expand Down Expand Up @@ -366,3 +366,32 @@ EOF
x: y [2000-01-01 01:01:01]
EOF
}

@test "filter field with name 'date' and 'name'" {
coffer create /secrets/d/ab --field date=2000 --field name=a
coffer create /secrets/d/ad --field date=2020 --field name=b
coffer create /secrets/d/abc --field date=9999

run cleanOutput coffer find / ab --filter date:contents~20 --filter "date>1000" --filter name~a

assert_success
assert_output - <<EOF
/
secrets/
d/
ab - [2000-01-01 01:01:01]
name: a [2000-01-01 01:01:01]
date: 2000 [2000-01-01 01:01:01]
EOF

run cleanOutput coffer find --filter name:contents~a --filter name~a
assert_success
assert_output - <<EOF
/
secrets/
d/
ab - [2000-01-01 01:01:01]
name: a [2000-01-01 01:01:01]
date: 2000 [2000-01-01 01:01:01]
EOF
}

0 comments on commit 05844ec

Please sign in to comment.