@@ -7,6 +7,7 @@ mod exist;
77mod exit_code;
88mod export;
99mod exporter;
10+ mod get;
1011mod in_desired_state;
1112mod metadata;
1213mod sleep;
@@ -22,6 +23,7 @@ use crate::exist::{Exist, State};
2223use crate :: exit_code:: ExitCode ;
2324use crate :: export:: Export ;
2425use crate :: exporter:: { Exporter , Resource } ;
26+ use crate :: get:: Get ;
2527use crate :: in_desired_state:: InDesiredState ;
2628use crate :: metadata:: Metadata ;
2729use crate :: sleep:: Sleep ;
@@ -113,6 +115,56 @@ fn main() {
113115 }
114116 String :: new ( )
115117 } ,
118+ SubCommand :: Get { input } => {
119+ let instances = vec ! [
120+ Get {
121+ name : Some ( "one" . to_string( ) ) ,
122+ id: Some ( 1 ) ,
123+ } ,
124+ Get {
125+ name : Some ( "two" . to_string( ) ) ,
126+ id: Some ( 2 ) ,
127+ } ,
128+ Get {
129+ name : Some ( "three" . to_string( ) ) ,
130+ id: Some ( 3 ) ,
131+ } ,
132+ ] ;
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" ) ;
138+ std:: process:: exit ( 1 ) ;
139+ } )
140+ } else {
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+ }
165+ } ;
166+ serde_json:: to_string ( & resource) . unwrap ( )
167+ } ,
116168 SubCommand :: InDesiredState { input } => {
117169 let mut in_desired_state = match serde_json:: from_str :: < in_desired_state:: InDesiredState > ( & input) {
118170 Ok ( in_desired_state) => in_desired_state,
@@ -162,6 +214,9 @@ fn main() {
162214 Schemas :: Exporter => {
163215 schema_for ! ( Exporter )
164216 } ,
217+ Schemas :: Get => {
218+ schema_for ! ( Get )
219+ } ,
165220 Schemas :: InDesiredState => {
166221 schema_for ! ( InDesiredState )
167222 } ,
0 commit comments