@@ -63,6 +63,7 @@ use openshell_providers::{
6363 profile_to_json, profile_to_yaml, profiles_to_json, profiles_to_yaml,
6464} ;
6565use owo_colors:: OwoColorize ;
66+ use std:: borrow:: Cow ;
6667use std:: collections:: { HashMap , HashSet } ;
6768use std:: io:: { ErrorKind , IsTerminal , Read , Write } ;
6869use std:: path:: { Path , PathBuf } ;
@@ -80,6 +81,27 @@ pub use openshell_core::forward::{
8081 ForwardSpec , find_forward_by_port, list_forwards, stop_forward, stop_forwards_for_sandbox,
8182} ;
8283
84+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
85+ pub enum PolicyGetView {
86+ Metadata ,
87+ Base ,
88+ Full ,
89+ }
90+
91+ impl PolicyGetView {
92+ pub fn from_flags ( base : bool , full : bool ) -> Self {
93+ match ( base, full) {
94+ ( true , _) => Self :: Base ,
95+ ( false , true ) => Self :: Full ,
96+ ( false , false ) => Self :: Metadata ,
97+ }
98+ }
99+
100+ fn includes_policy ( self ) -> bool {
101+ matches ! ( self , Self :: Base | Self :: Full )
102+ }
103+ }
104+
83105#[ derive( Debug , PartialEq , Eq ) ]
84106enum SandboxUploadPlan {
85107 GitAware {
@@ -6919,7 +6941,7 @@ pub async fn sandbox_policy_get(
69196941 server : & str ,
69206942 name : & str ,
69216943 version : u32 ,
6922- full : bool ,
6944+ view : PolicyGetView ,
69236945 output : & str ,
69246946 tls : & TlsOptions ,
69256947) -> Result < ( ) > {
@@ -6929,7 +6951,7 @@ pub async fn sandbox_policy_get(
69296951 server,
69306952 name,
69316953 version,
6932- full ,
6954+ view ,
69336955 output,
69346956 tls,
69356957 ( & mut stdout, & mut stderr) ,
@@ -6953,7 +6975,7 @@ pub async fn sandbox_policy_get_to_writer<W, E>(
69536975 server : & str ,
69546976 name : & str ,
69556977 version : u32 ,
6956- full : bool ,
6978+ view : PolicyGetView ,
69576979 output : & str ,
69586980 tls : & TlsOptions ,
69596981 writers : ( & mut W , & mut E ) ,
@@ -6963,7 +6985,7 @@ where
69636985 E : Write + Send ,
69646986{
69656987 if version == 0 {
6966- return sandbox_policy_get_effective_to_writer ( server, name, full , output, tls, writers)
6988+ return sandbox_policy_get_effective_to_writer ( server, name, view , output, tls, writers)
69676989 . await ;
69686990 }
69696991
@@ -6990,7 +7012,7 @@ where
69907012 Some ( inner. active_version ) ,
69917013 & rev,
69927014 status,
6993- full ,
7015+ view ,
69947016 ) ?;
69957017 writeln ! (
69967018 stdout,
@@ -7018,10 +7040,11 @@ where
70187040 writeln ! ( stdout, "Error: {}" , rev. load_error) . into_diagnostic ( ) ?;
70197041 }
70207042
7021- if full {
7043+ if view . includes_policy ( ) {
70227044 if let Some ( ref policy) = rev. policy {
70237045 writeln ! ( stdout, "---" ) . into_diagnostic ( ) ?;
7024- let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy)
7046+ let policy = policy_for_view ( policy, view) ;
7047+ let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy. as_ref ( ) )
70257048 . wrap_err ( "failed to serialize policy to YAML" ) ?;
70267049 write ! ( stdout, "{yaml_str}" ) . into_diagnostic ( ) ?;
70277050 } else {
@@ -7039,7 +7062,7 @@ where
70397062async fn sandbox_policy_get_effective_to_writer < W , E > (
70407063 server : & str ,
70417064 name : & str ,
7042- full : bool ,
7065+ view : PolicyGetView ,
70437066 output : & str ,
70447067 tls : & TlsOptions ,
70457068 writers : ( & mut W , & mut E ) ,
@@ -7112,10 +7135,11 @@ where
71127135 serde_json:: json!( config. global_policy_version) ,
71137136 ) ;
71147137 }
7115- if full {
7138+ if view. includes_policy ( ) {
7139+ let policy = policy_for_view ( policy, view) ;
71167140 obj. insert (
71177141 "policy" . to_string ( ) ,
7118- openshell_policy:: sandbox_policy_to_json_value ( policy) ?,
7142+ openshell_policy:: sandbox_policy_to_json_value ( policy. as_ref ( ) ) ?,
71197143 ) ;
71207144 }
71217145 writeln ! (
@@ -7135,9 +7159,10 @@ where
71357159 writeln ! ( stdout, "Global: {}" , config. global_policy_version)
71367160 . into_diagnostic ( ) ?;
71377161 }
7138- if full {
7162+ if view . includes_policy ( ) {
71397163 writeln ! ( stdout, "---" ) . into_diagnostic ( ) ?;
7140- let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy)
7164+ let policy = policy_for_view ( policy, view) ;
7165+ let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy. as_ref ( ) )
71417166 . wrap_err ( "failed to serialize policy to YAML" ) ?;
71427167 write ! ( stdout, "{yaml_str}" ) . into_diagnostic ( ) ?;
71437168 }
@@ -7151,7 +7176,7 @@ where
71517176pub async fn sandbox_policy_get_global (
71527177 server : & str ,
71537178 version : u32 ,
7154- full : bool ,
7179+ view : PolicyGetView ,
71557180 output : & str ,
71567181 tls : & TlsOptions ,
71577182) -> Result < ( ) > {
@@ -7171,7 +7196,7 @@ pub async fn sandbox_policy_get_global(
71717196 let status = PolicyStatus :: try_from ( rev. status ) . unwrap_or ( PolicyStatus :: Unspecified ) ;
71727197 match output {
71737198 "json" => {
7174- let obj = policy_revision_to_json ( "global" , None , None , & rev, status, full ) ?;
7199+ let obj = policy_revision_to_json ( "global" , None , None , & rev, status, view ) ?;
71757200 println ! ( "{}" , serde_json:: to_string_pretty( & obj) . into_diagnostic( ) ?) ;
71767201 return Ok ( ( ) ) ;
71777202 }
@@ -7190,10 +7215,11 @@ pub async fn sandbox_policy_get_global(
71907215 println ! ( "Loaded: {} ms" , rev. loaded_at_ms) ;
71917216 }
71927217
7193- if full {
7218+ if view . includes_policy ( ) {
71947219 if let Some ( ref policy) = rev. policy {
71957220 println ! ( "---" ) ;
7196- let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy)
7221+ let policy = policy_for_view ( policy, view) ;
7222+ let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy. as_ref ( ) )
71977223 . wrap_err ( "failed to serialize policy to YAML" ) ?;
71987224 print ! ( "{yaml_str}" ) ;
71997225 } else {
@@ -7223,7 +7249,7 @@ fn policy_revision_to_json(
72237249 active_version : Option < u32 > ,
72247250 rev : & openshell_core:: proto:: SandboxPolicyRevision ,
72257251 status : PolicyStatus ,
7226- full : bool ,
7252+ view : PolicyGetView ,
72277253) -> Result < serde_json:: Value > {
72287254 let mut obj = serde_json:: Map :: new ( ) ;
72297255 obj. insert ( "scope" . to_string ( ) , serde_json:: json!( scope) ) ;
@@ -7257,16 +7283,31 @@ fn policy_revision_to_json(
72577283 if !rev. load_error . is_empty ( ) {
72587284 obj. insert ( "load_error" . to_string ( ) , serde_json:: json!( rev. load_error) ) ;
72597285 }
7260- if full {
7286+ if view . includes_policy ( ) {
72617287 let policy = match rev. policy . as_ref ( ) {
7262- Some ( policy) => openshell_policy:: sandbox_policy_to_json_value ( policy) ?,
7288+ Some ( policy) => {
7289+ let policy = policy_for_view ( policy, view) ;
7290+ openshell_policy:: sandbox_policy_to_json_value ( policy. as_ref ( ) ) ?
7291+ }
72637292 None => serde_json:: Value :: Null ,
72647293 } ;
72657294 obj. insert ( "policy" . to_string ( ) , policy) ;
72667295 }
72677296 Ok ( serde_json:: Value :: Object ( obj) )
72687297}
72697298
7299+ fn policy_for_view ( policy : & SandboxPolicy , view : PolicyGetView ) -> Cow < ' _ , SandboxPolicy > {
7300+ if view != PolicyGetView :: Base {
7301+ return Cow :: Borrowed ( policy) ;
7302+ }
7303+
7304+ let mut base_policy = policy. clone ( ) ;
7305+ base_policy
7306+ . network_policies
7307+ . retain ( |name, _| !openshell_policy:: is_provider_rule_name ( name) ) ;
7308+ Cow :: Owned ( base_policy)
7309+ }
7310+
72707311pub async fn sandbox_policy_list (
72717312 server : & str ,
72727313 name : & str ,
0 commit comments