mirror of
https://github.com/digital-asset/daml.git
synced 2024-09-20 01:07:18 +03:00
Add an --output-file option to DAML Script (#5590)
This PR adds an --output-file option to DAML Script that writes the result of a DAML Script to a file and complements the --input-file option. changelog_begin - [DAML Script] ``daml script`` now has a `--output-file`` option that can be used to specify a file the result of the script should be written to. Similar to ``--input-file`` the result will be output in the DAML-LF JSON encoding. changelog_end
This commit is contained in:
parent
43def51fce
commit
7067fa432e
@ -192,6 +192,41 @@ packagingTests = testGroup "packaging"
|
||||
, "setup' = setup"
|
||||
]
|
||||
withCurrentDirectory (tmpDir </> "proj") $ callCommandSilent "daml build"
|
||||
, testCase "DAML Script --input-file and --output-file" $ withTempDir $ \projDir -> do
|
||||
writeFileUTF8 (projDir </> "daml.yaml") $ unlines
|
||||
[ "sdk-version: " <> sdkVersion
|
||||
, "name: proj"
|
||||
, "version: 0.0.1"
|
||||
, "source: ."
|
||||
, "dependencies: [daml-prim, daml-stdlib, daml-script]"
|
||||
]
|
||||
writeFileUTF8 (projDir </> "Main.daml") $ unlines
|
||||
[ "module Main where"
|
||||
, "import Daml.Script"
|
||||
, "test : Int -> Script (Int, Int)"
|
||||
, "test x = pure (x, x + 1)"
|
||||
]
|
||||
withCurrentDirectory projDir $ do
|
||||
callCommandSilent "daml build -o script.dar"
|
||||
writeFileUTF8 (projDir </> "input.json") "0"
|
||||
p :: Int <- fromIntegral <$> getFreePort
|
||||
withDevNull $ \devNull ->
|
||||
withCreateProcess (shell $ unwords ["daml sandbox --port " <> show p]) { std_out = UseHandle devNull } $ \_ _ _ _ -> do
|
||||
waitForConnectionOnPort (threadDelay 100000) p
|
||||
callCommand $ unwords
|
||||
[ "daml script"
|
||||
,"--wall-clock-time"
|
||||
, "--dar script.dar --script-name Main:test"
|
||||
, "--input-file input.json --output-file output.json"
|
||||
, "--ledger-host localhost --ledger-port " <> show p
|
||||
]
|
||||
contents <- readFileUTF8 (projDir </> "output.json")
|
||||
lines contents @?=
|
||||
[ "{"
|
||||
, " \"_1\": 0,"
|
||||
, " \"_2\": 1"
|
||||
, "}"
|
||||
]
|
||||
, testCase "Run init-script" $ withTempDir $ \tmpDir -> do
|
||||
let projDir = tmpDir </> "init-script-example"
|
||||
createDirectoryIfMissing True (projDir </> "daml")
|
||||
|
@ -20,6 +20,7 @@ case class RunnerConfig(
|
||||
timeProviderType: TimeProviderType,
|
||||
commandTtl: Duration,
|
||||
inputFile: Option[File],
|
||||
outputFile: Option[File],
|
||||
accessTokenFile: Option[Path],
|
||||
tlsConfig: Option[TlsConfiguration],
|
||||
jsonApi: Boolean,
|
||||
@ -83,6 +84,12 @@ object RunnerConfig {
|
||||
}
|
||||
.text("Path to a file containing the input value for the script in JSON format.")
|
||||
|
||||
opt[File]("output-file")
|
||||
.action { (t, c) =>
|
||||
c.copy(outputFile = Some(t))
|
||||
}
|
||||
.text("Path to a file where the result of the script will be written to in JSON format.")
|
||||
|
||||
opt[String]("access-token-file")
|
||||
.action { (f, c) =>
|
||||
c.copy(accessTokenFile = Some(Paths.get(f)))
|
||||
@ -159,6 +166,7 @@ object RunnerConfig {
|
||||
timeProviderType = null,
|
||||
commandTtl = Duration.ofSeconds(30L),
|
||||
inputFile = None,
|
||||
outputFile = None,
|
||||
accessTokenFile = None,
|
||||
tlsConfig = None,
|
||||
jsonApi = false,
|
||||
|
@ -9,6 +9,7 @@ import akka.stream._
|
||||
import java.nio.file.Files
|
||||
import java.time.Instant
|
||||
import java.util.stream.Collectors
|
||||
import scala.collection.JavaConverters._
|
||||
import scala.concurrent.{Await, ExecutionContext, Future}
|
||||
import scala.concurrent.duration.Duration
|
||||
import scala.io.Source
|
||||
@ -116,7 +117,14 @@ object RunnerMain {
|
||||
)
|
||||
Runner.connect(participantParams, clientConfig)
|
||||
}
|
||||
_ <- Runner.run(dar, scriptId, inputValue, clients, applicationId, timeProvider)
|
||||
result <- Runner.run(dar, scriptId, inputValue, clients, applicationId, timeProvider)
|
||||
_ <- Future {
|
||||
config.outputFile.foreach { outputFile =>
|
||||
val jsVal = LfValueCodec.apiValueToJsValue(
|
||||
result.toValue.assertNoRelCid(rcoid => s"Unexpected relative contract id $rcoid"))
|
||||
Files.write(outputFile.toPath, Seq(jsVal.prettyPrint).asJava)
|
||||
}
|
||||
}
|
||||
} yield ()
|
||||
|
||||
flow.onComplete(_ =>
|
||||
|
@ -191,6 +191,11 @@ We can then initialize our ledger passing in the json file via ``--input-file``.
|
||||
|
||||
If you open Navigator, you can now see the contracts that have been created.
|
||||
|
||||
While we will not use it here, there is also an ``--output-file``
|
||||
option that you can use to write the result of a script to a file
|
||||
using the DAML-LF JSON encoding. This is particularly useful if you need to consume
|
||||
the result from another program.
|
||||
|
||||
.. _script-ledger-initialization:
|
||||
|
||||
Using DAML Script for Ledger Initialization
|
||||
|
Loading…
Reference in New Issue
Block a user