aboutsummaryrefslogtreecommitdiffstats
path: root/src/command/register.rs
blob: c242e8abfb19413ca9437e3a5d6223bce01214ea (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
use std::fs::File;
use std::io;
use std::io::prelude::*;

use eyre::Result;
use structopt::StructOpt;

use atuin_client::api_client;
use atuin_client::settings::Settings;

#[derive(StructOpt)]
#[structopt(setting(structopt::clap::AppSettings::DeriveDisplayOrder))]
pub struct Cmd {
    #[structopt(long, short)]
    pub username: Option<String>,

    #[structopt(long, short)]
    pub email: Option<String>,

    #[structopt(long, short)]
    pub password: Option<String>,
}

fn get_input() -> Result<String> {
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    Ok(input.trim_end_matches(&['\r', '\n'][..]).to_string())
}

pub fn run(
    settings: &Settings,
    username: Option<String>,
    email: Option<String>,
    password: Option<String>,
) -> Result<()> {
    let username = if let Some(username) = username {
        username
    } else {
        eprint!("Please enter username: ");
        get_input().expect("Failed to read username from input")
    };

    let email = if let Some(email) = email {
        email
    } else {
        eprint!("Please enter email: ");
        get_input().expect("Failed to read email from input")
    };

    let password = if let Some(password) = password {
        password
    } else {
        eprint!("Please enter password: ");
        get_input().expect("Failed to read password from input")
    };

    let session = api_client::register(
        settings.sync_address.as_str(),
        username.as_str(),
        email.as_str(),
        password.as_str(),
    )?;

    let path = settings.session_path.as_str();
    let mut file = File::create(path)?;
    file.write_all(session.session.as_bytes())?;

    // Create a new key, and save it to disk
    let _key = atuin_client::encryption::new_key(settings)?;

    Ok(())
}