added configuration

This commit is contained in:
Ivan Yuriev 2024-12-09 20:46:31 +03:00
parent 2202a62604
commit f307747c76
6 changed files with 87 additions and 5 deletions

View File

@ -14,10 +14,12 @@ version = "1.0.0"
[dependencies]
gleam_stdlib = ">= 0.45.0 and < 2.0.0"
gleam_otp = ">= 0.14.1 and < 1.0.0"
gleam_otp = ">= 0.15.0 and < 1.0.0"
gleam_json = ">= 2.1.0 and < 3.0.0"
simplifile = ">= 2.2.0 and < 3.0.0"
decode = ">= 0.5.0 and < 1.0.0"
glisten = ">= 7.0.0 and < 8.0.0"
gleam_erlang = ">= 0.33.0 and < 1.0.0"
[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"

View File

@ -5,20 +5,25 @@ packages = [
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
{ name = "decode", version = "0.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "decode", source = "hex", outer_checksum = "05E14DC95A550BA51B8774485B04894B87A898C588B9B1C920104B110AED218B" },
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
{ name = "gleam_erlang", version = "0.32.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "B18643083A0117AC5CFD0C1AEEBE5469071895ECFA426DCC26517A07F6AD9948" },
{ name = "gleam_erlang", version = "0.33.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "A1D26B80F01901B59AABEE3475DD4C18D27D58FA5C897D922FCB9B099749C064" },
{ name = "gleam_json", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "0A57FB5666E695FD2BEE74C0428A98B0FC11A395D2C7B4CDF5E22C5DD32C74C6" },
{ name = "gleam_otp", version = "0.14.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5A8CE8DBD01C29403390A7BD5C0A63D26F865C83173CF9708E6E827E53159C65" },
{ name = "gleam_otp", version = "0.15.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "E9ED3DF7E7285DA0C440F46AE8236ADC8475E8CCBEE4899BF09A8468DA3F9187" },
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "glisten", version = "7.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "028C0882EAC7ABEDEFBE92CE4D1FEDADE95FA81B1B1AB099C4F91C133BEF2C42" },
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
{ name = "ranger", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "B8F3AFF23A3A5B5D9526B8D18E7C43A7DFD3902B151B97EC65397FE29192B695" },
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
{ name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" },
]
[requirements]
birl = { version = ">= 1.7.1 and < 2.0.0" }
decode = { version = ">= 0.5.0 and < 1.0.0" }
gleam_erlang = { version = ">= 0.33.0 and < 1.0.0" }
gleam_json = { version = ">= 2.1.0 and < 3.0.0" }
gleam_otp = { version = ">= 0.14.1 and < 1.0.0" }
gleam_otp = { version = ">= 0.15.0 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.45.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
glisten = { version = ">= 7.0.0 and < 8.0.0" }
simplifile = { version = ">= 2.2.0 and < 3.0.0" }

56
src/config.gleam Normal file
View File

@ -0,0 +1,56 @@
import gleam/io
import gleam/result
import gleam/string
import json_serde
import simplifile
import tree
pub type Config {
Config(snapshots_path: String, port: Int)
}
fn default() -> Config {
Config(snapshots_path: "snapshots", port: 8080)
}
pub fn load(path) -> Config {
case simplifile.read(path) {
Error(err) -> {
{ "Could not load configuration: " <> { err |> string.inspect } }
|> io.println
"Using the default config." |> io.println
default()
}
Ok(data) -> {
let config_tree = json_serde.deserialize(data)
case config_tree {
Error(_) -> handle_malformed()
Ok(tree) -> {
let snapshots_path = tree |> tree.get(["snapshots_path"])
let port = tree |> tree.get(["port"])
let res = result.all([snapshots_path, port])
case res {
Error(_) -> handle_malformed()
Ok(values) ->
case values {
[
tree.Leaf(tree.String(snapshots_path)),
tree.Leaf(tree.Int(port)),
] -> Config(snapshots_path, port)
_ -> handle_malformed()
}
}
}
}
}
}
}
fn handle_malformed() {
"Malformed configuration file."
|> io.println
"Using the default config." |> io.println
default()
}

View File

@ -1,3 +1,6 @@
import config
import gleam/io
pub fn main() {
todo
config.load("./config.json") |> io.debug
}

12
test/config_test.gleam Normal file
View File

@ -0,0 +1,12 @@
import config
import gleeunit
import gleeunit/should
pub fn main() {
gleeunit.main()
}
pub fn config_test() {
let config = config.load("./test/vault.json")
should.equal(config.Config("./snapshots", 12_345), config)
}

4
test/vault.json Normal file
View File

@ -0,0 +1,4 @@
{
"snapshots_path": "./snapshots",
"port": 12345
}