2023-07-05 18:17:03 +00:00
|
|
|
# Nimbus
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-07-05 18:17:03 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
|
|
|
std/[strutils, os],
|
|
|
|
chronicles,
|
|
|
|
stew/io2,
|
2023-07-08 15:01:33 +00:00
|
|
|
faststreams,
|
2024-02-28 17:31:45 +00:00
|
|
|
json_serialization,
|
|
|
|
json_serialization/std/tables,
|
2024-05-13 10:34:14 +00:00
|
|
|
../../eth_data/history_data_json_store,
|
|
|
|
../../eth_data/yaml_utils
|
2023-07-05 18:17:03 +00:00
|
|
|
|
2023-07-08 15:01:33 +00:00
|
|
|
export history_data_json_store
|
2023-07-05 18:17:03 +00:00
|
|
|
|
|
|
|
proc writePortalContentToJson*(
|
2024-02-28 17:31:45 +00:00
|
|
|
fh: OutputStreamHandle, content: JsonPortalContentTable
|
|
|
|
) =
|
2023-07-05 18:17:03 +00:00
|
|
|
try:
|
|
|
|
var writer = JsonWriter[DefaultFlavor].init(fh.s, pretty = true)
|
|
|
|
writer.writeValue(content)
|
|
|
|
except IOError as e:
|
|
|
|
fatal "Error occured while writing to file", error = e.msg
|
|
|
|
quit 1
|
|
|
|
|
2024-05-13 10:34:14 +00:00
|
|
|
proc writePortalContentToYaml*(file: string, contentKey: string, contentValue: string) =
|
|
|
|
let
|
|
|
|
yamlPortalContent =
|
|
|
|
YamlPortalContent(content_key: contentKey, content_value: contentValue)
|
|
|
|
res = yamlPortalContent.dumpToYaml(file)
|
|
|
|
if res.isErr():
|
|
|
|
error "Failed writing content to file", file, error = res.error
|
|
|
|
quit 1
|
|
|
|
else:
|
|
|
|
notice "Successfully wrote content to file", file
|
|
|
|
|
2023-07-05 18:17:03 +00:00
|
|
|
proc createAndOpenFile*(dataDir: string, fileName: string): OutputStreamHandle =
|
|
|
|
# Creates directory and file, if file already exists
|
|
|
|
# program is aborted with info to user, to avoid losing data
|
|
|
|
let fileName: string =
|
|
|
|
if not fileName.endsWith(".json"):
|
|
|
|
fileName & ".json"
|
|
|
|
else:
|
|
|
|
fileName
|
|
|
|
|
|
|
|
let filePath = dataDir / fileName
|
|
|
|
|
|
|
|
if isFile(filePath):
|
|
|
|
fatal "File under provided path already exists and would be overwritten",
|
|
|
|
path = filePath
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
let res = createPath(dataDir)
|
|
|
|
if res.isErr():
|
2024-02-28 17:31:45 +00:00
|
|
|
fatal "Error occurred while creating directory", error = ioErrorMsg(res.error)
|
2023-07-05 18:17:03 +00:00
|
|
|
quit 1
|
|
|
|
|
|
|
|
try:
|
|
|
|
return fileOutput(filePath)
|
|
|
|
except IOError as e:
|
|
|
|
fatal "Error occurred while opening the file", error = e.msg
|
|
|
|
quit 1
|
2024-05-13 10:34:14 +00:00
|
|
|
|
|
|
|
proc existsFile*(dataDir: string, fileName: string) =
|
|
|
|
let filePath = dataDir / fileName
|
|
|
|
|
|
|
|
if isFile(filePath):
|
|
|
|
fatal "File under provided path already exists and would be overwritten",
|
|
|
|
path = filePath
|
|
|
|
quit 1
|