summaryrefslogtreecommitdiffstats
path: root/cli/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/src/main.rs')
-rw-r--r--cli/src/main.rs35
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)
+}