@@ -5653,6 +5653,7 @@ pub async fn sandbox_policy_get(
56535653 name : & str ,
56545654 version : u32 ,
56555655 full : bool ,
5656+ json : bool ,
56565657 tls : & TlsOptions ,
56575658) -> Result < ( ) > {
56585659 let mut client = grpc_client ( server, tls) . await ?;
@@ -5669,6 +5670,19 @@ pub async fn sandbox_policy_get(
56695670 let inner = status_resp. into_inner ( ) ;
56705671 if let Some ( rev) = inner. revision {
56715672 let status = PolicyStatus :: try_from ( rev. status ) . unwrap_or ( PolicyStatus :: Unspecified ) ;
5673+ if json {
5674+ let obj = policy_revision_to_json (
5675+ "sandbox" ,
5676+ Some ( name) ,
5677+ Some ( inner. active_version ) ,
5678+ & rev,
5679+ status,
5680+ full,
5681+ ) ?;
5682+ println ! ( "{}" , serde_json:: to_string_pretty( & obj) . into_diagnostic( ) ?) ;
5683+ return Ok ( ( ) ) ;
5684+ }
5685+
56725686 println ! ( "Version: {}" , rev. version) ;
56735687 println ! ( "Hash: {}" , rev. policy_hash) ;
56745688 println ! ( "Status: {status:?}" ) ;
@@ -5704,6 +5718,7 @@ pub async fn sandbox_policy_get_global(
57045718 server : & str ,
57055719 version : u32 ,
57065720 full : bool ,
5721+ json : bool ,
57075722 tls : & TlsOptions ,
57085723) -> Result < ( ) > {
57095724 let mut client = grpc_client ( server, tls) . await ?;
@@ -5720,6 +5735,12 @@ pub async fn sandbox_policy_get_global(
57205735 let inner = status_resp. into_inner ( ) ;
57215736 if let Some ( rev) = inner. revision {
57225737 let status = PolicyStatus :: try_from ( rev. status ) . unwrap_or ( PolicyStatus :: Unspecified ) ;
5738+ if json {
5739+ let obj = policy_revision_to_json ( "global" , None , None , & rev, status, full) ?;
5740+ println ! ( "{}" , serde_json:: to_string_pretty( & obj) . into_diagnostic( ) ?) ;
5741+ return Ok ( ( ) ) ;
5742+ }
5743+
57235744 println ! ( "Scope: global" ) ;
57245745 println ! ( "Version: {}" , rev. version) ;
57255746 println ! ( "Hash: {}" , rev. policy_hash) ;
@@ -5748,6 +5769,66 @@ pub async fn sandbox_policy_get_global(
57485769 Ok ( ( ) )
57495770}
57505771
5772+ fn policy_status_json_name ( status : PolicyStatus ) -> & ' static str {
5773+ match status {
5774+ PolicyStatus :: Unspecified => "unspecified" ,
5775+ PolicyStatus :: Pending => "pending" ,
5776+ PolicyStatus :: Loaded => "loaded" ,
5777+ PolicyStatus :: Failed => "failed" ,
5778+ PolicyStatus :: Superseded => "superseded" ,
5779+ }
5780+ }
5781+
5782+ fn policy_revision_to_json (
5783+ scope : & str ,
5784+ sandbox : Option < & str > ,
5785+ active_version : Option < u32 > ,
5786+ rev : & openshell_core:: proto:: SandboxPolicyRevision ,
5787+ status : PolicyStatus ,
5788+ full : bool ,
5789+ ) -> Result < serde_json:: Value > {
5790+ let mut obj = serde_json:: Map :: new ( ) ;
5791+ obj. insert ( "scope" . to_string ( ) , serde_json:: json!( scope) ) ;
5792+ if let Some ( sandbox) = sandbox {
5793+ obj. insert ( "sandbox" . to_string ( ) , serde_json:: json!( sandbox) ) ;
5794+ }
5795+ obj. insert ( "version" . to_string ( ) , serde_json:: json!( rev. version) ) ;
5796+ obj. insert ( "hash" . to_string ( ) , serde_json:: json!( rev. policy_hash) ) ;
5797+ obj. insert (
5798+ "status" . to_string ( ) ,
5799+ serde_json:: json!( policy_status_json_name( status) ) ,
5800+ ) ;
5801+ if let Some ( active_version) = active_version {
5802+ obj. insert (
5803+ "active_version" . to_string ( ) ,
5804+ serde_json:: json!( active_version) ,
5805+ ) ;
5806+ }
5807+ if rev. created_at_ms > 0 {
5808+ obj. insert (
5809+ "created_at_ms" . to_string ( ) ,
5810+ serde_json:: json!( rev. created_at_ms) ,
5811+ ) ;
5812+ }
5813+ if rev. loaded_at_ms > 0 {
5814+ obj. insert (
5815+ "loaded_at_ms" . to_string ( ) ,
5816+ serde_json:: json!( rev. loaded_at_ms) ,
5817+ ) ;
5818+ }
5819+ if !rev. load_error . is_empty ( ) {
5820+ obj. insert ( "load_error" . to_string ( ) , serde_json:: json!( rev. load_error) ) ;
5821+ }
5822+ if full {
5823+ let policy = match rev. policy . as_ref ( ) {
5824+ Some ( policy) => openshell_policy:: sandbox_policy_to_json_value ( policy) ?,
5825+ None => serde_json:: Value :: Null ,
5826+ } ;
5827+ obj. insert ( "policy" . to_string ( ) , policy) ;
5828+ }
5829+ Ok ( serde_json:: Value :: Object ( obj) )
5830+ }
5831+
57515832pub async fn sandbox_policy_list (
57525833 server : & str ,
57535834 name : & str ,
0 commit comments