summaryrefslogblamecommitdiffstats
path: root/cli/src/main.rs
blob: ee10d176e4553e6e67ba4c4117e896fca035e41c (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                   







                                                       
                              


                                                     

























                                                                             



















                                                     





                                                                                       
// 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"))?;
            },
            &_ => {
                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)
}