propellor spin

This commit is contained in:
Joey Hess 2014-11-18 20:33:25 -04:00
parent cdad4fbe15
commit f9f60822ff
Failed to extract signature
1 changed files with 22 additions and 17 deletions

View File

@ -21,20 +21,23 @@ import qualified Propellor.Property.Docker.Shim as DockerShim
import Utility.FileMode import Utility.FileMode
import Utility.SafeCommand import Utility.SafeCommand
usage :: Handle -> IO a usage :: Handle -> IO ()
usage h = do usage h = hPutStrLn h $ unlines
hPutStrLn h $ unlines [ "Usage:"
[ "Usage:" , " propellor"
, " propellor" , " propellor hostname"
, " propellor hostname" , " propellor --spin hostname"
, " propellor --spin hostname" , " propellor --add-key keyid"
, " propellor --add-key keyid" , " propellor --set field context"
, " propellor --set field context" , " propellor --dump field context"
, " propellor --dump field context" , " propellor --edit field context"
, " propellor --edit field context" , " propellor --list-fields"
, " propellor --list-fields" ]
]
exitFailure usageError :: [String] -> IO a
usageError ps = do
usage stderr
error ("(Unexpected: " ++ show ps)
processCmdLine :: IO CmdLine processCmdLine :: IO CmdLine
processCmdLine = go =<< getArgs processCmdLine = go =<< getArgs
@ -46,21 +49,23 @@ processCmdLine = go =<< getArgs
go ("--dump":f:c:[]) = withprivfield f c Dump go ("--dump":f:c:[]) = withprivfield f c Dump
go ("--edit":f:c:[]) = withprivfield f c Edit go ("--edit":f:c:[]) = withprivfield f c Edit
go ("--list-fields":[]) = return ListFields go ("--list-fields":[]) = return ListFields
go ("--help":_) = usage stdout go ("--help":_) = do
usage stdout
exitFailure
go ("--update":h:[]) = return $ Update h go ("--update":h:[]) = return $ Update h
go ("--boot":h:[]) = return $ Update h -- for back-compat go ("--boot":h:[]) = return $ Update h -- for back-compat
go ("--continue":s:[]) = case readish s of go ("--continue":s:[]) = case readish s of
Just cmdline -> return $ Continue cmdline Just cmdline -> return $ Continue cmdline
Nothing -> errorMessage $ "--continue serialization failure (" ++ s ++ ")" Nothing -> errorMessage $ "--continue serialization failure (" ++ s ++ ")"
go (h:[]) go (h:[])
| "--" `isPrefixOf` h = usage stderr | "--" `isPrefixOf` h = usageError [h]
| otherwise = return $ Run h | otherwise = return $ Run h
go [] = do go [] = do
s <- takeWhile (/= '\n') <$> readProcess "hostname" ["-f"] s <- takeWhile (/= '\n') <$> readProcess "hostname" ["-f"]
if null s if null s
then errorMessage "Cannot determine hostname! Pass it on the command line." then errorMessage "Cannot determine hostname! Pass it on the command line."
else return $ Run s else return $ Run s
go _ = usage stderr go v = usageError v
withprivfield s c f = case readish s of withprivfield s c f = case readish s of
Just pf -> return $ f pf (Context c) Just pf -> return $ f pf (Context c)