Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add long list for identities #1096

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/crates/soroban-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use assert_fs::{fixture::FixtureError, prelude::PathChild, TempDir};
use fs_extra::dir::CopyOptions;

use soroban_cli::{
commands::{config, contract, contract::invoke, global},
commands::{config, contract, contract::invoke, global, identity},
CommandParser, Pwd,
};

Expand Down Expand Up @@ -158,15 +158,15 @@ impl TestEnv {

/// Returns the public key corresponding to the test identity's `hd_path`
pub fn test_address(&self, hd_path: usize) -> String {
self.cmd::<config::identity::address::Cmd>(&format!("--hd-path={hd_path}"))
self.cmd::<identity::address::Cmd>(&format!("--hd-path={hd_path}"))
.public_key()
.unwrap()
.to_string()
}

/// Returns the private key corresponding to the test identity's `hd_path`
pub fn test_show(&self, hd_path: usize) -> String {
self.cmd::<config::identity::show::Cmd>(&format!("--hd-path={hd_path}"))
self.cmd::<identity::show::Cmd>(&format!("--hd-path={hd_path}"))
.private_key()
.unwrap()
.to_string()
Expand Down
38 changes: 13 additions & 25 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use soroban_test::TestEnv;
use std::{fs, path::Path};

use crate::util::{add_identity, add_test_id, SecretKind, DEFAULT_SEED_PHRASE};
use soroban_cli::commands::config::network;
use soroban_cli::commands::network;

const NETWORK_PASSPHRASE: &str = "Local Sandbox Stellar Network ; September 2022";

Expand Down Expand Up @@ -32,8 +32,7 @@ fn set_and_remove_network() {
// .assert()
// .stdout("");
sandbox
.new_assert_cmd("config")
.arg("network")
.new_assert_cmd("network")
.arg("ls")
.assert()
.stdout("\n");
Expand All @@ -42,8 +41,7 @@ fn set_and_remove_network() {

fn add_network(sandbox: &TestEnv, name: &str) {
sandbox
.new_assert_cmd("config")
.arg("network")
.new_assert_cmd("network")
.arg("add")
.args([
"--rpc-url=https://127.0.0.1",
Expand All @@ -59,9 +57,8 @@ fn add_network(sandbox: &TestEnv, name: &str) {

fn add_network_global(sandbox: &TestEnv, dir: &Path, name: &str) {
sandbox
.new_assert_cmd("config")
.new_assert_cmd("network")
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("network")
.arg("add")
.arg("--global")
.arg("--rpc-url")
Expand All @@ -81,28 +78,25 @@ fn set_and_remove_global_network() {
add_network_global(&sandbox, &dir, "global");

sandbox
.new_assert_cmd("config")
.new_assert_cmd("network")
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("network")
.arg("ls")
.arg("--global")
.assert()
.stdout("global\n");

sandbox
.new_assert_cmd("config")
.new_assert_cmd("network")
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("network")
.arg("rm")
.arg("--global")
.arg("global")
.assert()
.stdout("");

sandbox
.new_assert_cmd("config")
.new_assert_cmd("network")
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("network")
.arg("ls")
.assert()
.stdout("\n");
Expand Down Expand Up @@ -150,8 +144,7 @@ fn read_identity() {
let ident_dir = dir.join(".soroban/identity");
assert!(ident_dir.exists());
sandbox
.new_assert_cmd("config")
.arg("identity")
.new_assert_cmd("identity")
.arg("ls")
.assert()
.stdout("test_id\n");
Expand All @@ -161,8 +154,7 @@ fn read_identity() {
fn generate_identity() {
let sandbox = TestEnv::default();
sandbox
.new_assert_cmd("config")
.arg("identity")
.new_assert_cmd("identity")
.arg("generate")
.arg("--seed")
.arg("0000000000000000")
Expand All @@ -172,8 +164,7 @@ fn generate_identity() {
.success();

sandbox
.new_assert_cmd("config")
.arg("identity")
.new_assert_cmd("identity")
.arg("ls")
.assert()
.stdout("test\n");
Expand All @@ -197,9 +188,8 @@ fn seed_phrase() {
);

sandbox
.new_assert_cmd("config")
.new_assert_cmd("id")
.current_dir(dir)
.arg("identity")
.arg("ls")
.assert()
.stdout("test_seed\n");
Expand All @@ -218,21 +208,19 @@ fn use_env() {
let sandbox = TestEnv::default();

sandbox
.new_assert_cmd("config")
.new_assert_cmd("identity")
.env(
"SOROBAN_SECRET_KEY",
"SDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2DYQCHVCQYFD",
)
.arg("identity")
.arg("add")
.arg("bob")
.assert()
.stdout("")
.success();

sandbox
.new_assert_cmd("config")
.arg("identity")
.new_assert_cmd("identity")
.arg("show")
.arg("bob")
.assert()
Expand Down
22 changes: 0 additions & 22 deletions cmd/soroban-cli/src/commands/config/identity/ls.rs

This file was deleted.

14 changes: 14 additions & 0 deletions cmd/soroban-cli/src/commands/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ impl Args {
.collect())
}

pub fn list_identities_long(&self) -> Result<Vec<(String, String)>, Error> {
Ok(KeyType::Identity
.list_paths(&self.local_and_global()?)
.into_iter()
.flatten()
.map(|(name, location)| {
let path = match location {
Location::Local(path) | Location::Global(path) => path,
};
(name, format!("{}", path.display()))
})
.collect())
}

pub fn list_networks(&self) -> Result<Vec<String>, Error> {
Ok(KeyType::Network
.list_paths(&self.local_and_global()?)
Expand Down
19 changes: 2 additions & 17 deletions cmd/soroban-cli/src/commands/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,20 @@ use crate::Pwd;

use self::{network::Network, secret::Secret};

pub mod identity;
use super::network;

pub mod locator;
pub mod network;
pub mod secret;

#[derive(Debug, Parser)]
pub enum Cmd {
/// Configure different identities to sign transactions.
#[command(subcommand)]
Identity(identity::Cmd),
/// Configure different networks
#[command(subcommand)]
Network(network::Cmd),
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Identity(#[from] identity::Error),
#[error(transparent)]
Network(#[from] network::Error),
#[error(transparent)]
Expand All @@ -34,16 +29,6 @@ pub enum Error {
Config(#[from] locator::Error),
}

impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
match &self {
Cmd::Identity(identity) => identity.run().await?,
Cmd::Network(network) => network.run()?,
}
Ok(())
}
}

#[derive(Debug, clap::Args, Clone, Default)]
#[group(skip)]
pub struct Args {
Expand Down
6 changes: 2 additions & 4 deletions cmd/soroban-cli/src/commands/contract/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ use soroban_spec_typescript::{self as typescript, boilerplate::Project};
use crate::wasm;
use crate::{
commands::{
config::{
locator,
network::{self, Network},
},
config::locator,
contract::{self, fetch},
network::{self, Network},
},
utils::contract_spec::{self, ContractSpec},
};
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/contract/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use soroban_spec::read::FromWasmError;
use stellar_strkey::DecodeError;

use super::super::config::{self, locator};
use crate::commands::config::network::{self, Network};
use crate::commands::network::{self, Network};
use crate::{
rpc::{self, Client},
utils, Pwd,
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl Cmd {
if let Some(mut val) = matches_.get_raw(&name) {
let mut s = val.next().unwrap().to_string_lossy().to_string();
if matches!(i.type_, ScSpecTypeDef::Address) {
let cmd = crate::commands::config::identity::address::Cmd {
let cmd = crate::commands::identity::address::Cmd {
name: Some(s.clone()),
hd_path: Some(0),
locator: self.config.locator.clone(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-cli/src/commands/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io;

use soroban_env_host::xdr::{self, ReadXdr};

use super::config::{locator, network};
use super::{config::locator, network};
use crate::{rpc, utils};

#[derive(Parser, Debug, Clone)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::super::{locator, secret};
use clap::command;

use super::super::config::{locator, secret};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::commands::config::secret::Secret;

use super::super::{locator, secret};
use super::super::config::{
locator,
secret::{self, Secret},
};
use clap::arg;

#[derive(thiserror::Error, Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::command;

use crate::commands::config::network;
use crate::commands::network;

use super::address;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use clap::{arg, command};

use super::super::{
locator, network,
use crate::commands::network;

use super::super::config::{
locator,
secret::{self, Secret},
};

Expand Down
40 changes: 40 additions & 0 deletions cmd/soroban-cli/src/commands/identity/ls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use clap::command;

use super::super::config::locator;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Config(#[from] locator::Error),
}

#[derive(Debug, clap::Parser, Clone)]
#[group(skip)]
pub struct Cmd {
#[command(flatten)]
pub config_locator: locator::Args,

#[arg(long, short = 'l')]
pub long: bool,
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
let res = if self.long { self.ls_l() } else { self.ls() }?.join("\n");
println!("{res}");
Ok(())
}

pub fn ls(&self) -> Result<Vec<String>, Error> {
Ok(self.config_locator.list_identities()?)
}

pub fn ls_l(&self) -> Result<Vec<String>, Error> {
Ok(self
.config_locator
.list_identities_long()?
.into_iter()
.map(|(name, location)| format!("{location}\nName: {name}\n"))
.collect::<Vec<String>>())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::super::locator;
use clap::command;

use super::super::config::locator;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::super::{locator, secret};
use clap::arg;

use super::super::config::{locator, secret};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Expand Down
Loading