Skip to content

Commit 0f096fa

Browse files
authored
Add stellar network unset to remove default network. (#2338)
1 parent 24bfc54 commit 0f096fa

File tree

8 files changed

+115
-2
lines changed

8 files changed

+115
-2
lines changed

FULL_HELP_DOCS.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ Configure connection to networks
13311331
- `health` — Fetch the health of the configured RPC
13321332
- `info` — Checks the health of the configured RPC
13331333
- `settings` — Fetch the network's config settings
1334+
- `unset` — Unset the default network defined previously with `network use <network>`
13341335

13351336
## `stellar network add`
13361337

@@ -1486,6 +1487,17 @@ Fetch the network's config settings
14861487
- `--network-passphrase <NETWORK_PASSPHRASE>` — Network passphrase to sign the transaction sent to the rpc server
14871488
- `-n`, `--network <NETWORK>` — Name of network to use from config
14881489

1490+
## `stellar network unset`
1491+
1492+
Unset the default network defined previously with `network use <network>`
1493+
1494+
**Usage:** `stellar network unset [OPTIONS]`
1495+
1496+
###### **Options (Global):**
1497+
1498+
- `--global` — ⚠️ Deprecated: global config is always on
1499+
- `--config-dir <CONFIG_DIR>` — Location of config directory. By default, it uses `$XDG_CONFIG_HOME/stellar` if set, falling back to `~/.config/stellar` otherwise. Contains configuration files, aliases, and other persistent settings
1500+
14891501
## `stellar container`
14901502

14911503
Start local networks in containers

cmd/crates/soroban-test/tests/it/integration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod hello_world;
99
mod init;
1010
mod keys;
1111
mod ledger;
12+
mod network;
1213
mod secure_store;
1314
mod snapshot;
1415
mod tx;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use predicates::prelude::{predicate, PredicateBooleanExt};
2+
use soroban_test::TestEnv;
3+
4+
#[tokio::test]
5+
#[allow(clippy::too_many_lines)]
6+
async fn set_default_network() {
7+
let sandbox = &TestEnv::new();
8+
9+
sandbox
10+
.new_assert_cmd("network")
11+
.arg("use")
12+
.arg("testnet")
13+
.assert()
14+
.stderr(predicate::str::contains(
15+
"The default network is set to `testnet`",
16+
))
17+
.success();
18+
}
19+
20+
#[tokio::test]
21+
#[allow(clippy::too_many_lines)]
22+
async fn unset_default_network() {
23+
let sandbox = &TestEnv::new();
24+
25+
sandbox
26+
.new_assert_cmd("network")
27+
.arg("use")
28+
.arg("testnet")
29+
.assert()
30+
.success();
31+
32+
sandbox
33+
.new_assert_cmd("env")
34+
.env_remove("STELLAR_NETWORK")
35+
.assert()
36+
.stdout(predicate::str::contains("STELLAR_NETWORK=testnet"))
37+
.success();
38+
39+
sandbox
40+
.new_assert_cmd("network")
41+
.arg("unset")
42+
.assert()
43+
.stderr(predicate::str::contains(
44+
"The default network has been unset",
45+
))
46+
.success();
47+
48+
sandbox
49+
.new_assert_cmd("env")
50+
.env_remove("STELLAR_NETWORK")
51+
.assert()
52+
.stdout(predicate::str::contains("STELLAR_NETWORK=").not())
53+
.success();
54+
}

cmd/soroban-cli/src/commands/keys/unset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Cmd {
1717
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
1818
let printer = Print::new(global_args.quiet);
1919

20-
self.config_locator.unset_identity()?;
20+
self.config_locator.unset_default_identity()?;
2121

2222
printer.infoln("The default source account has been unset".to_string());
2323

cmd/soroban-cli/src/commands/network/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod info;
88
pub mod ls;
99
pub mod rm;
1010
pub mod settings;
11+
pub mod unset;
1112

1213
#[derive(Debug, Parser)]
1314
pub enum Cmd {
@@ -34,6 +35,9 @@ pub enum Cmd {
3435

3536
/// Fetch the network's config settings
3637
Settings(settings::Cmd),
38+
39+
/// Unset the default network defined previously with `network use <network>`
40+
Unset(unset::Cmd),
3741
}
3842

3943
#[derive(thiserror::Error, Debug)]
@@ -58,6 +62,9 @@ pub enum Error {
5862

5963
#[error(transparent)]
6064
Settings(#[from] settings::Error),
65+
66+
#[error(transparent)]
67+
Unset(#[from] unset::Error),
6168
}
6269

6370
impl Cmd {
@@ -70,6 +77,7 @@ impl Cmd {
7077
Cmd::Health(cmd) => cmd.run(global_args).await?,
7178
Cmd::Info(cmd) => cmd.run(global_args).await?,
7279
Cmd::Settings(cmd) => cmd.run(global_args).await?,
80+
Cmd::Unset(cmd) => cmd.run(global_args)?,
7381
}
7482
Ok(())
7583
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::{commands::global, print::Print};
2+
3+
use super::locator;
4+
5+
#[derive(thiserror::Error, Debug)]
6+
pub enum Error {
7+
#[error(transparent)]
8+
Config(#[from] locator::Error),
9+
}
10+
11+
#[derive(Debug, clap::Parser, Clone)]
12+
#[group(skip)]
13+
pub struct Cmd {
14+
#[command(flatten)]
15+
pub config_locator: locator::Args,
16+
}
17+
18+
impl Cmd {
19+
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
20+
let printer = Print::new(global_args.quiet);
21+
22+
self.config_locator.unset_default_network()?;
23+
24+
printer.infoln("The default network has been unset".to_string());
25+
26+
Ok(())
27+
}
28+
}

cmd/soroban-cli/src/config/locator.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,14 @@ impl Args {
215215
Config::new()?.set_identity(name).save()
216216
}
217217

218-
pub fn unset_identity(&self) -> Result<(), Error> {
218+
pub fn unset_default_identity(&self) -> Result<(), Error> {
219219
Config::new()?.unset_identity().save()
220220
}
221221

222+
pub fn unset_default_network(&self) -> Result<(), Error> {
223+
Config::new()?.unset_network().save()
224+
}
225+
222226
pub fn list_identities(&self) -> Result<Vec<String>, Error> {
223227
Ok(KeyType::Identity
224228
.list_paths(&self.local_and_global()?)?

cmd/soroban-cli/src/config/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ impl Config {
213213
self
214214
}
215215

216+
#[must_use]
217+
pub fn unset_network(mut self) -> Self {
218+
self.defaults.network = None;
219+
self
220+
}
221+
216222
pub fn save(&self) -> Result<(), locator::Error> {
217223
let toml_string = toml::to_string(&self)?;
218224
let path = cli_config_file()?;

0 commit comments

Comments
 (0)