@@ -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,45 @@ fn main() {
113115 }
114116 String :: new ( )
115117 } ,
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+ let instances = vec ! [
127+ Get {
128+ name : Some ( "one" . to_string( ) ) ,
129+ id: Some ( 1 ) ,
130+ } ,
131+ Get {
132+ name : Some ( "two" . to_string( ) ) ,
133+ id: Some ( 2 ) ,
134+ } ,
135+ Get {
136+ name : Some ( "three" . to_string( ) ) ,
137+ id: Some ( 3 ) ,
138+ } ,
139+ ] ;
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 == Some ( name. clone ( ) ) ) . 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) ;
149+ std:: process:: exit ( 1 ) ;
150+ } )
151+ } else {
152+ eprintln ! ( "No name or id provided in input" ) ;
153+ std:: process:: exit ( 1 ) ;
154+ } ;
155+ serde_json:: to_string ( & resource) . unwrap ( )
156+ } ,
116157 SubCommand :: InDesiredState { input } => {
117158 let mut in_desired_state = match serde_json:: from_str :: < in_desired_state:: InDesiredState > ( & input) {
118159 Ok ( in_desired_state) => in_desired_state,
@@ -162,6 +203,9 @@ fn main() {
162203 Schemas :: Exporter => {
163204 schema_for ! ( Exporter )
164205 } ,
206+ Schemas :: Get => {
207+ schema_for ! ( Get )
208+ } ,
165209 Schemas :: InDesiredState => {
166210 schema_for ! ( InDesiredState )
167211 } ,
0 commit comments