summaryrefslogtreecommitdiff
path: root/examples/todo/main.rs
blob: 0832f68e70d666b3e9c56647b7e4c1b5f7e05c30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
mod commands;

use anyhow::{Context, Result};
use clap::Parser;
use notion::ids::DatabaseId;
use notion::NotionApi;
use serde::{Deserialize, Serialize};

// From <https://docs.rs/clap/3.0.0-beta.2/clap/>
#[derive(Parser, Debug)]
#[clap(version = "1.0", author = "Jake Swenson")]
struct Opts {
    #[clap(subcommand)]
    command: SubCommand,
}

#[derive(Parser, Debug)]
enum SubCommand {
    /// Configure what database this notion-todo example uses
    Config,
    /// List all todos
    List,
    /// Add a todo item to the notion database
    Add,
    /// Complete a todo item
    Check,
}

#[derive(Deserialize, Serialize)]
struct TodoConfig {
    api_token: Option<String>,
    task_database_id: Option<DatabaseId>,
}

#[tokio::main]
async fn main() -> Result<()> {
    let opts: Opts = Opts::parse();

    // https://docs.rs/config/0.11.0/config/
    let config = config::Config::default()
        .with_merged(config::File::with_name("todo_config"))
        .unwrap_or_default()
        .with_merged(config::Environment::with_prefix("NOTION"))?;

    let config: TodoConfig = config.try_into().context("Failed to read config")?;

    let notion_api = NotionApi::new(
        std::env::var("NOTION_API_TOKEN")
            .or(config
                .api_token
                .ok_or(anyhow::anyhow!("No api token from config")))
            .context(
                "No Notion API token found in either the environment variable \
                        `NOTION_API_TOKEN` or the config file!",
            )?,
    )?;

    match opts.command {
        SubCommand::Config => commands::configure::configure(notion_api).await,
        SubCommand::List => list_tasks(notion_api),
        SubCommand::Add => add_task(notion_api),
        SubCommand::Check => complete_task(notion_api),
    }
}

fn list_tasks(_notion_api: NotionApi) -> Result<()> {
    Ok(())
}

fn add_task(_notion_api: NotionApi) -> Result<()> {
    Ok(())
}

fn complete_task(_notion_api: NotionApi) -> Result<()> {
    Ok(())
}