Use exit status and stderr properly in terminal tool

This commit is contained in:
Vidar Holen 2013-10-20 15:03:14 -07:00
parent b1af7bb8f2
commit 206900fb64
1 changed files with 17 additions and 18 deletions

View File

@ -24,7 +24,6 @@ import System.Environment
import System.Exit import System.Exit
import System.IO import System.IO
clear = ansi 0 clear = ansi 0
ansi n = "\x1B[" ++ (show n) ++ "m" ansi n = "\x1B[" ++ (show n) ++ "m"
@ -45,14 +44,14 @@ doFile path colorFunc = do
contents <- readFile actualPath contents <- readFile actualPath
doInput path contents colorFunc doInput path contents colorFunc
else do else do
putStrLn (colorFunc "error" $ "No such file: " ++ actualPath) hPutStrLn stderr (colorFunc "error" $ "No such file: " ++ actualPath)
return False
doInput filename contents colorFunc = do doInput filename contents colorFunc = do
let fileLines = lines contents let fileLines = lines contents
let lineCount = length fileLines let lineCount = length fileLines
let comments = shellCheck contents let comments = shellCheck contents
let groups = groupWith scLine comments let groups = groupWith scLine comments
if not $ null comments then do
mapM_ (\x -> do mapM_ (\x -> do
let lineNum = scLine (head x) let lineNum = scLine (head x)
let line = if lineNum < 1 || lineNum > lineCount let line = if lineNum < 1 || lineNum > lineCount
@ -64,8 +63,7 @@ doInput filename contents colorFunc = do
mapM (\c -> putStrLn (colorFunc (scSeverity c) $ cuteIndent c)) x mapM (\c -> putStrLn (colorFunc (scSeverity c) $ cuteIndent c)) x
putStrLn "" putStrLn ""
) groups ) groups
else do return $ null comments
putStrLn ("No comments for " ++ filename)
cuteIndent comment = cuteIndent comment =
(replicate ((scColumn comment) - 1) ' ') ++ "^-- " ++ (scMessage comment) (replicate ((scColumn comment) - 1) ' ') ++ "^-- " ++ (scMessage comment)
@ -81,6 +79,7 @@ main = do
hPutStrLn stderr "shellcheck -- bash/sh script static analysis tool" hPutStrLn stderr "shellcheck -- bash/sh script static analysis tool"
hPutStrLn stderr "Usage: shellcheck filenames..." hPutStrLn stderr "Usage: shellcheck filenames..."
exitFailure exitFailure
else else do
mapM (\f -> doFile f colors) args statuses <- mapM (\f -> doFile f colors) args
if and statuses then exitSuccess else exitFailure