@@ -5,25 +5,23 @@ use clap::Args;
55use icp:: context:: { CanisterSelection , EnvironmentSelection , NetworkSelection } ;
66use icp:: identity:: IdentitySelection ;
77
8- use crate :: options:: IdentityOpt ;
8+ use crate :: options:: { EnvironmentOpt , IdentityOpt , NetworkOpt } ;
99
1010#[ derive( Args , Debug ) ]
1111pub ( crate ) struct CanisterEnvironmentArgs {
1212 /// Name or principal of canister to target
1313 /// When using a name an environment must be specified
1414 pub ( crate ) canister : Canister ,
1515
16- /// Name of the target environment
17- #[ arg( long) ]
18- pub ( crate ) environment : Option < Environment > ,
16+ #[ command( flatten) ]
17+ pub ( crate ) environment : EnvironmentOpt ,
1918}
2019
2120impl CanisterEnvironmentArgs {
2221 /// Convert arguments into selection enums for canister and environment
2322 pub ( crate ) fn selections ( & self ) -> ( CanisterSelection , EnvironmentSelection ) {
2423 let canister_selection: CanisterSelection = self . canister . clone ( ) . into ( ) ;
25- let environment_selection: EnvironmentSelection =
26- self . environment . clone ( ) . unwrap_or_default ( ) . into ( ) ;
24+ let environment_selection: EnvironmentSelection = self . environment . clone ( ) . into ( ) ;
2725 ( canister_selection, environment_selection)
2826 }
2927}
@@ -35,15 +33,12 @@ pub(crate) struct CanisterCommandArgs {
3533 /// When using a name an environment must be specified
3634 pub ( crate ) canister : Canister ,
3735
38- /// Name of the network to target, conflicts with environment argument
39- #[ arg( long, conflicts_with = "environment" ) ]
40- pub ( crate ) network : Option < Network > ,
36+ #[ command( flatten) ]
37+ pub ( crate ) network : NetworkOpt ,
4138
42- /// Name of the target environment
43- #[ arg( long) ]
44- pub ( crate ) environment : Option < Environment > ,
39+ #[ command( flatten) ]
40+ pub ( crate ) environment : EnvironmentOpt ,
4541
46- /// The identity to use for this request
4742 #[ command( flatten) ]
4843 pub ( crate ) identity : IdentityOpt ,
4944}
@@ -60,12 +55,8 @@ impl CanisterCommandArgs {
6055 /// Convert command arguments into selection enums
6156 pub ( crate ) fn selections ( & self ) -> CommandSelections {
6257 let canister_selection: CanisterSelection = self . canister . clone ( ) . into ( ) ;
63- let environment_selection: EnvironmentSelection =
64- self . environment . clone ( ) . unwrap_or_default ( ) . into ( ) ;
65- let network_selection: NetworkSelection = match self . network . clone ( ) {
66- Some ( network) => network. into_selection ( ) ,
67- None => NetworkSelection :: FromEnvironment ,
68- } ;
58+ let environment_selection: EnvironmentSelection = self . environment . clone ( ) . into ( ) ;
59+ let network_selection: NetworkSelection = self . network . clone ( ) . into ( ) ;
6960 let identity_selection: IdentitySelection = self . identity . clone ( ) . into ( ) ;
7061
7162 CommandSelections {
@@ -111,73 +102,6 @@ impl Display for Canister {
111102 }
112103}
113104
114- #[ derive( Clone , Debug , PartialEq ) ]
115- pub ( crate ) enum Network {
116- Name ( String ) ,
117- Url ( String ) ,
118- }
119-
120- impl From < & str > for Network {
121- fn from ( v : & str ) -> Self {
122- if v. starts_with ( "http://" ) || v. starts_with ( "https://" ) {
123- return Self :: Url ( v. to_string ( ) ) ;
124- }
125-
126- Self :: Name ( v. to_string ( ) )
127- }
128- }
129-
130- impl Network {
131- pub ( crate ) fn into_selection ( self ) -> NetworkSelection {
132- match self {
133- Network :: Name ( name) => NetworkSelection :: Named ( name) ,
134- Network :: Url ( url) => NetworkSelection :: Url ( url) ,
135- }
136- }
137- }
138-
139- #[ derive( Clone , Debug , PartialEq ) ]
140- pub ( crate ) enum Environment {
141- Name ( String ) ,
142- Default ( String ) ,
143- }
144-
145- impl Environment {
146- pub ( crate ) fn name ( & self ) -> & str {
147- match self {
148- Environment :: Name ( name) => name,
149- Environment :: Default ( name) => name,
150- }
151- }
152- }
153-
154- impl Default for Environment {
155- fn default ( ) -> Self {
156- Self :: Default ( "local" . to_string ( ) )
157- }
158- }
159-
160- impl From < & str > for Environment {
161- fn from ( v : & str ) -> Self {
162- Self :: Name ( v. to_string ( ) )
163- }
164- }
165-
166- impl From < Environment > for EnvironmentSelection {
167- fn from ( v : Environment ) -> Self {
168- match v {
169- Environment :: Name ( name) => EnvironmentSelection :: Named ( name) ,
170- Environment :: Default ( _) => EnvironmentSelection :: Default ,
171- }
172- }
173- }
174-
175- impl Display for Environment {
176- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
177- write ! ( f, "{}" , self . name( ) )
178- }
179- }
180-
181105#[ cfg( test) ]
182106mod tests {
183107 use candid:: Principal ;
@@ -201,22 +125,4 @@ mod tests {
201125 Canister :: Principal ( Principal :: from_text( cid) . expect( "failed to parse principal" ) ) ,
202126 ) ;
203127 }
204-
205- #[ test]
206- fn network_by_name ( ) {
207- assert_eq ! (
208- Network :: from( "my-network" ) ,
209- Network :: Name ( "my-network" . to_string( ) ) ,
210- ) ;
211- }
212-
213- #[ test]
214- fn network_by_url_http ( ) {
215- let url = "http://www.example.com" ;
216-
217- assert_eq ! (
218- Network :: from( url) ,
219- Network :: Url ( "http://www.example.com" . to_string( ) ) ,
220- ) ;
221- }
222128}
0 commit comments