@@ -116,13 +116,6 @@ fn main() {
116
116
String :: new ( )
117
117
} ,
118
118
SubCommand :: Get { input } => {
119
- let get = match serde_json:: from_str :: < Get > ( & input) {
120
- Ok ( get) => get,
121
- Err ( err) => {
122
- eprintln ! ( "Error JSON does not match schema: {err}" ) ;
123
- std:: process:: exit ( 1 ) ;
124
- }
125
- } ;
126
119
let instances = vec ! [
127
120
Get {
128
121
name : Some ( "one" . to_string( ) ) ,
@@ -137,20 +130,38 @@ fn main() {
137
130
id: Some ( 3 ) ,
138
131
} ,
139
132
] ;
140
- // depending on the input, return the appropriate instance whether it is name or id or both
141
- let resource = if let Some ( name) = get. name {
142
- instances. into_iter ( ) . find ( |i| i. name . as_ref ( ) == Some ( & name) ) . unwrap_or_else ( || {
143
- eprintln ! ( "No instance found with name: {name}" ) ;
144
- std:: process:: exit ( 1 ) ;
145
- } )
146
- } else if let Some ( id) = get. id {
147
- instances. into_iter ( ) . find ( |i| i. id == Some ( id) ) . unwrap_or_else ( || {
148
- eprintln ! ( "No instance found with id: {id}" ) ;
133
+
134
+ let resource = if input. is_empty ( ) {
135
+ // If neither name nor id is provided, return the first instance
136
+ instances. into_iter ( ) . next ( ) . unwrap_or_else ( || {
137
+ eprintln ! ( "No instances found" ) ;
149
138
std:: process:: exit ( 1 ) ;
150
139
} )
151
140
} else {
152
- eprintln ! ( "No name or id provided in input" ) ;
153
- std:: process:: exit ( 1 ) ;
141
+ let get = match serde_json:: from_str :: < Get > ( & input) {
142
+ Ok ( get) => get,
143
+ Err ( err) => {
144
+ eprintln ! ( "Error JSON does not match schema: {err}" ) ;
145
+ std:: process:: exit ( 1 ) ;
146
+ }
147
+ } ;
148
+ // depending on the input, return the appropriate instance whether it is name or id or both
149
+ if let Some ( name) = get. name {
150
+ instances. into_iter ( ) . find ( |i| i. name . as_ref ( ) == Some ( & name) ) . unwrap_or_else ( || {
151
+ eprintln ! ( "No instance found with name: {name}" ) ;
152
+ std:: process:: exit ( 1 ) ;
153
+ } )
154
+ } else if let Some ( id) = get. id {
155
+ instances. into_iter ( ) . find ( |i| i. id == Some ( id) ) . unwrap_or_else ( || {
156
+ eprintln ! ( "No instance found with id: {id}" ) ;
157
+ std:: process:: exit ( 1 ) ;
158
+ } )
159
+ } else {
160
+ instances. into_iter ( ) . next ( ) . unwrap_or_else ( || {
161
+ eprintln ! ( "No instances found" ) ;
162
+ std:: process:: exit ( 1 ) ;
163
+ } )
164
+ }
154
165
} ;
155
166
serde_json:: to_string ( & resource) . unwrap ( )
156
167
} ,
0 commit comments