File size: 1,748 Bytes
9552aa0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use clap::Parser;
use mimalloc::MiMalloc;
use password_auth::generate_hash;
use rpassword::prompt_password;
use std::process;
use yaml_rust2::{yaml, Yaml, YamlEmitter};

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

/// A password tool for Ferron
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
  /// The username, for which you want to generate an user entry
  #[arg()]
  username: String,
}

fn main() {
  let args = Args::parse();

  let password = match prompt_password("Password: ") {
    Ok(pass) => pass,
    Err(e) => {
      eprintln!("Error reading password: {}", e);
      process::exit(1);
    }
  };
  let password2 = match prompt_password("Confirm password: ") {
    Ok(pass) => pass,
    Err(e) => {
      eprintln!("Error reading password confirmation: {}", e);
      process::exit(1);
    }
  };

  if password != password2 {
    eprintln!("Passwords don't match!");
    process::exit(1);
  }

  let password_hash = generate_hash(password);

  let mut yaml_user_hashmap = yaml::Hash::new();
  yaml_user_hashmap.insert(
    Yaml::String("name".to_string()),
    Yaml::String(args.username),
  );
  yaml_user_hashmap.insert(
    Yaml::String("pass".to_string()),
    Yaml::String(password_hash),
  );

  let yaml_data = Yaml::Array(vec![Yaml::Hash(yaml_user_hashmap)]);

  let mut output = String::new();
  if let Err(e) = YamlEmitter::new(&mut output).dump(&yaml_data) {
    eprintln!("Error generating YAML output: {}", e);
    process::exit(1);
  }

  println!("Copy the user object below into \"users\" property of either global configuration or a virtual host in the \"ferron.yaml\" file. Remember about the indentation in the server configuration.");
  println!("{}", output);
}