feat: add si command line interface
#37
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- Add a `si` command line interface with `whoami`, `signup`, `login` and `logout` that uses the same identity, data and keychain as the app without the app running
|
||||||
- Add a confirmation dialog before deleting a repository
|
- Add a confirmation dialog before deleting a repository
|
||||||
- Browse the files of a commit as of that commit from the Commits list, the short id on a row shows the commit's files in place of the worktree and the branch selector shows the commit until a branch or tag is picked again
|
- Browse the files of a commit as of that commit from the Commits list, the short id on a row shows the commit's files in place of the worktree and the branch selector shows the commit until a branch or tag is picked again
|
||||||
|
|
||||||
|
|||||||
Generated
+127
-1
@@ -166,12 +166,56 @@ version = "0.1.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
|
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstream"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"anstyle-parse",
|
||||||
|
"anstyle-query",
|
||||||
|
"anstyle-wincon",
|
||||||
|
"colorchoice",
|
||||||
|
"is_terminal_polyfill",
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstyle"
|
name = "anstyle"
|
||||||
version = "1.0.14"
|
version = "1.0.14"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-parse"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
|
||||||
|
dependencies = [
|
||||||
|
"utf8parse",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-query"
|
||||||
|
version = "1.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anstyle-wincon"
|
||||||
|
version = "3.0.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||||
|
dependencies = [
|
||||||
|
"anstyle",
|
||||||
|
"once_cell_polyfill",
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.105"
|
version = "1.0.105"
|
||||||
@@ -998,6 +1042,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "50066ca966813ca8957ee15361752f19424a8ff620dbe9c8993d9f99d99b04b3"
|
checksum = "50066ca966813ca8957ee15361752f19424a8ff620dbe9c8993d9f99d99b04b3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
|
"clap_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1006,8 +1051,22 @@ version = "4.6.10"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a0ed97c96ef4efb3cc84e09fa388df4463e7cc3224ec38d7b1d9e45749333590"
|
checksum = "a0ed97c96ef4efb3cc84e09fa388df4463e7cc3224ec38d7b1d9e45749333590"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anstream",
|
||||||
"anstyle",
|
"anstyle",
|
||||||
"clap_lex",
|
"clap_lex",
|
||||||
|
"strsim",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_derive"
|
||||||
|
version = "4.6.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b4c174f11f949b9ca8c60580925e282b9cbd5454750e506f08648b2038080cd7"
|
||||||
|
dependencies = [
|
||||||
|
"heck 0.5.0",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 3.0.10",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1067,6 +1126,12 @@ version = "1.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
|
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colorchoice"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "combine"
|
name = "combine"
|
||||||
version = "4.6.9"
|
version = "4.6.9"
|
||||||
@@ -4187,6 +4252,12 @@ version = "2.12.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0"
|
checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "is_terminal_polyfill"
|
||||||
|
version = "1.70.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
@@ -5487,6 +5558,12 @@ version = "1.21.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell_polyfill"
|
||||||
|
version = "1.70.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "oo7"
|
name = "oo7"
|
||||||
version = "0.6.0"
|
version = "0.6.0"
|
||||||
@@ -6570,6 +6647,27 @@ dependencies = [
|
|||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rpassword"
|
||||||
|
version = "7.5.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fa2c5d9304b618df61ff17f3d3485c6e1854a3d94902a49abbb8d43dfbe0464"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"rtoolbox",
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rtoolbox"
|
||||||
|
version = "0.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a1efe12a1469752d0e6ff5ebec0b6ef4924cc5c4c71046b0ec730040535819d"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust-embed"
|
name = "rust-embed"
|
||||||
version = "8.14.0"
|
version = "8.14.0"
|
||||||
@@ -7105,7 +7203,6 @@ dependencies = [
|
|||||||
"gpui",
|
"gpui",
|
||||||
"gpui_platform",
|
"gpui_platform",
|
||||||
"metadata",
|
"metadata",
|
||||||
"paths",
|
|
||||||
"reqwest_client",
|
"reqwest_client",
|
||||||
"rust-embed",
|
"rust-embed",
|
||||||
"settings",
|
"settings",
|
||||||
@@ -7136,6 +7233,22 @@ dependencies = [
|
|||||||
"utils",
|
"utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "signed_cli"
|
||||||
|
version = "0.2.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"clap",
|
||||||
|
"flume 0.11.1",
|
||||||
|
"gpui",
|
||||||
|
"gpui_platform",
|
||||||
|
"nostr",
|
||||||
|
"rpassword",
|
||||||
|
"signed_state",
|
||||||
|
"tracing",
|
||||||
|
"tracing-subscriber",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "signed_core"
|
name = "signed_core"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
@@ -7190,6 +7303,7 @@ dependencies = [
|
|||||||
"nostr",
|
"nostr",
|
||||||
"nostr-connect",
|
"nostr-connect",
|
||||||
"nostr-sdk",
|
"nostr-sdk",
|
||||||
|
"paths",
|
||||||
"rustls",
|
"rustls",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"settings",
|
"settings",
|
||||||
@@ -7380,6 +7494,12 @@ dependencies = [
|
|||||||
"float-cmp",
|
"float-cmp",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strum"
|
name = "strum"
|
||||||
version = "0.28.0"
|
version = "0.28.0"
|
||||||
@@ -8324,6 +8444,12 @@ dependencies = [
|
|||||||
"bytes",
|
"bytes",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utf8parse"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "utf_types"
|
name = "utf_types"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
[package]
|
||||||
|
name = "signed_cli"
|
||||||
|
version.workspace = true
|
||||||
|
edition.workspace = true
|
||||||
|
publish.workspace = true
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "si"
|
||||||
|
path = "src/main.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
signed_state = { path = "../signed_state" }
|
||||||
|
|
||||||
|
gpui.workspace = true
|
||||||
|
gpui_platform.workspace = true
|
||||||
|
anyhow.workspace = true
|
||||||
|
clap = { version = "4", features = ["derive"] }
|
||||||
|
flume.workspace = true
|
||||||
|
nostr.workspace = true
|
||||||
|
rpassword = "7"
|
||||||
|
tracing.workspace = true
|
||||||
|
tracing-subscriber.workspace = true
|
||||||
|
|
||||||
|
[lints]
|
||||||
|
workspace = true
|
||||||
@@ -0,0 +1,313 @@
|
|||||||
|
use std::io::Write as _;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use anyhow::{Context as _, bail};
|
||||||
|
use clap::{Parser, Subcommand};
|
||||||
|
use gpui::{App, AsyncApp, Entity, Subscription};
|
||||||
|
use nostr::prelude::{PublicKey, ToBech32 as _};
|
||||||
|
use signed_state::{Backend, BackendEvent};
|
||||||
|
|
||||||
|
/// Command line interface for Signed.
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(name = "si", version, about)]
|
||||||
|
struct Cli {
|
||||||
|
#[command(subcommand)]
|
||||||
|
command: Command,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum Command {
|
||||||
|
/// Create a new identity and store it on this device.
|
||||||
|
Signup {
|
||||||
|
/// Name published in the profile.
|
||||||
|
name: String,
|
||||||
|
/// Read the passphrase from this file instead of prompting.
|
||||||
|
#[arg(long, value_name = "PATH")]
|
||||||
|
passphrase_file: Option<PathBuf>,
|
||||||
|
},
|
||||||
|
/// Store an existing identity on this device.
|
||||||
|
Login {
|
||||||
|
/// Read the credential (nsec, ncryptsec or bunker URI) from this file instead of prompting.
|
||||||
|
#[arg(long, value_name = "PATH")]
|
||||||
|
credential_file: Option<PathBuf>,
|
||||||
|
/// Read the passphrase from this file instead of prompting.
|
||||||
|
#[arg(long, value_name = "PATH")]
|
||||||
|
passphrase_file: Option<PathBuf>,
|
||||||
|
},
|
||||||
|
/// Print the identity stored on this device.
|
||||||
|
Whoami {
|
||||||
|
/// Read the passphrase from this file instead of prompting.
|
||||||
|
#[arg(long, value_name = "PATH")]
|
||||||
|
passphrase_file: Option<PathBuf>,
|
||||||
|
},
|
||||||
|
/// Remove the identity stored on this device.
|
||||||
|
Logout,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
tracing_subscriber::fmt()
|
||||||
|
.with_writer(std::io::stderr)
|
||||||
|
.with_max_level(tracing::Level::WARN)
|
||||||
|
.init();
|
||||||
|
|
||||||
|
let cli = Cli::parse();
|
||||||
|
|
||||||
|
gpui_platform::headless().run(move |cx| {
|
||||||
|
// The callback runs before the platform's run loop, so the command awaits inside a foreground task.
|
||||||
|
cx.spawn(async move |cx| {
|
||||||
|
let mut code = 0;
|
||||||
|
|
||||||
|
if let Err(error) = run(cli.command, cx).await {
|
||||||
|
eprintln!("error: {error:#}");
|
||||||
|
code = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(error) = std::io::stdout().flush() {
|
||||||
|
eprintln!("error: failed to write output: {error}");
|
||||||
|
code = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quitting the platform loop does not carry an exit code, so the process ends here.
|
||||||
|
std::process::exit(code);
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(command: Command, cx: &mut AsyncApp) -> anyhow::Result<()> {
|
||||||
|
match command {
|
||||||
|
Command::Signup {
|
||||||
|
name,
|
||||||
|
passphrase_file,
|
||||||
|
} => signup(&name, passphrase_file.as_deref(), cx).await,
|
||||||
|
Command::Login {
|
||||||
|
credential_file,
|
||||||
|
passphrase_file,
|
||||||
|
} => login(credential_file.as_deref(), passphrase_file.as_deref(), cx).await,
|
||||||
|
Command::Whoami { passphrase_file } => whoami(passphrase_file.as_deref(), cx).await,
|
||||||
|
Command::Logout => logout(cx).await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Opens the stores shared with the desktop app and starts forwarding the backend events.
|
||||||
|
fn open(
|
||||||
|
cx: &mut AsyncApp,
|
||||||
|
) -> anyhow::Result<(Entity<Backend>, flume::Receiver<BackendEvent>, Subscription)> {
|
||||||
|
cx.update(signed_state::init)?;
|
||||||
|
let backend = cx.update(|cx| Backend::global(cx));
|
||||||
|
|
||||||
|
let (sender, receiver) = flume::unbounded();
|
||||||
|
// The returned subscription keeps the forwarding alive; dropping it ends it.
|
||||||
|
let subscription = cx.subscribe(&backend, move |_, event: &BackendEvent, _| {
|
||||||
|
// Sending fails only once the command stopped listening.
|
||||||
|
sender.send(event.clone()).ok();
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok((backend, receiver, subscription))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Waits for the next backend event.
|
||||||
|
async fn next_event(receiver: &flume::Receiver<BackendEvent>) -> anyhow::Result<BackendEvent> {
|
||||||
|
Ok(receiver.recv_async().await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The public key of the restored signer.
|
||||||
|
fn current_user(backend: &Entity<Backend>, cx: &App) -> anyhow::Result<PublicKey> {
|
||||||
|
backend
|
||||||
|
.read(cx)
|
||||||
|
.current_user()
|
||||||
|
.context("the signer changed without an identity")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// How the device is signed in once its stored credential was read.
|
||||||
|
enum Identity {
|
||||||
|
SignedIn(PublicKey),
|
||||||
|
PassphraseRequired,
|
||||||
|
SignedOut,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Waits for the stored credential to be read.
|
||||||
|
async fn restore(
|
||||||
|
backend: &Entity<Backend>,
|
||||||
|
receiver: &flume::Receiver<BackendEvent>,
|
||||||
|
cx: &AsyncApp,
|
||||||
|
) -> anyhow::Result<Identity> {
|
||||||
|
let mut failure = None;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match next_event(receiver).await? {
|
||||||
|
BackendEvent::SignerChanged => {
|
||||||
|
return Ok(Identity::SignedIn(
|
||||||
|
cx.update(|cx| current_user(backend, cx))?,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
BackendEvent::PassphraseRequired => return Ok(Identity::PassphraseRequired),
|
||||||
|
BackendEvent::SignerRequired => {
|
||||||
|
// A failed read is reported first, then the signed-out state.
|
||||||
|
if let Some(message) = failure {
|
||||||
|
bail!("{message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(Identity::SignedOut);
|
||||||
|
}
|
||||||
|
BackendEvent::Error(message) => failure = Some(message),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fails when the device already holds a credential, since a second one would shadow it on restore.
|
||||||
|
async fn require_signed_out(
|
||||||
|
backend: &Entity<Backend>,
|
||||||
|
receiver: &flume::Receiver<BackendEvent>,
|
||||||
|
cx: &AsyncApp,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
match restore(backend, receiver, cx).await? {
|
||||||
|
Identity::SignedOut => Ok(()),
|
||||||
|
Identity::SignedIn(public_key) => {
|
||||||
|
bail!("this device already holds an identity ({public_key}); run `si logout` first")
|
||||||
|
}
|
||||||
|
Identity::PassphraseRequired => {
|
||||||
|
bail!("this device already holds an encrypted identity; run `si logout` first")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn whoami(passphrase_file: Option<&Path>, cx: &mut AsyncApp) -> anyhow::Result<()> {
|
||||||
|
let (backend, receiver, _subscription) = open(cx)?;
|
||||||
|
|
||||||
|
let public_key = match restore(&backend, &receiver, cx).await? {
|
||||||
|
Identity::SignedIn(public_key) => public_key,
|
||||||
|
Identity::PassphraseRequired => {
|
||||||
|
let passphrase = read_passphrase(passphrase_file)?;
|
||||||
|
backend
|
||||||
|
.update(cx, |backend, cx| {
|
||||||
|
backend.restore_with_passphrase(&passphrase, cx)
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
Identity::SignedOut => {
|
||||||
|
bail!("no identity on this device; run `si signup` or `si login` first")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
print_identity(public_key)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn signup(
|
||||||
|
name: &str,
|
||||||
|
passphrase_file: Option<&Path>,
|
||||||
|
cx: &mut AsyncApp,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let (backend, receiver, _subscription) = open(cx)?;
|
||||||
|
|
||||||
|
require_signed_out(&backend, &receiver, cx).await?;
|
||||||
|
|
||||||
|
let passphrase = new_passphrase(passphrase_file)?;
|
||||||
|
let public_key = backend
|
||||||
|
.update(cx, |backend, cx| {
|
||||||
|
backend.create_identity(name, &passphrase, cx)
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
print_identity(public_key)?;
|
||||||
|
|
||||||
|
// The identity is stored already; wait so exiting does not drop the events still being sent.
|
||||||
|
loop {
|
||||||
|
match next_event(&receiver).await? {
|
||||||
|
BackendEvent::IdentityPublished(Ok(())) => return Ok(()),
|
||||||
|
BackendEvent::IdentityPublished(Err(message)) => bail!("{message}"),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn login(
|
||||||
|
credential_file: Option<&Path>,
|
||||||
|
passphrase_file: Option<&Path>,
|
||||||
|
cx: &mut AsyncApp,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let (backend, receiver, _subscription) = open(cx)?;
|
||||||
|
|
||||||
|
require_signed_out(&backend, &receiver, cx).await?;
|
||||||
|
|
||||||
|
let credential = read_credential(credential_file)?;
|
||||||
|
let credential = credential.trim();
|
||||||
|
|
||||||
|
let public_key = if credential.starts_with("nsec1") {
|
||||||
|
let passphrase = new_passphrase(passphrase_file)?;
|
||||||
|
backend
|
||||||
|
.update(cx, |backend, cx| {
|
||||||
|
backend.import_nsec(credential, &passphrase, cx)
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
} else if credential.starts_with("ncryptsec1") {
|
||||||
|
let passphrase = read_passphrase(passphrase_file)?;
|
||||||
|
backend
|
||||||
|
.update(cx, |backend, cx| {
|
||||||
|
backend.import_ncryptsec(credential, &passphrase, cx)
|
||||||
|
})
|
||||||
|
.await?
|
||||||
|
} else if credential.starts_with("bunker://") {
|
||||||
|
backend
|
||||||
|
.update(cx, |backend, cx| backend.import_bunker(credential, cx))
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
bail!("the credential must be an nsec, ncryptsec or bunker:// URI");
|
||||||
|
};
|
||||||
|
|
||||||
|
print_identity(public_key)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn logout(cx: &mut AsyncApp) -> anyhow::Result<()> {
|
||||||
|
let (backend, receiver, _subscription) = open(cx)?;
|
||||||
|
|
||||||
|
if let Identity::SignedOut = restore(&backend, &receiver, cx).await? {
|
||||||
|
bail!("no identity on this device");
|
||||||
|
}
|
||||||
|
|
||||||
|
backend.update(cx, |backend, cx| backend.sign_out(cx)).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads one line from a file, without its line ending.
|
||||||
|
fn read_line(path: &Path) -> anyhow::Result<String> {
|
||||||
|
let line = std::fs::read_to_string(path)?;
|
||||||
|
Ok(line.trim_end_matches(['\r', '\n']).to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads the passphrase from `path`, or from the terminal without echoing it.
|
||||||
|
fn read_passphrase(path: Option<&Path>) -> anyhow::Result<String> {
|
||||||
|
match path {
|
||||||
|
Some(path) => read_line(path),
|
||||||
|
None => Ok(rpassword::prompt_password("Passphrase: ")?),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads the passphrase for a new key, confirming it when it is typed.
|
||||||
|
fn new_passphrase(path: Option<&Path>) -> anyhow::Result<String> {
|
||||||
|
let Some(path) = path else {
|
||||||
|
let passphrase = rpassword::prompt_password("Choose a passphrase: ")?;
|
||||||
|
if passphrase != rpassword::prompt_password("Confirm passphrase: ")? {
|
||||||
|
bail!("passphrases do not match");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(passphrase);
|
||||||
|
};
|
||||||
|
|
||||||
|
read_line(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads the credential from `path`, or from the terminal without echoing it.
|
||||||
|
fn read_credential(path: Option<&Path>) -> anyhow::Result<String> {
|
||||||
|
match path {
|
||||||
|
Some(path) => read_line(path),
|
||||||
|
None => Ok(rpassword::prompt_password(
|
||||||
|
"Credential (nsec, ncryptsec or bunker URI): ",
|
||||||
|
)?),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_identity(public_key: PublicKey) -> anyhow::Result<()> {
|
||||||
|
println!("npub: {}", public_key.to_bech32()?);
|
||||||
|
println!("hex: {public_key}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -58,7 +58,7 @@ impl Render for ExitDialog {
|
|||||||
.label("Exit")
|
.label("Exit")
|
||||||
.variant(ButtonVariant::Destructive)
|
.variant(ButtonVariant::Destructive)
|
||||||
.on_click(move |_, _, cx| {
|
.on_click(move |_, _, cx| {
|
||||||
backend.update(cx, |backend, cx| backend.sign_out(cx));
|
backend.update(cx, |backend, cx| backend.sign_out(cx)).detach();
|
||||||
confirm_close.close(cx);
|
confirm_close.close(cx);
|
||||||
})
|
})
|
||||||
.into_any_element(),
|
.into_any_element(),
|
||||||
|
|||||||
@@ -2,9 +2,8 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use gpui::prelude::FluentBuilder as _;
|
use gpui::prelude::FluentBuilder as _;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
AnyElement, App, AppContext as _, Context, Entity, IntoElement, ParentElement as _, Pixels,
|
AnyElement, App, AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render,
|
||||||
Render, ScrollHandle, SharedString, Styled as _, Subscription, Task, WeakEntity, Window, div,
|
ScrollHandle, SharedString, Styled as _, Subscription, Task, WeakEntity, Window, div, px,
|
||||||
px,
|
|
||||||
};
|
};
|
||||||
use helpers::{
|
use helpers::{
|
||||||
ActivityItem, ActivityRow, RepositoryItem, RepositoryRow, activity_items, repository_items,
|
ActivityItem, ActivityRow, RepositoryItem, RepositoryRow, activity_items, repository_items,
|
||||||
@@ -25,10 +24,6 @@ use crate::shared::log_dropped;
|
|||||||
|
|
||||||
mod helpers;
|
mod helpers;
|
||||||
|
|
||||||
const AVATAR_SIDE: Pixels = px(96.);
|
|
||||||
/// The centered content width, so the profile does not stretch across a wide window.
|
|
||||||
const CONTENT_WIDTH: Pixels = px(720.);
|
|
||||||
|
|
||||||
/// A user's identity, announced repositories, and recent activity.
|
/// A user's identity, announced repositories, and recent activity.
|
||||||
pub struct ProfileView {
|
pub struct ProfileView {
|
||||||
scroll_handle: ScrollHandle,
|
scroll_handle: ScrollHandle,
|
||||||
@@ -181,7 +176,7 @@ impl ProfileView {
|
|||||||
.name(name.clone())
|
.name(name.clone())
|
||||||
.picture(profile.picture())
|
.picture(profile.picture())
|
||||||
.fully_rounded(true)
|
.fully_rounded(true)
|
||||||
.size(AVATAR_SIDE),
|
.size_24(),
|
||||||
)
|
)
|
||||||
.child(div().text_xl().font_semibold().child(name))
|
.child(div().text_xl().font_semibold().child(name))
|
||||||
.when_some(about, |this, about| {
|
.when_some(about, |this, about| {
|
||||||
@@ -262,7 +257,7 @@ impl Render for ProfileView {
|
|||||||
.child(
|
.child(
|
||||||
v_flex().w_full().py_12().items_center().child(
|
v_flex().w_full().py_12().items_center().child(
|
||||||
v_flex()
|
v_flex()
|
||||||
.w(CONTENT_WIDTH)
|
.w(px(720.))
|
||||||
.px_6()
|
.px_6()
|
||||||
.gap_10()
|
.gap_10()
|
||||||
.child(self.render_identity(cx))
|
.child(self.render_identity(cx))
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ signed_core = { path = "../signed_core" }
|
|||||||
signed_git = { path = "../signed_git" }
|
signed_git = { path = "../signed_git" }
|
||||||
signed_nostr = { path = "../signed_nostr" }
|
signed_nostr = { path = "../signed_nostr" }
|
||||||
settings = { path = "../settings" }
|
settings = { path = "../settings" }
|
||||||
|
paths = { path = "../paths" }
|
||||||
utils = { path = "../utils" }
|
utils = { path = "../utils" }
|
||||||
|
|
||||||
nostr.workspace = true
|
nostr.workspace = true
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ pub enum BackendEvent {
|
|||||||
status: PushStepStatus,
|
status: PushStepStatus,
|
||||||
},
|
},
|
||||||
Error(String),
|
Error(String),
|
||||||
|
/// The relay list, profile and grasp list of a new identity were published; `Err` when no relay took them.
|
||||||
|
IdentityPublished(Result<(), String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BackendEvent {
|
impl BackendEvent {
|
||||||
@@ -550,13 +552,19 @@ impl Backend {
|
|||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
cx.spawn(async move |this, cx| {
|
cx.spawn(async move |this, cx| {
|
||||||
if let Err(error) = futures::future::try_join_all(publishes).await {
|
let published = futures::future::try_join_all(publishes)
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(|error| {
|
||||||
|
format!("identity created, but publishing it failed: {error}")
|
||||||
|
});
|
||||||
|
|
||||||
this.update(cx, |_this, cx| {
|
this.update(cx, |_this, cx| {
|
||||||
cx.emit(BackendEvent::error(format!(
|
if let Err(message) = &published {
|
||||||
"identity created, but publishing it failed: {error}"
|
cx.emit(BackendEvent::error(message.clone()));
|
||||||
)));
|
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
|
cx.emit(BackendEvent::IdentityPublished(published));
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok::<(), anyhow::Error>(())
|
Ok::<(), anyhow::Error>(())
|
||||||
})
|
})
|
||||||
@@ -1153,7 +1161,7 @@ impl Backend {
|
|||||||
self.passphrase_required
|
self.passphrase_required
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sign_out(&mut self, cx: &mut Context<Self>) {
|
pub fn sign_out(&mut self, cx: &mut Context<Self>) -> Task<Result<(), Error>> {
|
||||||
self.supersede_session();
|
self.supersede_session();
|
||||||
// Nothing may be signed with the identity the user just left.
|
// Nothing may be signed with the identity the user just left.
|
||||||
self.signer.swap_inner(Keys::generate());
|
self.signer.swap_inner(Keys::generate());
|
||||||
@@ -1165,6 +1173,11 @@ impl Backend {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let delete = cx.delete_credentials(USER_KEYRING);
|
let delete = cx.delete_credentials(USER_KEYRING);
|
||||||
|
|
||||||
|
cx.emit(BackendEvent::SignerChanged);
|
||||||
|
cx.emit(BackendEvent::SignerRequired);
|
||||||
|
cx.notify();
|
||||||
|
|
||||||
cx.spawn(async move |this, cx| {
|
cx.spawn(async move |this, cx| {
|
||||||
if let Err(error) = delete.await {
|
if let Err(error) = delete.await {
|
||||||
this.update(cx, |_this, cx| {
|
this.update(cx, |_this, cx| {
|
||||||
@@ -1176,10 +1189,6 @@ impl Backend {
|
|||||||
|
|
||||||
Ok::<(), Error>(())
|
Ok::<(), Error>(())
|
||||||
})
|
})
|
||||||
.detach();
|
|
||||||
cx.emit(BackendEvent::SignerChanged);
|
|
||||||
cx.emit(BackendEvent::SignerRequired);
|
|
||||||
cx.notify();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sync_inbox(&mut self, cx: &mut Context<Self>) {
|
fn sync_inbox(&mut self, cx: &mut Context<Self>) {
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ mod repos;
|
|||||||
mod state;
|
mod state;
|
||||||
mod sync_status;
|
mod sync_status;
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
|
|
||||||
pub use backend::{Backend, BackendEvent};
|
pub use backend::{Backend, BackendEvent};
|
||||||
pub use bootstrap::{sync_bootstrap_only, user_grasp_list_servers};
|
pub use bootstrap::{sync_bootstrap_only, user_grasp_list_servers};
|
||||||
pub use checkouts::CheckoutsStore;
|
pub use checkouts::CheckoutsStore;
|
||||||
@@ -26,17 +24,29 @@ pub use push::{GraspServer, PushOutcome};
|
|||||||
pub use refresh::{RefreshGate, RefreshRequest};
|
pub use refresh::{RefreshGate, RefreshRequest};
|
||||||
pub use repo::RepoStore;
|
pub use repo::RepoStore;
|
||||||
pub use repos::RepoListStore;
|
pub use repos::RepoListStore;
|
||||||
|
use settings::SettingsStore;
|
||||||
pub use signed_git::{Nip34Binding, Nip34Kind};
|
pub use signed_git::{Nip34Binding, Nip34Kind};
|
||||||
use signed_nostr::NostrBackend;
|
use signed_nostr::NostrBackend;
|
||||||
pub use state::{GitPushMode, GitPushPolicy, GitRef, PushStep, PushStepStatus, SyncState};
|
pub use state::{GitPushMode, GitPushPolicy, GitRef, PushStep, PushStepStatus, SyncState};
|
||||||
pub use sync_status::{CheckoutSyncStatus, SyncEvent, SyncStatusDirectory, SyncStatusStore};
|
pub use sync_status::{CheckoutSyncStatus, SyncEvent, SyncStatusDirectory, SyncStatusStore};
|
||||||
|
|
||||||
pub fn init(
|
/// Loads the settings, creates the data directories and installs the stores every frontend shares.
|
||||||
db_path: impl AsRef<Path>,
|
pub fn init(cx: &mut App) -> anyhow::Result<()> {
|
||||||
repos_root: impl Into<PathBuf>,
|
SettingsStore::set_global(cx.new(|_| SettingsStore::new(paths::settings_file())), cx);
|
||||||
scan_paths: Vec<PathBuf>,
|
|
||||||
cx: &mut App,
|
let scan_paths = SettingsStore::global(cx)
|
||||||
) -> anyhow::Result<()> {
|
.read(cx)
|
||||||
|
.settings()
|
||||||
|
.local_repos
|
||||||
|
.scan_paths
|
||||||
|
.clone();
|
||||||
|
|
||||||
|
for dir in [paths::nostr_dir(), paths::repos_dir()] {
|
||||||
|
if let Err(error) = std::fs::create_dir_all(dir) {
|
||||||
|
log::warn!("failed to create {}: {error}", dir.display());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// rustls uses the `aws_lc_rs` provider by default.
|
// rustls uses the `aws_lc_rs` provider by default.
|
||||||
if rustls::crypto::aws_lc_rs::default_provider()
|
if rustls::crypto::aws_lc_rs::default_provider()
|
||||||
.install_default()
|
.install_default()
|
||||||
@@ -45,7 +55,7 @@ pub fn init(
|
|||||||
log::debug!("a crypto provider is already installed");
|
log::debug!("a crypto provider is already installed");
|
||||||
}
|
}
|
||||||
|
|
||||||
let db_path = db_path.as_ref().to_path_buf();
|
let db_path = paths::nostr_dir().to_path_buf();
|
||||||
|
|
||||||
let backend = match cx
|
let backend = match cx
|
||||||
.foreground_executor()
|
.foreground_executor()
|
||||||
@@ -89,7 +99,7 @@ pub fn init(
|
|||||||
|
|
||||||
let (client, signer) = (backend.client, backend.signer);
|
let (client, signer) = (backend.client, backend.signer);
|
||||||
|
|
||||||
Mirrors::install(repos_root);
|
Mirrors::install(paths::repos_dir().to_path_buf());
|
||||||
|
|
||||||
Backend::set_global(cx.new(|cx| Backend::new(client, signer, cx)), cx);
|
Backend::set_global(cx.new(|cx| Backend::new(client, signer, cx)), cx);
|
||||||
RepoListStore::set_global(cx.new(RepoListStore::new), cx);
|
RepoListStore::set_global(cx.new(RepoListStore::new), cx);
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ icons = [
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
metadata = { path = "../crates/metadata" }
|
metadata = { path = "../crates/metadata" }
|
||||||
paths = { path = "../crates/paths" }
|
|
||||||
settings = { path = "../crates/settings" }
|
settings = { path = "../crates/settings" }
|
||||||
signed_app = { path = "../crates/signed_app" }
|
signed_app = { path = "../crates/signed_app" }
|
||||||
signed_state = { path = "../crates/signed_state" }
|
signed_state = { path = "../crates/signed_state" }
|
||||||
|
|||||||
+4
-20
@@ -54,31 +54,15 @@ fn main() {
|
|||||||
cx.on_action(|_: &Quit, cx| cx.quit());
|
cx.on_action(|_: &Quit, cx| cx.quit());
|
||||||
cx.set_app_identity("build.signed.signed", "Signed");
|
cx.set_app_identity("build.signed.signed", "Signed");
|
||||||
|
|
||||||
let store = cx.new(|_| SettingsStore::new(paths::settings_file()));
|
if let Err(error) = signed_state::init(cx) {
|
||||||
SettingsStore::set_global(store.clone(), cx);
|
|
||||||
|
|
||||||
let settings = store.read(cx).settings().clone();
|
|
||||||
signed_app::AppRoot::apply_appearance_mode(settings.appearance, cx);
|
|
||||||
|
|
||||||
if let Err(error) = std::fs::create_dir_all(paths::nostr_dir()) {
|
|
||||||
tracing::warn!("failed to create nostr dir: {error}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(error) = std::fs::create_dir_all(paths::repos_dir()) {
|
|
||||||
tracing::warn!("failed to create repos dir: {error}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(error) = signed_state::init(
|
|
||||||
paths::nostr_dir(),
|
|
||||||
paths::repos_dir().to_path_buf(),
|
|
||||||
settings.local_repos.scan_paths.clone(),
|
|
||||||
cx,
|
|
||||||
) {
|
|
||||||
tracing::error!("failed to initialize application state: {error:#}");
|
tracing::error!("failed to initialize application state: {error:#}");
|
||||||
cx.quit();
|
cx.quit();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let appearance = SettingsStore::global(cx).read(cx).settings().appearance;
|
||||||
|
signed_app::AppRoot::apply_appearance_mode(appearance, cx);
|
||||||
|
|
||||||
metadata::init(cx);
|
metadata::init(cx);
|
||||||
|
|
||||||
let theme = cx.theme();
|
let theme = cx.theme();
|
||||||
|
|||||||
Reference in New Issue
Block a user