beautiful table for --list-fields, with the hostnames

This commit is contained in:
Joey Hess 2014-07-06 16:44:13 -04:00
parent 5002b5e391
commit 20df4170b0
2 changed files with 11 additions and 7 deletions

View File

@ -76,7 +76,7 @@ defaultMain hostlist = do
go _ (Set field context) = setPrivData field context go _ (Set field context) = setPrivData field context
go _ (Dump field context) = dumpPrivData field context go _ (Dump field context) = dumpPrivData field context
go _ (Edit field context) = editPrivData field context go _ (Edit field context) = editPrivData field context
go _ ListFields = listPrivDataFields go _ ListFields = listPrivDataFields hostlist
go _ (AddKey keyid) = addKey keyid go _ (AddKey keyid) = addKey keyid
go _ (Chain hn) = withhost hn $ \h -> do go _ (Chain hn) = withhost hn $ \h -> do
r <- runPropellor h $ ensureProperties $ hostProperties h r <- runPropellor h $ ensureProperties $ hostProperties h

View File

@ -8,6 +8,7 @@ import System.IO
import System.Directory import System.Directory
import Data.Maybe import Data.Maybe
import Data.Monoid import Data.Monoid
import Data.List
import Control.Monad import Control.Monad
import Control.Monad.IfElse import Control.Monad.IfElse
import "mtl" Control.Monad.Reader import "mtl" Control.Monad.Reader
@ -88,20 +89,23 @@ editPrivData field context = do
readFile f readFile f
setPrivDataTo field context v' setPrivDataTo field context v'
listPrivDataFields :: IO () listPrivDataFields :: [Host] -> IO ()
listPrivDataFields = do listPrivDataFields hosts = do
m <- decryptPrivData m <- decryptPrivData
putStrLn "\n" putStrLn "\n"
let rows = map mkrow (M.keys m) let usedby = M.unionsWith (++) $ map mkhostmap hosts
let rows = map (mkrow usedby) (M.keys m)
let table = tableWithHeader header rows let table = tableWithHeader header rows
putStr $ unlines $ formatTable table putStr $ unlines $ formatTable table
where where
header = ["Field", "Context", "Hosts"] header = ["Field", "Context", "Used by"]
mkrow (field, (Context context)) = mkrow usedby k@(field, (Context context)) =
[ shellEscape $ show field [ shellEscape $ show field
, shellEscape context , shellEscape context
, "xxx" , intercalate ", " $ sort $ fromMaybe [] $ M.lookup k usedby
] ]
mkhostmap host = M.fromList $ map (\k -> (k, [hostName host])) $
S.toList $ _privDataFields $ hostInfo host
setPrivDataTo :: PrivDataField -> Context -> PrivData -> IO () setPrivDataTo :: PrivDataField -> Context -> PrivData -> IO ()
setPrivDataTo field context value = do setPrivDataTo field context value = do