Skip to content

Commit

Permalink
generalise file extension check and also check in shebang.
Browse files Browse the repository at this point in the history
note that i would have used shellcheck for this information but it does
not export this data structure.
  • Loading branch information
Philip Cunningham committed Sep 24, 2016
1 parent e51fbe3 commit 48413c8
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/CC/ShellCheck/ShellScript.hs
Expand Up @@ -11,7 +11,9 @@ module CC.ShellCheck.ShellScript (

import Control.Monad.Extra
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as Char8
import Data.List
import Data.Monoid
import Data.Shebang (Shebang(..), Interpretter(..), Argument(..))
import qualified Data.Shebang as Shebang
import System.Directory
Expand All @@ -20,9 +22,24 @@ import System.FilePath.Posix

--------------------------------------------------------------------------------

-- | List of shells the engine should be able to handle.
validShells :: [BS.ByteString]
validShells = ["sh", "ash", "dash", "bash", "ksh"]

--------------------------------------------------------------------------------

-- | List of valid shell file extensions.
validShellExtensions :: [BS.ByteString]
validShellExtensions = ("." <>) <$> validShells

--------------------------------------------------------------------------------

-- | Checks to see if file has correct extension.
hasShellExtension :: FilePath -> Bool
hasShellExtension path = takeExtension path == ".sh" || takeExtension path == ".bash" || takeExtension path == ".dash" || takeExtension path == ".ksh" || takeExtension path == ".ash"
hasShellExtension path = extension `elem` validShellExtensions
where
extension :: BS.ByteString
extension = Char8.pack $ takeExtension path

--------------------------------------------------------------------------------

Expand All @@ -32,11 +49,8 @@ hasValidInterpretter (Shebang (Interpretter int) maybeArgument) =
if BS.isSuffixOf "env" int
then case maybeArgument of
Nothing -> False
Just (Argument arg) -> any (`BS.isPrefixOf` arg) shellScriptWhitelist
else any (`BS.isSuffixOf` int) shellScriptWhitelist
where
shellScriptWhitelist :: [BS.ByteString]
shellScriptWhitelist = ["sh", "ash", "dash", "bash", "ksh"]
Just (Argument arg) -> any (`BS.isPrefixOf` arg) validShells
else any (`BS.isSuffixOf` int) validShells

--------------------------------------------------------------------------------

Expand Down

0 comments on commit 48413c8

Please sign in to comment.