diff options
author | Yorhel <git@yorhel.nl> | 2018-01-14 12:51:13 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2018-01-14 12:51:13 +0100 |
commit | cf822850437eb922b8eef7a881ec889a722111f6 (patch) | |
tree | 8f88a6ff774f18086272aa4debb1fecc3408040b | |
parent | 7f0525356fb8735bf4fa777b2be8fad8bfa7f604 (diff) |
Add file path tests to pre_if
-rw-r--r-- | Main.hs | 16 | ||||
-rw-r--r-- | nginx-confgen.cabal | 1 | ||||
-rw-r--r-- | test/main.conf | 2 |
3 files changed, 18 insertions, 1 deletions
@@ -9,6 +9,8 @@ import Data.HashMap.Strict (HashMap) import qualified Data.HashMap.Strict as M import Data.List (intercalate) import Data.Void +import System.Directory +import System.IO.Error (tryIOError) import Text.Megaparsec import Text.Megaparsec.Char import qualified Text.Megaparsec.Char.Lexer as L @@ -262,6 +264,16 @@ ifExpand st arg conf = do [a, [Literal "=" ], b] -> (==) <$> interpArg a <*> interpArg b [a, [Literal "!="], b] -> (/=) <$> interpArg a <*> interpArg b + -- File tests + [[Literal "-f"], a] -> interpArg a >>= doesFileExist + [[Literal "!-f"], a] -> not <$> (interpArg a >>= doesFileExist) + [[Literal "-d"], a] -> interpArg a >>= doesDirectoryExist + [[Literal "!-d"], a] -> not <$> (interpArg a >>= doesDirectoryExist) + [[Literal "-e"], a] -> interpArg a >>= doesPathExist + [[Literal "!-e"], a] -> not <$> (interpArg a >>= doesPathExist) + [[Literal "-x"], a] -> interpArg a >>= doesExecutableExist + [[Literal "!-x"], a] -> not <$> (interpArg a >>= doesExecutableExist) + -- Dunno _ -> throwIO IfUnknown @@ -312,6 +324,10 @@ ifExpand st arg conf = do isNonEmpty (Literal "") = False isNonEmpty _ = True + -- System.Directory is missing this check + doesExecutableExist :: FilePath -> IO Bool + doesExecutableExist p = either (const False) executable <$> tryIOError (getPermissions p) + diff --git a/nginx-confgen.cabal b/nginx-confgen.cabal index d36d317..c4bedd1 100644 --- a/nginx-confgen.cabal +++ b/nginx-confgen.cabal @@ -18,5 +18,6 @@ executable nginx-confgen main-is: Main.hs default-language: Haskell2010 build-depends: base + , directory , megaparsec , unordered-containers diff --git a/test/main.conf b/test/main.conf index a946a45..83a8ae1 100644 --- a/test/main.conf +++ b/test/main.conf @@ -9,7 +9,7 @@ events { pre_set $name ""; #" This is the \$name variable"; # Set a variable -pre_if ($name = x$name) { +pre_if (!-d /bin) { the pre_if matched; pre_set $var 2; # Not visible outside of this pre_if block... hmmmm. |