@@ -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 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 {
@@ -6867,7 +6889,7 @@ pub async fn sandbox_policy_get(
68676889 server : & str ,
68686890 name : & str ,
68696891 version : u32 ,
6870- full : bool ,
6892+ view : PolicyGetView ,
68716893 output : & str ,
68726894 tls : & TlsOptions ,
68736895) -> Result < ( ) > {
@@ -6877,7 +6899,7 @@ pub async fn sandbox_policy_get(
68776899 server,
68786900 name,
68796901 version,
6880- full ,
6902+ view ,
68816903 output,
68826904 tls,
68836905 ( & mut stdout, & mut stderr) ,
@@ -6901,7 +6923,7 @@ pub async fn sandbox_policy_get_to_writer<W, E>(
69016923 server : & str ,
69026924 name : & str ,
69036925 version : u32 ,
6904- full : bool ,
6926+ view : PolicyGetView ,
69056927 output : & str ,
69066928 tls : & TlsOptions ,
69076929 writers : ( & mut W , & mut E ) ,
@@ -6911,7 +6933,7 @@ where
69116933 E : Write + Send ,
69126934{
69136935 if version == 0 {
6914- return sandbox_policy_get_effective_to_writer ( server, name, full , output, tls, writers)
6936+ return sandbox_policy_get_effective_to_writer ( server, name, view , output, tls, writers)
69156937 . await ;
69166938 }
69176939
@@ -6938,7 +6960,7 @@ where
69386960 Some ( inner. active_version ) ,
69396961 & rev,
69406962 status,
6941- full ,
6963+ view ,
69426964 ) ?;
69436965 writeln ! (
69446966 stdout,
@@ -6966,10 +6988,11 @@ where
69666988 writeln ! ( stdout, "Error: {}" , rev. load_error) . into_diagnostic ( ) ?;
69676989 }
69686990
6969- if full {
6991+ if view . includes_policy ( ) {
69706992 if let Some ( ref policy) = rev. policy {
69716993 writeln ! ( stdout, "---" ) . into_diagnostic ( ) ?;
6972- let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy)
6994+ let policy = policy_for_view ( policy, view) ;
6995+ let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy. as_ref ( ) )
69736996 . wrap_err ( "failed to serialize policy to YAML" ) ?;
69746997 write ! ( stdout, "{yaml_str}" ) . into_diagnostic ( ) ?;
69756998 } else {
@@ -6987,7 +7010,7 @@ where
69877010async fn sandbox_policy_get_effective_to_writer < W , E > (
69887011 server : & str ,
69897012 name : & str ,
6990- full : bool ,
7013+ view : PolicyGetView ,
69917014 output : & str ,
69927015 tls : & TlsOptions ,
69937016 writers : ( & mut W , & mut E ) ,
@@ -7060,10 +7083,11 @@ where
70607083 serde_json:: json!( config. global_policy_version) ,
70617084 ) ;
70627085 }
7063- if full {
7086+ if view. includes_policy ( ) {
7087+ let policy = policy_for_view ( policy, view) ;
70647088 obj. insert (
70657089 "policy" . to_string ( ) ,
7066- openshell_policy:: sandbox_policy_to_json_value ( policy) ?,
7090+ openshell_policy:: sandbox_policy_to_json_value ( policy. as_ref ( ) ) ?,
70677091 ) ;
70687092 }
70697093 writeln ! (
@@ -7083,9 +7107,10 @@ where
70837107 writeln ! ( stdout, "Global: {}" , config. global_policy_version)
70847108 . into_diagnostic ( ) ?;
70857109 }
7086- if full {
7110+ if view . includes_policy ( ) {
70877111 writeln ! ( stdout, "---" ) . into_diagnostic ( ) ?;
7088- let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy)
7112+ let policy = policy_for_view ( policy, view) ;
7113+ let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy. as_ref ( ) )
70897114 . wrap_err ( "failed to serialize policy to YAML" ) ?;
70907115 write ! ( stdout, "{yaml_str}" ) . into_diagnostic ( ) ?;
70917116 }
@@ -7099,7 +7124,7 @@ where
70997124pub async fn sandbox_policy_get_global (
71007125 server : & str ,
71017126 version : u32 ,
7102- full : bool ,
7127+ view : PolicyGetView ,
71037128 output : & str ,
71047129 tls : & TlsOptions ,
71057130) -> Result < ( ) > {
@@ -7119,7 +7144,7 @@ pub async fn sandbox_policy_get_global(
71197144 let status = PolicyStatus :: try_from ( rev. status ) . unwrap_or ( PolicyStatus :: Unspecified ) ;
71207145 match output {
71217146 "json" => {
7122- let obj = policy_revision_to_json ( "global" , None , None , & rev, status, full ) ?;
7147+ let obj = policy_revision_to_json ( "global" , None , None , & rev, status, view ) ?;
71237148 println ! ( "{}" , serde_json:: to_string_pretty( & obj) . into_diagnostic( ) ?) ;
71247149 return Ok ( ( ) ) ;
71257150 }
@@ -7138,10 +7163,11 @@ pub async fn sandbox_policy_get_global(
71387163 println ! ( "Loaded: {} ms" , rev. loaded_at_ms) ;
71397164 }
71407165
7141- if full {
7166+ if view . includes_policy ( ) {
71427167 if let Some ( ref policy) = rev. policy {
71437168 println ! ( "---" ) ;
7144- let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy)
7169+ let policy = policy_for_view ( policy, view) ;
7170+ let yaml_str = openshell_policy:: serialize_sandbox_policy ( policy. as_ref ( ) )
71457171 . wrap_err ( "failed to serialize policy to YAML" ) ?;
71467172 print ! ( "{yaml_str}" ) ;
71477173 } else {
@@ -7171,7 +7197,7 @@ fn policy_revision_to_json(
71717197 active_version : Option < u32 > ,
71727198 rev : & openshell_core:: proto:: SandboxPolicyRevision ,
71737199 status : PolicyStatus ,
7174- full : bool ,
7200+ view : PolicyGetView ,
71757201) -> Result < serde_json:: Value > {
71767202 let mut obj = serde_json:: Map :: new ( ) ;
71777203 obj. insert ( "scope" . to_string ( ) , serde_json:: json!( scope) ) ;
@@ -7205,16 +7231,31 @@ fn policy_revision_to_json(
72057231 if !rev. load_error . is_empty ( ) {
72067232 obj. insert ( "load_error" . to_string ( ) , serde_json:: json!( rev. load_error) ) ;
72077233 }
7208- if full {
7234+ if view . includes_policy ( ) {
72097235 let policy = match rev. policy . as_ref ( ) {
7210- Some ( policy) => openshell_policy:: sandbox_policy_to_json_value ( policy) ?,
7236+ Some ( policy) => {
7237+ let policy = policy_for_view ( policy, view) ;
7238+ openshell_policy:: sandbox_policy_to_json_value ( policy. as_ref ( ) ) ?
7239+ }
72117240 None => serde_json:: Value :: Null ,
72127241 } ;
72137242 obj. insert ( "policy" . to_string ( ) , policy) ;
72147243 }
72157244 Ok ( serde_json:: Value :: Object ( obj) )
72167245}
72177246
7247+ fn policy_for_view ( policy : & SandboxPolicy , view : PolicyGetView ) -> Cow < ' _ , SandboxPolicy > {
7248+ if view != PolicyGetView :: Base {
7249+ return Cow :: Borrowed ( policy) ;
7250+ }
7251+
7252+ let mut base_policy = policy. clone ( ) ;
7253+ base_policy
7254+ . network_policies
7255+ . retain ( |name, _| !openshell_policy:: is_provider_rule_name ( name) ) ;
7256+ Cow :: Owned ( base_policy)
7257+ }
7258+
72187259pub async fn sandbox_policy_list (
72197260 server : & str ,
72207261 name : & str ,
0 commit comments