This commit is contained in:
parent
33599d888d
commit
54233b6dd5
5 changed files with 58 additions and 57 deletions
|
@ -7,16 +7,12 @@ repository = { type = "forgejo", host = "7cs.dev", user = "lily", repo = "gleam-
|
|||
|
||||
[dependencies]
|
||||
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
|
||||
snag = ">= 1.1.0 and < 2.0.0"
|
||||
gleam_http = ">= 4.0.0 and < 5.0.0"
|
||||
dot_env = ">= 1.2.0 and < 2.0.0"
|
||||
stratus = ">= 0.9.7 and < 1.0.0"
|
||||
gleam_httpc = ">= 4.2.0 and < 5.0.0"
|
||||
gleam_otp = ">= 0.16.1 and < 1.0.0"
|
||||
gleam_erlang = ">= 0.34.0 and < 1.0.0"
|
||||
gleam_json = ">= 3.0.1 and < 4.0.0"
|
||||
birl = ">= 1.8.0 and < 2.0.0"
|
||||
|
||||
[dev-dependencies]
|
||||
gleeunit = ">= 1.0.0 and < 2.0.0"
|
||||
oas_generator = ">= 1.3.3 and < 2.0.0"
|
||||
dot_env = ">= 1.2.0 and < 2.0.0"
|
||||
|
|
|
@ -8,38 +8,29 @@ packages = [
|
|||
{ name = "glam", version = "2.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "4932A2D139AB0389E149396407F89654928D7B815E212BB02F13C66F53B1BBA1" },
|
||||
{ name = "glance", version = "3.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "F3458292AFB4136CEE23142A8727C1270494E7A96978B9B9F9D2C1618583EF3D" },
|
||||
{ name = "glance_printer", version = "3.0.0", build_tools = ["gleam"], requirements = ["glam", "glance", "gleam_stdlib"], otp_app = "glance_printer", source = "hex", outer_checksum = "8386C156B413B90828884577F89AADD4C131AA1C3DAFC826607BAEE40E636ACE" },
|
||||
{ name = "gleam_crypto", version = "1.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "917BC8B87DBD584830E3B389CBCAB140FFE7CB27866D27C6D0FB87A9ECF35602" },
|
||||
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
|
||||
{ name = "gleam_http", version = "4.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "0A62451FC85B98062E0907659D92E6A89F5F3C0FBE4AB8046C99936BF6F91DBC" },
|
||||
{ name = "gleam_httpc", version = "4.2.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "5EE88F7AF296C09B578BAB4D5383F24AB3A9A7E9D4B4772D112A00405D1DC516" },
|
||||
{ name = "gleam_json", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "5BA154440B22D9800955B1AB854282FA37B97F30F409D76B0824D0A60C934188" },
|
||||
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
|
||||
{ name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" },
|
||||
{ name = "gleam_stdlib", version = "0.60.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "621D600BB134BC239CB2537630899817B1A42E60A1D46C5E9F3FAE39F88C800B" },
|
||||
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||
{ name = "gleeunit", version = "1.5.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D33B7736CF0766ED3065F64A1EBB351E72B2E8DE39BAFC8ADA0E35E92A6A934F" },
|
||||
{ name = "glexer", version = "2.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "5C235CBDF4DA5203AD5EAB1D6D8B456ED8162C5424FE2309CFFB7EF438B7C269" },
|
||||
{ name = "gramps", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "59194B3980110B403EE6B75330DB82CDE05FC8138491C2EAEACBC7AAEF30B2E8" },
|
||||
{ name = "justin", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "justin", source = "hex", outer_checksum = "7FA0C6DB78640C6DC5FBFD59BF3456009F3F8B485BF6825E97E1EB44E9A1E2CD" },
|
||||
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
|
||||
{ name = "oas", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_stdlib"], otp_app = "oas", source = "hex", outer_checksum = "01FF7BEDB4389084D96D11FB26902F14F75256FDAD812E3AE65C1E29B7BCA80E" },
|
||||
{ name = "oas_generator", version = "1.3.3", build_tools = ["gleam"], requirements = ["glance", "glance_printer", "gleam_http", "gleam_json", "gleam_stdlib", "justin", "oas", "simplifile", "snag"], otp_app = "oas_generator", source = "hex", outer_checksum = "9E2AD137E6883D99F0D7C515B42D4CFBA60A8B00DD3ED62ADE0DED29B84D8328" },
|
||||
{ name = "ranger", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_yielder"], otp_app = "ranger", source = "hex", outer_checksum = "C8988E8F8CDBD3E7F4D8F2E663EF76490390899C2B2885A6432E942495B3E854" },
|
||||
{ name = "simplifile", version = "2.2.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "C88E0EE2D509F6D86EB55161D631657675AA7684DAB83822F7E59EB93D9A60E3" },
|
||||
{ name = "snag", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "7E9F06390040EB5FAB392CE642771484136F2EC103A92AE11BA898C8167E6E17" },
|
||||
{ name = "stratus", version = "0.9.7", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "gramps", "logging"], otp_app = "stratus", source = "hex", outer_checksum = "4B3344B24B0808AF5828EFE3BEB824D3298E0ECAC8069D85D14A6A33BAD85CBD" },
|
||||
]
|
||||
|
||||
[requirements]
|
||||
birl = { version = ">= 1.8.0 and < 2.0.0" }
|
||||
dot_env = { version = ">= 1.2.0 and < 2.0.0" }
|
||||
gleam_erlang = { version = ">= 0.34.0 and < 1.0.0" }
|
||||
gleam_http = { version = ">= 4.0.0 and < 5.0.0" }
|
||||
gleam_httpc = { version = ">= 4.2.0 and < 5.0.0" }
|
||||
gleam_json = { version = ">= 3.0.1 and < 4.0.0" }
|
||||
gleam_otp = { version = ">= 0.16.1 and < 1.0.0" }
|
||||
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
|
||||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
|
||||
oas_generator = { version = ">= 1.3.3 and < 2.0.0" }
|
||||
snag = { version = ">= 1.1.0 and < 2.0.0" }
|
||||
stratus = { version = ">= 0.9.7 and < 1.0.0" }
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
import dot_env
|
||||
import dot_env/env
|
||||
import gleam/result
|
||||
import utils/auth.{type AccountToken, type AgentToken}
|
||||
|
||||
pub type Env {
|
||||
Env(account_token: AccountToken, agent_token: AgentToken)
|
||||
}
|
||||
|
||||
pub type EnvError {
|
||||
MissingVar(name: String)
|
||||
InvalidVar(name: String)
|
||||
}
|
||||
|
||||
fn get_string(name: String) -> Result(String, EnvError) {
|
||||
env.get_string(name) |> result.replace_error(MissingVar(name))
|
||||
}
|
||||
|
||||
pub fn load() -> Result(Env, EnvError) {
|
||||
dot_env.default |> dot_env.load
|
||||
use account_token <- result.try(
|
||||
get_string("ACCOUNT_TOKEN")
|
||||
|> result.then(fn(token_str) {
|
||||
auth.parse_account_token(token_str)
|
||||
|> result.replace_error(InvalidVar("ACCOUNT_TOKEN"))
|
||||
}),
|
||||
)
|
||||
use agent_token <- result.try(
|
||||
get_string("AGENT_TOKEN")
|
||||
|> result.then(fn(token_str) {
|
||||
auth.parse_agent_token(token_str)
|
||||
|> result.replace_error(InvalidVar("AGENT_TOKEN"))
|
||||
}),
|
||||
)
|
||||
Ok(Env(account_token:, agent_token:))
|
||||
}
|
||||
|
||||
pub fn load_unsafe() -> Env {
|
||||
let assert Ok(env) = load() as "Failed to load environment variables"
|
||||
env
|
||||
}
|
55
test/env.gleam
Normal file
55
test/env.gleam
Normal file
|
@ -0,0 +1,55 @@
|
|||
import dot_env
|
||||
import dot_env/env
|
||||
import gleam/result
|
||||
import utils/auth.{type AccountToken, type AgentToken}
|
||||
|
||||
pub type Env {
|
||||
Env(account_token: AccountToken, agent_token: AgentToken)
|
||||
}
|
||||
|
||||
pub type EnvError {
|
||||
MissingVar(name: String)
|
||||
InvalidVar(name: String)
|
||||
}
|
||||
|
||||
pub fn get_account_token() -> Result(AccountToken, EnvError) {
|
||||
env.get_string("ACCOUNT_TOKEN")
|
||||
|> result.replace_error(MissingVar("ACCOUNT_TOKEN"))
|
||||
|> result.then(fn(value) {
|
||||
auth.parse_account_token(value)
|
||||
|> result.replace_error(InvalidVar("ACCOUNT_TOKEN"))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_agent_token() -> Result(AgentToken, EnvError) {
|
||||
env.get_string("AGENT_TOKEN")
|
||||
|> result.replace_error(MissingVar("AGENT_TOKEN"))
|
||||
|> result.then(fn(value) {
|
||||
auth.parse_agent_token(value)
|
||||
|> result.replace_error(InvalidVar("AGENT_TOKEN"))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn load_env() -> Result(Env, EnvError) {
|
||||
use account_token <- result.try(get_account_token())
|
||||
use agent_token <- result.try(get_agent_token())
|
||||
Ok(Env(account_token:, agent_token:))
|
||||
}
|
||||
|
||||
pub fn load_dotenv() -> Result(Env, EnvError) {
|
||||
dot_env.load_default()
|
||||
load_env()
|
||||
}
|
||||
|
||||
pub fn load_env_unsafe() -> Env {
|
||||
let assert Ok(account_token) = get_account_token()
|
||||
as "Missing or invalid account token"
|
||||
let assert Ok(agent_token) = get_agent_token()
|
||||
as "Missing or invalid agent token"
|
||||
Env(account_token:, agent_token:)
|
||||
}
|
||||
|
||||
pub fn load_dotenv_unsafe() -> Env {
|
||||
dot_env.load_default()
|
||||
load_env_unsafe()
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
import endpoints/accounts
|
||||
import endpoints/global
|
||||
import env
|
||||
import gleeunit
|
||||
import utils/env
|
||||
|
||||
pub fn main() -> Nil {
|
||||
gleeunit.main()
|
||||
|
@ -16,5 +16,5 @@ pub fn error_codes_test() {
|
|||
}
|
||||
|
||||
pub fn account_test() {
|
||||
let assert Ok(_) = accounts.get_account(env.load_unsafe().agent_token)
|
||||
let assert Ok(_) = accounts.get_account(env.load_dotenv_unsafe().agent_token)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue