// Command line app for the fatura invoicing system
//
// SPDX-FileCopyrightText: 2024 Eilertsens Kodeknekkeri
// SPDX-FileCopyrightText: 2024 Harald Eilertsen
//
// SPDX-License-Identifier: AGPL-3.0-or-later
mod api;
use api::{Client, Connection};
fn main() -> Result<(), Box<dyn std::error::Error>> {
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"))?;
},
"--edit-client" => {
let client_id = args.next().expect("missing client id").parse()?;
let json = args.next().expect("missing json data");
Client::update(conn, client_id, &json)?;
},
&_ => {
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);
if let Some(contact) = c.contact {
print!(", c/o {}", contact);
}
if let Some(address) = c.address {
print!(", {}", address);
}
if let Some(phone) = c.phone {
print!(", ph: {}", phone);
}
if c.vat {
print!(", VAT");
}
println!("");
}
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)
}