Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
cli: Add rad config get
Browse files Browse the repository at this point in the history
Gets a specific config value.
  • Loading branch information
cloudhead committed Feb 7, 2024
1 parent e5fcbba commit 137961c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
15 changes: 14 additions & 1 deletion radicle-cli/examples/rad-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ $ rad config
{
"publicExplorer": "https://app.radicle.xyz/nodes/$host/$rid$path",
"preferredSeeds": [
"z6MkrLMMsiPWUcNPHcRajuMi9mDfYckSoJyPwwnknocNYPm7@seed.radicle.garden:8776"
"z6MkrLMMsiPWUcNPHcRajuMi9mDfYckSoJyPwwnknocNYPm7@seed.radicle.garden:8776",
"z6MksmpU5b1dS7oaqF2bHXhQi1DWy2hB7Mh9CuN7y1DN6QSz@seed.radicle.xyz:8776"
],
"web": {
"pinned": {
Expand Down Expand Up @@ -49,3 +50,15 @@ $ rad config
}
}
```

You can also get any value in the configuration by path, eg.

```
$ rad config get node.alias
alice
$ rad config get preferredSeeds
z6MkrLMMsiPWUcNPHcRajuMi9mDfYckSoJyPwwnknocNYPm7@seed.radicle.garden:8776
z6MksmpU5b1dS7oaqF2bHXhQi1DWy2hB7Mh9CuN7y1DN6QSz@seed.radicle.xyz:8776
$ rad config get node.limits.routingMaxSize
1000
```
48 changes: 45 additions & 3 deletions radicle-cli/src/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(clippy::or_fun_call)]
use std::ffi::OsString;
use std::path::Path;
use std::process;

use anyhow::anyhow;
Expand All @@ -16,9 +17,11 @@ pub const HELP: Help = Help {
Usage
rad config [<option>...]
rad config show
rad config init
rad config edit
rad config show [<option>...]
rad config init [<option>...]
rad config edit [<option>...]
rad config get <key> [<option>...]
If no argument is specified, prints the current radicle configuration as JSON.
To initialize a new configuration file, use `rad config init`.
Expand All @@ -34,6 +37,7 @@ Options
enum Operation {
#[default]
Show,
Get(String),
Init,
Edit,
}
Expand All @@ -59,6 +63,12 @@ impl Args for Options {
"show" => op = Some(Operation::Show),
"edit" => op = Some(Operation::Edit),
"init" => op = Some(Operation::Init),
"get" => {
let value = parser.value()?;
let key = value.to_string_lossy();

op = Some(Operation::Get(key.to_string()));
}
unknown => anyhow::bail!("unknown operation '{unknown}'"),
},
_ => return Err(anyhow!(arg.unexpected())),
Expand All @@ -82,6 +92,12 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {
Operation::Show => {
term::json::to_pretty(&profile.config, path.as_path())?.print();
}
Operation::Get(key) => {
let data = serde_json::to_value(profile.config)?;
if let Some(value) = get_value(&data, &key) {
print_value(value)?;
}
}
Operation::Init => {
if path.try_exists()? {
anyhow::bail!("configuration file already exists at `{}`", path.display());
Expand All @@ -104,3 +120,29 @@ pub fn run(options: Options, ctx: impl term::Context) -> anyhow::Result<()> {

Ok(())
}

/// Get JSON value under a path.
fn get_value<'a>(data: &'a serde_json::Value, path: &'a str) -> Option<&'a serde_json::Value> {
path.split('.').try_fold(data, |acc, key| {
if let serde_json::Value::Object(obj) = acc {
obj.get(key)
} else {
None
}
})
}

/// Print a JSON Value.
fn print_value(value: &serde_json::Value) -> anyhow::Result<()> {
match value {
serde_json::Value::Null => {}
serde_json::Value::Bool(b) => term::print(b),
serde_json::Value::Array(a) => a.iter().try_for_each(print_value)?,
serde_json::Value::Number(n) => term::print(n),
serde_json::Value::String(s) => term::print(s),
serde_json::Value::Object(o) => {
term::json::to_pretty(&o, Path::new("config.json"))?.print()
}
}
Ok(())
}
6 changes: 5 additions & 1 deletion radicle-cli/tests/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{env, thread, time};
use radicle::git;
use radicle::node;
use radicle::node::address::Store as _;
use radicle::node::config::seeds::{RADICLE_COMMUNITY_NODE, RADICLE_TEAM_NODE};
use radicle::node::routing::Store as _;
use radicle::node::Handle as _;
use radicle::node::{Alias, DEFAULT_TIMEOUT};
Expand Down Expand Up @@ -238,7 +239,10 @@ fn rad_inspect() {
fn rad_config() {
let mut environment = Environment::new();
let alias = Alias::new("alice");
let profile = environment.profile(profile::Config::new(alias));
let profile = environment.profile(profile::Config {
preferred_seeds: vec![RADICLE_COMMUNITY_NODE.clone(), RADICLE_TEAM_NODE.clone()],
..profile::Config::new(alias)
});
let working = tempfile::tempdir().unwrap();

test(
Expand Down

0 comments on commit 137961c

Please sign in to comment.