Skip to content

Commit 48413c8

Browse files
author
Philip Cunningham
committed
generalise file extension check and also check in shebang.
note that i would have used shellcheck for this information but it does not export this data structure.
1 parent e51fbe3 commit 48413c8

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/CC/ShellCheck/ShellScript.hs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module CC.ShellCheck.ShellScript (
1111

1212
import Control.Monad.Extra
1313
import qualified Data.ByteString as BS
14+
import qualified Data.ByteString.Char8 as Char8
1415
import Data.List
16+
import Data.Monoid
1517
import Data.Shebang (Shebang(..), Interpretter(..), Argument(..))
1618
import qualified Data.Shebang as Shebang
1719
import System.Directory
@@ -20,9 +22,24 @@ import System.FilePath.Posix
2022

2123
--------------------------------------------------------------------------------
2224

25+
-- | List of shells the engine should be able to handle.
26+
validShells :: [BS.ByteString]
27+
validShells = ["sh", "ash", "dash", "bash", "ksh"]
28+
29+
--------------------------------------------------------------------------------
30+
31+
-- | List of valid shell file extensions.
32+
validShellExtensions :: [BS.ByteString]
33+
validShellExtensions = ("." <>) <$> validShells
34+
35+
--------------------------------------------------------------------------------
36+
2337
-- | Checks to see if file has correct extension.
2438
hasShellExtension :: FilePath -> Bool
25-
hasShellExtension path = takeExtension path == ".sh" || takeExtension path == ".bash" || takeExtension path == ".dash" || takeExtension path == ".ksh" || takeExtension path == ".ash"
39+
hasShellExtension path = extension `elem` validShellExtensions
40+
where
41+
extension :: BS.ByteString
42+
extension = Char8.pack $ takeExtension path
2643

2744
--------------------------------------------------------------------------------
2845

@@ -32,11 +49,8 @@ hasValidInterpretter (Shebang (Interpretter int) maybeArgument) =
3249
if BS.isSuffixOf "env" int
3350
then case maybeArgument of
3451
Nothing -> False
35-
Just (Argument arg) -> any (`BS.isPrefixOf` arg) shellScriptWhitelist
36-
else any (`BS.isSuffixOf` int) shellScriptWhitelist
37-
where
38-
shellScriptWhitelist :: [BS.ByteString]
39-
shellScriptWhitelist = ["sh", "ash", "dash", "bash", "ksh"]
52+
Just (Argument arg) -> any (`BS.isPrefixOf` arg) validShells
53+
else any (`BS.isSuffixOf` int) validShells
4054

4155
--------------------------------------------------------------------------------
4256

0 commit comments

Comments
 (0)