diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2024-04-24 11:27:42 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2024-04-24 11:27:42 +0200 |
commit | a4dbf815cc418514a268ca54464c2d1b9c413055 (patch) | |
tree | 81d6a5944a00839f37c7d9ea469274f8d8425338 /cli/src/main.rs | |
parent | 1c3e4dba9fe90faa318933a99eecdb9cd5e7481a (diff) | |
download | faktura-a4dbf815cc418514a268ca54464c2d1b9c413055.tar.gz faktura-a4dbf815cc418514a268ca54464c2d1b9c413055.tar.bz2 faktura-a4dbf815cc418514a268ca54464c2d1b9c413055.zip |
cli: Add feature to save clients to the db.
Also introduces a Connection object to handle the actual connection to
the remote API. We also load the remote API url and jwt token from the
process environment so these are no longer hardcoded into the source
code.
Diffstat (limited to 'cli/src/main.rs')
-rw-r--r-- | cli/src/main.rs | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs index 4c34e18..ee10d17 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -7,11 +7,36 @@ mod api; -use api::Client; +use api::{Client, Connection}; fn main() -> Result<(), Box<dyn std::error::Error>> { - let clients = Client::all()?; + let conn = Connection::new( + std::env::var("FAKTURA_API_URL")?, + std::env::var("FAKTURA_JWT_TOKEN")? + ); + + let mut args = std::env::args().skip(1); + if let Some(cmd) = args.next() { + match cmd.as_str() { + "--list-clients" => { + list_clients(conn)?; + }, + "--add-client" => { + add_client(conn, &args.next() + .expect("expected new client json data"))?; + }, + &_ => { + println!("Unknown command: {}", cmd); + } + } + } + + Ok(()) +} + +fn list_clients(conn: Connection) -> Result<(), Box<dyn std::error::Error>> { + let clients = Client::all(conn)?; for c in clients { print!("{}: {} <{}>", c.id, c.name, c.email); @@ -32,3 +57,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { Ok(()) } + +fn add_client(conn: Connection, json: &str) -> Result<(), Box<dyn std::error::Error>> { + let client: Client = serde_json::from_str(json)?; + println!("{:?}", client); + client.save(conn) +} |