-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSummarizeSSHKeys.hs
64 lines (53 loc) · 1.61 KB
/
SummarizeSSHKeys.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
import SSHKeys
import Control.Monad
import Data.Foldable
import Options.Applicative
import System.Exit
import System.IO
import Text.Printf
import qualified System.Console.Terminal.Size as TS
data Options = Options
{ optFiles :: [FilePath]
} deriving (Show)
main :: IO ()
main = do
cols <- maybe 100 TS.width <$> TS.size
Options {..} <- customExecParser
( prefs $ columns cols )
( info
( helper <*> do
let strArguments mods = (:) <$> strArgument mods <*> many (strArgument mempty)
optFiles <- strArguments $
metavar "KEYSFILE ..." <> value "/dev/stdin" <>
help "Files containing SSH public keys" <> showDefaultWith id
pure Options{..}
)
( fullDesc <> header "Output readable summaries of SSH authorized_keys files" )
)
for_ optFiles $ \f -> do
result <- parseFile f <$> readFile f
case result of
Left e -> do
hPrint stderr e
exitFailure
Right ls -> do
forM_ ls $ \case
Entry (o, k, h, c) ->
putStrLn $ printf "%s %-7s %-10s %s" (summOpts o) (summKind k) (summHash h) c
_ ->
return ()
summOpts :: [Option] -> String
summOpts o = printf "[%d]" $ length o
summKind :: String -> String
summKind k =
case prefix of
"ssh" -> suffix
_ -> prefix
where
(prefix, rest) = span (/= '-') k
suffix = drop 1 rest
summHash :: String -> String
summHash = reverse . take 10 . dropWhile (== '=') . reverse