Skip to content

Commit 259461d

Browse files
committed
Move auth functions to a separate module
1 parent 463deff commit 259461d

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

src/command/admin.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,6 @@ pub fn add_user(args: &AddUserArgs) -> Result<()> {
6060
.print()
6161
}
6262

63-
#[derive(Args)]
64-
pub struct SignupArgs {
65-
pub name: String,
66-
pub password: String,
67-
}
68-
69-
pub fn sign_up(args: &SignupArgs) -> Result<()> {
70-
rpc::call(
71-
"add_user",
72-
json!({"name": args.name, "password": args.password}),
73-
)?
74-
.print()?;
75-
let res = rpc::call(
76-
"create_api_key",
77-
json!({"username": args.name, "password": args.password, "label": "Created by btcmap-cli during signup"}),
78-
)?;
79-
let res = res.result.unwrap();
80-
let api_key = res["token"].as_str().unwrap();
81-
settings::put_str("password", api_key)?;
82-
println!("You are now logged in as {}", args.name);
83-
Ok(())
84-
}
85-
8663
#[derive(Args)]
8764
pub struct ChangePasswordArgs {
8865
pub username: String,

src/command/auth.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::{rpc, settings, Result};
2+
use clap::Args;
3+
use serde_json::json;
4+
5+
#[derive(Args)]
6+
pub struct SignUpArgs {
7+
pub name: String,
8+
pub password: String,
9+
}
10+
11+
pub fn sign_up(args: &SignUpArgs) -> Result<()> {
12+
rpc::call(
13+
"add_user",
14+
json!({"name": args.name, "password": args.password}),
15+
)?
16+
.print()?;
17+
let res = rpc::call(
18+
"create_api_key",
19+
json!({"username": args.name, "password": args.password, "label": "Created by btcmap-cli during signup"}),
20+
)?;
21+
let res = res.result.unwrap();
22+
let api_key = res["token"].as_str().unwrap();
23+
settings::put_str("password", api_key)?;
24+
println!("You are now logged in as {}", args.name);
25+
Ok(())
26+
}

src/command/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod admin;
22
pub mod area;
3+
pub mod auth;
34
pub mod common;
45
pub mod element;
56
pub mod event;

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ enum Commands {
5757

5858
// Auth - https://github.com/teambtcmap/btcmap-api/blob/master/docs/rpc-api/auth.md
5959
/// Sign up and get an auth token
60-
Signup(command::admin::SignupArgs),
60+
Signup(command::auth::SignUpArgs),
6161
/// Change admin password. Knowledge of an old password is required
6262
ChangePassword(command::admin::ChangePasswordArgs),
6363
/// Create API key. You need to provide your username and password, as well as a key label
@@ -173,7 +173,7 @@ fn main() -> Result<()> {
173173
Commands::GenerateElementIcons(args) => element::generate_element_icons(args),
174174
Commands::GenerateElementCategories(args) => element::generate_element_categories(args),
175175
// Admin
176-
Commands::Signup(args) => command::admin::sign_up(args),
176+
Commands::Signup(args) => command::auth::sign_up(args),
177177
Commands::AddUser(args) => command::admin::add_user(args),
178178
Commands::AddAdminAction(args) => command::admin::add_admin_action(args),
179179
Commands::RemoveAdminAction(args) => command::admin::remove_admin_action(args),

0 commit comments

Comments
 (0)