Skip to content

Commit

Permalink
ovsdb: Add --output-file switch.
Browse files Browse the repository at this point in the history
`ovsdb2ddlog` used to write its output to stdout.  This does not work
well with makefiles as command of the form `ovsdb2ddlog -f
file.schema > file.dl` produces an empty output file given an invalid
input, which will not be automatically deleted by `make`.

We introduce an optional `--output-file` switch that tells
`ovsdb2ddlog` to write its output to the specified file.  Without this
switch, the output will be written to stdout as before.
  • Loading branch information
ryzhyk committed Feb 13, 2020
1 parent 49fed75 commit c755492
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions adapters/ovsdb/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ data TOption = OVSFile String
| ProxyTable String
| ConfigJsonI String
| ConfigJsonO String
| OutputFile String
| Version

data Action = ActionCompile
Expand All @@ -63,9 +64,11 @@ options = [ Option ['v'] ["version"] (NoArg Version) "
, Option [] ["ro"] (ReqArg ROColumn "TABLE.COLUMN") "Mark COLUMN as read-only."
, Option ['k'] ["key"] (ReqArg KeyColumn "TABLE.COLUMN") "Mark COLUMN as key."
, Option ['p'] ["gen-proxy"] (ReqArg ProxyTable "TABLE") "Generate output proxy table for TABLE."
, Option [] ["output-file"] (ReqArg OutputFile "FILE.dl") "Write output to FILE.dl. If this option is not specified, output will be written to stdout."
]

data Config = Config { ovsSchemaFile:: FilePath
, outputFile :: Maybe FilePath
, outputTables :: [(String, [String])]
, proxyTables :: [String]
, keyColumns :: M.Map String [String]
Expand All @@ -77,6 +80,7 @@ instance ToJSON Config

defaultConfig :: Config
defaultConfig = Config { ovsSchemaFile= ""
, outputFile = Nothing
, outputTables = []
, proxyTables = []
, keyColumns = M.empty
Expand All @@ -87,6 +91,9 @@ addOption (_, config) Version = do return (ActionVersion, config)
addOption (a, config) (OVSFile f) = do
when (ovsSchemaFile config /= "") $ errorWithoutStackTrace "Multiple input files specified"
return (a, config {ovsSchemaFile = f})
addOption (a, config) (OutputFile f) = do
when (isJust $ outputFile config) $ errorWithoutStackTrace "Multiple output files specified"
return (a, config {outputFile = Just f})
addOption (a, config) (OutputTable t) = return (a, config{ outputTables = nub ((t,[]) : outputTables config)})
addOption (a, config) (ProxyTable t) = return (a, config{ proxyTables = nub (t : proxyTables config)})
addOption (a, config) (KeyColumn c) = do
Expand Down Expand Up @@ -138,6 +145,8 @@ main = do
then do putStrLn $ "OVSDB-to-DDlog compiler " ++ dDLOG_VERSION ++ " (" ++ gitHash ++ ")"
putStrLn $ "Copyright (c) 2019 VMware, Inc. (MIT License)"
else do
dlschema <- compileSchemaFile ovsSchemaFile outputTables proxyTables keyColumns
putStrLn $ render dlschema
dlschema <- render <$> compileSchemaFile ovsSchemaFile outputTables proxyTables keyColumns
case outputFile of
Nothing -> putStrLn dlschema
Just ofile -> writeFile ofile dlschema
return ()

0 comments on commit c755492

Please sign in to comment.