Skip to content

Commit 4e6511f

Browse files
committed
Redirect q settings list cmd to qchat
1 parent 84bfe74 commit 4e6511f

File tree

3 files changed

+82
-48
lines changed

3 files changed

+82
-48
lines changed

crates/q_cli/src/cli/mod.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use std::io::{
2525
Write as _,
2626
stdout,
2727
};
28-
use std::path::PathBuf;
2928
use std::process::ExitCode;
3029

3130
use anstream::{
@@ -61,7 +60,6 @@ use fig_log::{
6160
};
6261
use fig_proto::local::UiElement;
6362
use fig_settings::sqlite::database;
64-
use fig_util::directories::home_local_bin;
6563
use fig_util::{
6664
CLI_BINARY_NAME,
6765
PRODUCT_NAME,
@@ -88,6 +86,7 @@ use crate::util::desktop::{
8886
use crate::util::{
8987
CliContext,
9088
assert_logged_in,
89+
qchat_path,
9190
};
9291

9392
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
@@ -599,37 +598,6 @@ async fn launch_dashboard(help_fallback: bool) -> Result<ExitCode> {
599598
Ok(ExitCode::SUCCESS)
600599
}
601600

602-
#[cfg(target_os = "linux")]
603-
fn qchat_path() -> Result<PathBuf> {
604-
use fig_os_shim::Context;
605-
use fig_util::consts::CHAT_BINARY_NAME;
606-
607-
let ctx = Context::new();
608-
if let Some(path) = ctx.process_info().current_pid().exe() {
609-
// This is required for deb installations.
610-
if path.starts_with("/usr/bin") {
611-
return Ok(PathBuf::from("/usr/bin").join(CHAT_BINARY_NAME));
612-
}
613-
}
614-
615-
if let Ok(local_bin_path) = home_local_bin() {
616-
let local_bin_path = local_bin_path.join(CHAT_BINARY_NAME);
617-
if local_bin_path.exists() {
618-
return Ok(local_bin_path);
619-
}
620-
}
621-
622-
Ok(PathBuf::from(CHAT_BINARY_NAME))
623-
}
624-
625-
#[cfg(target_os = "macos")]
626-
fn qchat_path() -> Result<PathBuf> {
627-
use fig_util::consts::CHAT_BINARY_NAME;
628-
use macos_utils::bundle::get_bundle_path_for_executable;
629-
630-
Ok(get_bundle_path_for_executable(CHAT_BINARY_NAME).unwrap_or(home_local_bin()?.join(CHAT_BINARY_NAME)))
631-
}
632-
633601
#[cfg(test)]
634602
mod test {
635603
use super::*;

crates/q_cli/src/cli/settings.rs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,24 @@ use crate::util::desktop::{
3535
use crate::util::{
3636
CliContext,
3737
app_not_running_message,
38+
qchat_path,
3839
};
3940

4041
#[derive(Debug, Subcommand, PartialEq, Eq)]
4142
pub enum SettingsSubcommands {
4243
/// Open the settings file
4344
Open,
44-
/// List all the settings
45+
/// List settings
46+
List {
47+
/// Show all available settings
48+
#[arg(long)]
49+
all: bool,
50+
/// Format of the output
51+
#[arg(long, short, value_enum, default_value_t)]
52+
format: OutputFormat,
53+
},
54+
/// List configured settings
55+
#[command(hide = true)]
4556
All {
4657
/// Format of the output
4758
#[arg(long, short, value_enum, default_value_t)]
@@ -60,7 +71,7 @@ pub struct SettingsArgs {
6071
key: Option<String>,
6172
/// value
6273
value: Option<String>,
63-
/// Delete a value
74+
/// Delete a key (No value needed)
6475
#[arg(long, short)]
6576
delete: bool,
6677
/// Format of the output
@@ -89,25 +100,45 @@ impl SettingsArgs {
89100
bail!("The EDITOR environment variable is not set")
90101
}
91102
},
103+
Some(SettingsSubcommands::List { all, format }) => {
104+
let mut args = vec!["settings".to_string(), "list".to_string()];
105+
if all {
106+
args.push("--all".to_string());
107+
}
108+
if format != OutputFormat::default() {
109+
args.push("--format".to_string());
110+
args.push(format!("{:?}", format).to_lowercase());
111+
}
112+
113+
let status = tokio::process::Command::new(qchat_path()?).args(&args).status().await?;
114+
115+
Ok(if status.success() {
116+
ExitCode::SUCCESS
117+
} else {
118+
ExitCode::FAILURE
119+
})
120+
},
92121
Some(SettingsSubcommands::All { format }) => {
93-
let settings = fig_settings::OldSettings::load()?.map().clone();
122+
let mut args = vec!["settings".to_string(), "list".to_string()];
94123

95-
match format {
96-
OutputFormat::Plain => {
97-
for (key, value) in settings {
98-
println!("{key} = {value}");
99-
}
100-
},
101-
OutputFormat::Json => println!("{}", serde_json::to_string(&settings)?),
102-
OutputFormat::JsonPretty => {
103-
println!("{}", serde_json::to_string_pretty(&settings)?);
104-
},
124+
if format != OutputFormat::default() {
125+
args.push("--format".to_string());
126+
args.push(format!("{:?}", format).to_lowercase());
105127
}
106128

107-
Ok(ExitCode::SUCCESS)
129+
let status = tokio::process::Command::new(qchat_path()?).args(&args).status().await?;
130+
131+
Ok(if status.success() {
132+
ExitCode::SUCCESS
133+
} else {
134+
ExitCode::FAILURE
135+
})
108136
},
109137
None => match &self.key {
110138
Some(key) => match (&self.value, self.delete) {
139+
(Some(_), true) => Err(eyre::eyre!(
140+
"the argument '--delete' cannot be used with '[VALUE]'\n Usage: q settings --delete {key}"
141+
)),
111142
(None, false) => match fig_settings::settings::get_value(key)? {
112143
Some(value) => {
113144
match self.format {
@@ -161,9 +192,14 @@ impl SettingsArgs {
161192

162193
Ok(ExitCode::SUCCESS)
163194
},
164-
_ => Ok(ExitCode::SUCCESS),
165195
},
166196
None => {
197+
if self.delete {
198+
return Err(eyre::eyre!(
199+
"the argument '--delete' requires a <KEY>\n Usage: q settings --delete <KEY>"
200+
));
201+
}
202+
167203
if manifest::is_minimal() || system_info::is_remote() {
168204
Cli::parse_from([CLI_BINARY_NAME, "settings", "--help"]);
169205
return Ok(ExitCode::SUCCESS);

crates/q_cli/src/util/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ use eyre::{
3838
};
3939
use fig_ipc::local::quit_command;
4040
use fig_util::consts::APP_BUNDLE_ID;
41+
use fig_util::directories::home_local_bin;
4142
use fig_util::{
43+
CHAT_BINARY_NAME,
4244
CLI_BINARY_NAME,
4345
PRODUCT_NAME,
4446
};
@@ -124,6 +126,34 @@ pub fn app_path_from_bundle_id(bundle_id: impl AsRef<OsStr>) -> Option<String> {
124126
}
125127
}
126128

129+
#[cfg(target_os = "linux")]
130+
pub fn qchat_path() -> Result<PathBuf> {
131+
use fig_os_shim::Context;
132+
133+
let ctx = Context::new();
134+
if let Some(path) = ctx.process_info().current_pid().exe() {
135+
// This is required for deb installations.
136+
if path.starts_with("/usr/bin") {
137+
return Ok(PathBuf::from("/usr/bin").join(CHAT_BINARY_NAME));
138+
}
139+
}
140+
141+
if let Ok(local_bin_path) = home_local_bin() {
142+
let local_bin_path = local_bin_path.join(CHAT_BINARY_NAME);
143+
if local_bin_path.exists() {
144+
return Ok(local_bin_path);
145+
}
146+
}
147+
148+
Ok(PathBuf::from(CHAT_BINARY_NAME))
149+
}
150+
151+
#[cfg(target_os = "macos")]
152+
pub fn qchat_path() -> Result<PathBuf> {
153+
use macos_utils::bundle::get_bundle_path_for_executable;
154+
155+
Ok(get_bundle_path_for_executable(CHAT_BINARY_NAME).unwrap_or(home_local_bin()?.join(CHAT_BINARY_NAME)))
156+
}
127157
pub async fn quit_fig(verbose: bool) -> Result<ExitCode> {
128158
if fig_util::system_info::in_cloudshell() {
129159
bail!("Restarting {PRODUCT_NAME} is not supported in CloudShell");

0 commit comments

Comments
 (0)