@@ -5653,6 +5653,7 @@ pub async fn sandbox_policy_get(
56535653 name : & str ,
56545654 version : u32 ,
56555655 full : bool ,
5656+ output : & str ,
56565657 tls : & TlsOptions ,
56575658) -> Result < ( ) > {
56585659 let mut client = grpc_client ( server, tls) . await ?;
@@ -5669,6 +5670,23 @@ 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+ match output {
5674+ "json" => {
5675+ let obj = policy_revision_to_json (
5676+ "sandbox" ,
5677+ Some ( name) ,
5678+ Some ( inner. active_version ) ,
5679+ & rev,
5680+ status,
5681+ full,
5682+ ) ?;
5683+ println ! ( "{}" , serde_json:: to_string_pretty( & obj) . into_diagnostic( ) ?) ;
5684+ return Ok ( ( ) ) ;
5685+ }
5686+ "table" => { }
5687+ _ => return Err ( miette ! ( "unsupported output format: {output}" ) ) ,
5688+ }
5689+
56725690 println ! ( "Version: {}" , rev. version) ;
56735691 println ! ( "Hash: {}" , rev. policy_hash) ;
56745692 println ! ( "Status: {status:?}" ) ;
@@ -5704,6 +5722,7 @@ pub async fn sandbox_policy_get_global(
57045722 server : & str ,
57055723 version : u32 ,
57065724 full : bool ,
5725+ output : & str ,
57075726 tls : & TlsOptions ,
57085727) -> Result < ( ) > {
57095728 let mut client = grpc_client ( server, tls) . await ?;
@@ -5720,6 +5739,16 @@ pub async fn sandbox_policy_get_global(
57205739 let inner = status_resp. into_inner ( ) ;
57215740 if let Some ( rev) = inner. revision {
57225741 let status = PolicyStatus :: try_from ( rev. status ) . unwrap_or ( PolicyStatus :: Unspecified ) ;
5742+ match output {
5743+ "json" => {
5744+ let obj = policy_revision_to_json ( "global" , None , None , & rev, status, full) ?;
5745+ println ! ( "{}" , serde_json:: to_string_pretty( & obj) . into_diagnostic( ) ?) ;
5746+ return Ok ( ( ) ) ;
5747+ }
5748+ "table" => { }
5749+ _ => return Err ( miette ! ( "unsupported output format: {output}" ) ) ,
5750+ }
5751+
57235752 println ! ( "Scope: global" ) ;
57245753 println ! ( "Version: {}" , rev. version) ;
57255754 println ! ( "Hash: {}" , rev. policy_hash) ;
@@ -5748,6 +5777,66 @@ pub async fn sandbox_policy_get_global(
57485777 Ok ( ( ) )
57495778}
57505779
5780+ fn policy_status_json_name ( status : PolicyStatus ) -> & ' static str {
5781+ match status {
5782+ PolicyStatus :: Unspecified => "unspecified" ,
5783+ PolicyStatus :: Pending => "pending" ,
5784+ PolicyStatus :: Loaded => "loaded" ,
5785+ PolicyStatus :: Failed => "failed" ,
5786+ PolicyStatus :: Superseded => "superseded" ,
5787+ }
5788+ }
5789+
5790+ fn policy_revision_to_json (
5791+ scope : & str ,
5792+ sandbox : Option < & str > ,
5793+ active_version : Option < u32 > ,
5794+ rev : & openshell_core:: proto:: SandboxPolicyRevision ,
5795+ status : PolicyStatus ,
5796+ full : bool ,
5797+ ) -> Result < serde_json:: Value > {
5798+ let mut obj = serde_json:: Map :: new ( ) ;
5799+ obj. insert ( "scope" . to_string ( ) , serde_json:: json!( scope) ) ;
5800+ if let Some ( sandbox) = sandbox {
5801+ obj. insert ( "sandbox" . to_string ( ) , serde_json:: json!( sandbox) ) ;
5802+ }
5803+ obj. insert ( "version" . to_string ( ) , serde_json:: json!( rev. version) ) ;
5804+ obj. insert ( "hash" . to_string ( ) , serde_json:: json!( rev. policy_hash) ) ;
5805+ obj. insert (
5806+ "status" . to_string ( ) ,
5807+ serde_json:: json!( policy_status_json_name( status) ) ,
5808+ ) ;
5809+ if let Some ( active_version) = active_version {
5810+ obj. insert (
5811+ "active_version" . to_string ( ) ,
5812+ serde_json:: json!( active_version) ,
5813+ ) ;
5814+ }
5815+ if rev. created_at_ms > 0 {
5816+ obj. insert (
5817+ "created_at_ms" . to_string ( ) ,
5818+ serde_json:: json!( rev. created_at_ms) ,
5819+ ) ;
5820+ }
5821+ if rev. loaded_at_ms > 0 {
5822+ obj. insert (
5823+ "loaded_at_ms" . to_string ( ) ,
5824+ serde_json:: json!( rev. loaded_at_ms) ,
5825+ ) ;
5826+ }
5827+ if !rev. load_error . is_empty ( ) {
5828+ obj. insert ( "load_error" . to_string ( ) , serde_json:: json!( rev. load_error) ) ;
5829+ }
5830+ if full {
5831+ let policy = match rev. policy . as_ref ( ) {
5832+ Some ( policy) => openshell_policy:: sandbox_policy_to_json_value ( policy) ?,
5833+ None => serde_json:: Value :: Null ,
5834+ } ;
5835+ obj. insert ( "policy" . to_string ( ) , policy) ;
5836+ }
5837+ Ok ( serde_json:: Value :: Object ( obj) )
5838+ }
5839+
57515840pub async fn sandbox_policy_list (
57525841 server : & str ,
57535842 name : & str ,
0 commit comments