@@ -6,6 +6,7 @@ use clap::{
66 Args ,
77 Parser ,
88 Subcommand ,
9+ ValueEnum ,
910} ;
1011use eyre:: {
1112 Result ,
@@ -35,13 +36,24 @@ use crate::util::desktop::{
3536use crate :: util:: {
3637 CliContext ,
3738 app_not_running_message,
39+ qchat_path,
3840} ;
3941
4042#[ derive( Debug , Subcommand , PartialEq , Eq ) ]
4143pub enum SettingsSubcommands {
4244 /// Open the settings file
4345 Open ,
44- /// List all the settings
46+ /// List settings
47+ List {
48+ /// Show all available settings
49+ #[ arg( long) ]
50+ all : bool ,
51+ /// Format of the output
52+ #[ arg( long, short, value_enum, default_value_t) ]
53+ format : OutputFormat ,
54+ } ,
55+ /// List configured settings
56+ #[ command( hide = true ) ]
4557 All {
4658 /// Format of the output
4759 #[ arg( long, short, value_enum, default_value_t) ]
@@ -52,15 +64,15 @@ pub enum SettingsSubcommands {
5264#[ derive( Debug , Args , PartialEq , Eq ) ]
5365#[ command( subcommand_negates_reqs = true ) ]
5466#[ command( args_conflicts_with_subcommands = true ) ]
55- #[ command( group( ArgGroup :: new( "vals" ) . requires( "key" ) . args( & [ "value" , "delete" , " format"] ) ) ) ]
67+ #[ command( group( ArgGroup :: new( "vals" ) . requires( "key" ) . args( & [ "value" , "format" ] ) ) ) ]
5668pub struct SettingsArgs {
5769 #[ command( subcommand) ]
5870 cmd : Option < SettingsSubcommands > ,
5971 /// key
6072 key : Option < String > ,
6173 /// value
6274 value : Option < String > ,
63- /// Delete a value
75+ /// Delete a key (No value needed)
6476 #[ arg( long, short) ]
6577 delete : bool ,
6678 /// Format of the output
@@ -89,25 +101,45 @@ impl SettingsArgs {
89101 bail ! ( "The EDITOR environment variable is not set" )
90102 }
91103 } ,
104+ Some ( SettingsSubcommands :: List { all, format } ) => {
105+ let mut args = vec ! [ "settings" . to_string( ) , "list" . to_string( ) ] ;
106+ if all {
107+ args. push ( "--all" . to_string ( ) ) ;
108+ }
109+ if format != OutputFormat :: default ( ) {
110+ args. push ( "--format" . to_string ( ) ) ;
111+ args. push ( format. to_possible_value ( ) . unwrap ( ) . get_name ( ) . to_string ( ) ) ;
112+ }
113+
114+ let status = tokio:: process:: Command :: new ( qchat_path ( ) ?) . args ( & args) . status ( ) . await ?;
115+
116+ Ok ( if status. success ( ) {
117+ ExitCode :: SUCCESS
118+ } else {
119+ ExitCode :: FAILURE
120+ } )
121+ } ,
92122 Some ( SettingsSubcommands :: All { format } ) => {
93- let settings = fig_settings :: OldSettings :: load ( ) ? . map ( ) . clone ( ) ;
123+ let mut args = vec ! [ "settings" . to_string ( ) , "list" . to_string ( ) ] ;
94124
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- } ,
125+ if format != OutputFormat :: default ( ) {
126+ args. push ( "--format" . to_string ( ) ) ;
127+ args. push ( format. to_possible_value ( ) . unwrap ( ) . get_name ( ) . to_string ( ) ) ;
105128 }
106129
107- Ok ( ExitCode :: SUCCESS )
130+ let status = tokio:: process:: Command :: new ( qchat_path ( ) ?) . args ( & args) . status ( ) . await ?;
131+
132+ Ok ( if status. success ( ) {
133+ ExitCode :: SUCCESS
134+ } else {
135+ ExitCode :: FAILURE
136+ } )
108137 } ,
109138 None => match & self . key {
110139 Some ( key) => match ( & self . value , self . delete ) {
140+ ( Some ( _) , true ) => Err ( eyre:: eyre!(
141+ "the argument '--delete' cannot be used with '[VALUE]'\n Usage: q settings --delete {key}"
142+ ) ) ,
111143 ( None , false ) => match fig_settings:: settings:: get_value ( key) ? {
112144 Some ( value) => {
113145 match self . format {
@@ -161,9 +193,14 @@ impl SettingsArgs {
161193
162194 Ok ( ExitCode :: SUCCESS )
163195 } ,
164- _ => Ok ( ExitCode :: SUCCESS ) ,
165196 } ,
166197 None => {
198+ if self . delete {
199+ return Err ( eyre:: eyre!(
200+ "the argument '--delete' requires a <KEY>\n Usage: q settings --delete <KEY>"
201+ ) ) ;
202+ }
203+
167204 if manifest:: is_minimal ( ) || system_info:: is_remote ( ) {
168205 Cli :: parse_from ( [ CLI_BINARY_NAME , "settings" , "--help" ] ) ;
169206 return Ok ( ExitCode :: SUCCESS ) ;
0 commit comments