upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/ngit/sub_commands/create.rs
blob: 1c2b8db39464aca05b09cfde5eded500f6225666 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use anyhow::{Context, Result};
use clap::Parser;
use ngit::client::Params;
use nostr_sdk::ToBech32;

use crate::{
    cli::Cli,
    client::{Client, Connect},
    git::Repo,
    login::fresh::signup_non_interactive,
};

#[derive(Parser)]
pub struct SubCommandArgs {
    /// Display name for the new account
    #[arg(long, required = true)]
    pub name: String,

    /// Relay URLs for the new account's relay list (can be specified multiple
    /// times). Defaults to the relay-default-set if not provided.
    #[arg(long = "relay", value_parser, num_args = 1)]
    pub relays: Vec<String>,

    /// Don't publish metadata to relays (offline mode)
    #[arg(long)]
    pub offline: bool,

    /// Save credentials only to local git config
    #[arg(long)]
    pub local: bool,
}

pub async fn launch(_cli: &Cli, args: &SubCommandArgs) -> Result<()> {
    let git_repo = Repo::discover().ok();

    let params = Params::with_git_config_relay_defaults(&git_repo.as_ref());

    let relay_urls = if args.relays.is_empty() {
        params.relay_default_set.clone()
    } else {
        args.relays.clone()
    };

    let client = if args.offline {
        None
    } else {
        Some(Client::new(params))
    };

    let publish = !args.offline;

    let (_signer, public_key, _signer_info, keys) = signup_non_interactive(
        args.name.clone(),
        client.as_ref(),
        args.local,
        publish,
        relay_urls,
    )
    .await
    .context("failed to create account")?;

    // Display the generated nsec prominently
    println!("\n✓ Account created successfully!");
    println!("\nDisplay name: {}", args.name);
    println!("Public key (npub): {}", public_key.to_bech32()?);
    println!("\n⚠️  IMPORTANT: Save your secret key (nsec) securely!");
    println!("nsec: {}", keys.secret_key().to_bech32()?);
    println!("\nYou will need this key to log in from other devices.");
    println!("Run 'ngit account export-keys' to see this again.\n");

    if publish {
        println!("✓ Published metadata to relays");
    }

    if args.local {
        println!("✓ Saved credentials to local git config only");
    } else {
        println!("✓ Saved credentials to global git config");
    }

    // Disconnect client if it was created
    if let Some(client) = client {
        client.disconnect().await?;
    }

    Ok(())
}