aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/cli.rs
blob: e1eeac32eb1a889b19765b72c3f0d787fc716998 (plain)
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
extern crate planetwars_server;
extern crate tokio;

use clap::Parser;
use planetwars_server::db;
use planetwars_server::{create_db_pool, get_config};

#[derive(clap::Parser)]
struct Args {
    #[clap(subcommand)]
    action: Action,
}

#[derive(clap::Subcommand)]
enum Action {
    SetPassword(SetPassword),
}

impl Action {
    async fn run(self) {
        match self {
            Action::SetPassword(set_password) => set_password.run().await,
        }
    }
}

#[derive(clap::Parser)]
struct SetPassword {
    #[clap(value_parser)]
    username: String,

    #[clap(value_parser)]
    new_password: String,
}

impl SetPassword {
    async fn run(self) {
        let global_config = get_config().unwrap();
        let pool = create_db_pool(&global_config).await;

        let mut conn = pool.get().await.expect("could not get database connection");
        let credentials = db::users::Credentials {
            username: &self.username,
            password: &self.new_password,
        };
        db::users::set_user_password(credentials, &mut conn).expect("could not set password");
    }
}

#[tokio::main]
pub async fn main() {
    let args = Args::parse();
    args.action.run().await;
}