Skip to content

Commit

Permalink
Add global config for default network and identity.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Oct 29, 2024
1 parent 1c3f4d2 commit 705e2b0
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 10 deletions.
50 changes: 50 additions & 0 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Anything after the `--` double dash (the "slop") is parsed as arguments to the c

* `contract` — Tools for smart contract developers
* `events` — Watch the network for contract events
* `env` — Show current environment
* `keys` — Create and manage identities including keys and addresses
* `network` — Start and configure networks
* `snapshot` — Download a snapshot of a ledger from an archive
Expand Down Expand Up @@ -896,6 +897,19 @@ Watch the network for contract events



## `stellar env`

Show current environment

**Usage:** `stellar env [OPTIONS]`

###### **Options:**

* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."



## `stellar keys`

Create and manage identities including keys and addresses
Expand All @@ -911,6 +925,7 @@ Create and manage identities including keys and addresses
* `ls` — List identities
* `rm` — Remove an identity
* `show` — Given an identity return its private key
* `use` — Set the default identity that will be used on all commands. This allows you to skip `--source-account` or setting a environment variable, while reusing this value in all commands that require it



Expand Down Expand Up @@ -1051,6 +1066,23 @@ Given an identity return its private key



## `stellar keys use`

Set the default identity that will be used on all commands. This allows you to skip `--source-account` or setting a environment variable, while reusing this value in all commands that require it

**Usage:** `stellar keys use [OPTIONS] <NAME>`

###### **Arguments:**

* `<NAME>` — Set the default network name

###### **Options:**

* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."



## `stellar network`

Start and configure networks
Expand All @@ -1064,6 +1096,7 @@ Start and configure networks
* `ls` — List networks
* `start` — ⚠️ Deprecated: use `stellar container start` instead
* `stop` — ⚠️ Deprecated: use `stellar container stop` instead
* `use` — Set the default network that will be used on all commands. This allows you to skip `--network` or setting a environment variable, while reusing this value in all commands that require it
* `container` — Commands to start, stop and get logs for a quickstart container


Expand Down Expand Up @@ -1173,6 +1206,23 @@ Stop a network started with `network start`. For example, if you ran `stellar ne



## `stellar network use`

Set the default network that will be used on all commands. This allows you to skip `--network` or setting a environment variable, while reusing this value in all commands that require it

**Usage:** `stellar network use [OPTIONS] <NAME>`

###### **Arguments:**

* `<NAME>` — Set the default network name

###### **Options:**

* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."



## `stellar network container`

Commands to start, stop and get logs for a quickstart container
Expand Down
53 changes: 53 additions & 0 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,56 @@ fn config_dirs_precedence() {
))
.stdout("SAQMV6P3OWM2SKCK3OEWNXSRYWK5RNNUL5CPHQGIJF2WVT4EI2BZ63GG\n");
}

#[test]
fn set_default_identity() {
let sandbox = TestEnv::default();

sandbox
.new_assert_cmd("keys")
.env(
"SOROBAN_SECRET_KEY",
"SC4ZPYELVR7S7EE7KZDZN3ETFTNQHHLTUL34NUAAWZG5OK2RGJ4V2U3Z",
)
.arg("add")
.arg("alice")
.assert()
.success();

sandbox
.new_assert_cmd("keys")
.arg("use")
.arg("alice")
.assert()
.stderr(predicate::str::contains(
"The default source account is set to `alice`",
))
.success();

sandbox
.new_assert_cmd("env")
.assert()
.stdout(predicate::str::contains("identity=alice"))
.success();
}

#[test]
fn set_default_network() {
let sandbox = TestEnv::default();

sandbox
.new_assert_cmd("network")
.arg("use")
.arg("testnet")
.assert()
.stderr(predicate::str::contains(
"The default network is set to `testnet`",
))
.success();

sandbox
.new_assert_cmd("env")
.assert()
.stdout(predicate::str::contains("network=testnet"))
.success();
}
37 changes: 37 additions & 0 deletions cmd/soroban-cli/src/commands/env/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{
commands::global,
config::locator::{self, config, config_file},
};
use clap::Parser;

#[derive(Debug, Parser)]
pub struct Cmd {
#[command(flatten)]
pub config_locator: locator::Args,
}

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

impl Cmd {
pub fn run(&self, _global_args: &global::Args) -> Result<(), Error> {
let config = config()?;

println!("config_file={}", config_file()?.to_string_lossy());

println!(
"network={}",
config.defaults.network.unwrap_or("(unset)".to_string())
);

println!(
"identity={}",
config.defaults.identity.unwrap_or("(unset)".to_string())
);

Ok(())
}
}
35 changes: 35 additions & 0 deletions cmd/soroban-cli/src/commands/keys/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use clap::command;

use crate::{commands::global, config::locator, print::Print};

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

#[derive(Debug, clap::Parser, Clone)]
#[group(skip)]
pub struct Cmd {
/// Set the default network name.
pub name: String,

#[command(flatten)]
pub config_locator: locator::Args,
}

impl Cmd {
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let printer = Print::new(global_args.quiet);
let _ = self.config_locator.read_identity(&self.name)?;

self.config_locator.write_default_identity(&self.name)?;

printer.infoln(format!(
"The default source account is set to `{}`",
self.name,
));

Ok(())
}
}
20 changes: 20 additions & 0 deletions cmd/soroban-cli/src/commands/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::Parser;

pub mod add;
pub mod address;
pub mod default;
pub mod fund;
pub mod generate;
pub mod ls;
Expand All @@ -13,18 +14,30 @@ pub mod show;
pub enum Cmd {
/// Add a new identity (keypair, ledger, macOS keychain)
Add(add::Cmd),

/// Given an identity return its address (public key)
Address(address::Cmd),

/// Fund an identity on a test network
Fund(fund::Cmd),

/// Generate a new identity with a seed phrase, currently 12 words
Generate(generate::Cmd),

/// List identities
Ls(ls::Cmd),

/// Remove an identity
Rm(rm::Cmd),

/// Given an identity return its private key
Show(show::Cmd),

/// Set the default identity that will be used on all commands.
/// This allows you to skip `--source-account` or setting a environment
/// variable, while reusing this value in all commands that require it.
#[command(name = "use")]
Default(default::Cmd),
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -34,18 +47,24 @@ pub enum Error {

#[error(transparent)]
Address(#[from] address::Error),

#[error(transparent)]
Fund(#[from] fund::Error),

#[error(transparent)]
Generate(#[from] generate::Error),

#[error(transparent)]
Rm(#[from] rm::Error),

#[error(transparent)]
Ls(#[from] ls::Error),

#[error(transparent)]
Show(#[from] show::Error),

#[error(transparent)]
Default(#[from] default::Error),
}

impl Cmd {
Expand All @@ -58,6 +77,7 @@ impl Cmd {
Cmd::Ls(cmd) => cmd.run()?,
Cmd::Rm(cmd) => cmd.run()?,
Cmd::Show(cmd) => cmd.run()?,
Cmd::Default(cmd) => cmd.run(global_args)?,
};
Ok(())
}
Expand Down
22 changes: 21 additions & 1 deletion cmd/soroban-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::config;
pub mod cache;
pub mod completion;
pub mod contract;
pub mod env;
pub mod events;
pub mod global;
pub mod keys;
Expand Down Expand Up @@ -115,7 +116,8 @@ impl Root {
Cmd::Version(version) => version.run(),
Cmd::Keys(id) => id.run(&self.global_args).await?,
Cmd::Tx(tx) => tx.run(&self.global_args).await?,
Cmd::Cache(data) => data.run()?,
Cmd::Cache(cache) => cache.run()?,
Cmd::Env(env) => env.run(&self.global_args)?,
};
Ok(())
}
Expand All @@ -134,9 +136,13 @@ pub enum Cmd {
/// Tools for smart contract developers
#[command(subcommand)]
Contract(contract::Cmd),

/// Watch the network for contract events
Events(events::Cmd),

/// Show current environment
Env(env::Cmd),

/// Create and manage identities including keys and addresses
#[command(subcommand)]
Keys(keys::Cmd),
Expand All @@ -159,9 +165,11 @@ pub enum Cmd {
/// Print shell completion code for the specified shell.
#[command(long_about = completion::LONG_ABOUT)]
Completion(completion::Cmd),

/// Cache for transactions and contract specs
#[command(subcommand)]
Cache(cache::Cmd),

/// Print version information
Version(version::Cmd),
}
Expand All @@ -171,24 +179,36 @@ pub enum Error {
// TODO: stop using Debug for displaying errors
#[error(transparent)]
Contract(#[from] contract::Error),

#[error(transparent)]
Events(#[from] events::Error),

#[error(transparent)]
Keys(#[from] keys::Error),

#[error(transparent)]
Xdr(#[from] stellar_xdr::cli::Error),

#[error(transparent)]
Clap(#[from] clap::error::Error),

#[error(transparent)]
Plugin(#[from] plugin::Error),

#[error(transparent)]
Network(#[from] network::Error),

#[error(transparent)]
Snapshot(#[from] snapshot::Error),

#[error(transparent)]
Tx(#[from] tx::Error),

#[error(transparent)]
Cache(#[from] cache::Error),

#[error(transparent)]
Env(#[from] env::Error),
}

#[async_trait]
Expand Down
Loading

0 comments on commit 705e2b0

Please sign in to comment.