|
| 1 | +{- |
| 2 | +Copyright 2023 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +-} |
| 16 | + |
| 17 | +module Arguments (validate, translate) where |
| 18 | + |
| 19 | +import Data.List (group, sort) |
| 20 | +import Data.Maybe (mapMaybe) |
| 21 | + |
| 22 | +validate :: [String] -> Maybe String |
| 23 | +validate args |
| 24 | + | [] <- errors = Nothing |
| 25 | + | otherwise = Just $ unlines errors |
| 26 | + where |
| 27 | + errors = mapMaybe forString args ++ map (\s -> "duplicate argument: \"" <> s <> "\"") duplicates |
| 28 | + forString s = |
| 29 | + if '=' `elem` s |
| 30 | + then Nothing |
| 31 | + else Just ("no '=' in \"" <> s <> "\"") |
| 32 | + keys = map (fst . toTuple) args |
| 33 | + duplicates = concatMap (take 1) $ filter ((<) 1 . length) $ group $ sort keys |
| 34 | + |
| 35 | +translate :: [String] -> (FilePath, [String], Maybe String, Maybe String) |
| 36 | +translate args = (executable', path' : "-j" : "--sarif" : "--no-exit-code" : flags, category, token) |
| 37 | + where |
| 38 | + argsMap = map toTuple args |
| 39 | + executable = lookup "binary" argsMap |
| 40 | + executable' |
| 41 | + | Nothing <- executable = "/hlint" |
| 42 | + | Just "" <- executable = "/hlint" |
| 43 | + | Just s <- executable = s |
| 44 | + path = lookup "path" argsMap |
| 45 | + path' |
| 46 | + | Nothing <- path = "." |
| 47 | + | Just "" <- path = "." |
| 48 | + | Just s <- path = s |
| 49 | + category = lookup "category" argsMap |
| 50 | + token = lookup "token" argsMap |
| 51 | + flags = |
| 52 | + concatMap toFlag $ |
| 53 | + filter (flip elem ["binary", "path", "category", "token"] . fst) argsMap |
| 54 | + |
| 55 | +toTuple :: String -> (String, String) |
| 56 | +toTuple s = (key, drop 1 prefixedValue) |
| 57 | + where |
| 58 | + (key, prefixedValue) = break (== '=') s |
| 59 | + |
| 60 | +toFlag :: (String, String) -> [String] |
| 61 | +toFlag _ = [] |
0 commit comments