@@ -63,9 +63,12 @@ pub struct GetArgs {
63
63
64
64
#[ derive( Args , Debug ) ]
65
65
pub struct LoadArgs {
66
- /// Process to start
67
- #[ arg( required = true ) ]
68
- process : String ,
66
+ /// Globally set variable
67
+ #[ arg( required = false , long, short) ]
68
+ global : bool ,
69
+ /// Process to start, not required if --global flag is set
70
+ #[ arg( required_unless_present = "global" ) ]
71
+ process : Option < String > ,
69
72
/// Relative or absolute path to file to read variables from.
70
73
/// Note that it must in .env format
71
74
#[ arg( long, short, default_value = ".env" ) ]
@@ -81,9 +84,12 @@ pub struct SetArgs {
81
84
/// Value for environment variable
82
85
#[ arg( required = true ) ]
83
86
value : String ,
84
- /// Process to start
85
- #[ arg( required = true ) ]
86
- process : String ,
87
+ /// Globally set variable
88
+ #[ arg( required = false , long, short) ]
89
+ global : bool ,
90
+ /// Process to start, not required if --global flag is set
91
+ #[ arg( required_unless_present = "global" ) ]
92
+ process : Option < String > ,
87
93
}
88
94
89
95
/// Args for delete command
@@ -92,9 +98,19 @@ pub struct DeleteArgs {
92
98
/// Environment variable name
93
99
#[ arg( required = true ) ]
94
100
key : String ,
95
- /// Process to start
96
- #[ arg( required = true ) ]
97
- process : String ,
101
+ /// Globally set variable
102
+ #[ arg( required = false , long, short) ]
103
+ global : bool ,
104
+ /// Process to start, not required if --global flag is set
105
+ #[ arg( required_unless_present = "global" ) ]
106
+ process : Option < String > ,
107
+ }
108
+
109
+ fn validate_var_name ( name : & str ) -> Result < ( ) , String > {
110
+ if name. contains ( ' ' ) {
111
+ return Err ( "Variable name cannot contain spaces" . into ( ) ) ;
112
+ }
113
+ Ok ( ( ) )
98
114
}
99
115
100
116
fn main ( ) {
@@ -151,37 +167,73 @@ fn main() {
151
167
match dotenv_parser:: parse_dotenv ( & content) {
152
168
Ok ( variables) => {
153
169
for ( key, value) in variables. into_iter ( ) {
154
- unsafe { env:: set_var ( key, value) } ;
170
+ if opt. global {
171
+ if let Err ( err) = globalenv:: set_var ( & key, & value) {
172
+ error ( & format ! ( "can't globally set variables: {}" , err) , cli. exit_on_error ) ;
173
+ }
174
+ } else {
175
+ unsafe { env:: set_var ( key, value) } ;
176
+ }
177
+ }
178
+ if let Some ( process) = opt. process {
179
+ run ( process, cli. exit_on_error ) ;
155
180
}
156
- run ( opt. process , cli. exit_on_error ) ;
157
181
}
158
182
Err ( err) => {
159
183
error ( err. to_string ( ) . as_str ( ) , cli. exit_on_error ) ;
160
- run ( opt. process , cli. exit_on_error ) ;
184
+ if let Some ( process) = opt. process {
185
+ run ( process, cli. exit_on_error ) ;
186
+ }
161
187
process:: exit ( 1 ) ;
162
188
}
163
189
}
164
190
}
165
191
Err ( err) => {
166
192
error ( err. to_string ( ) . as_str ( ) , cli. exit_on_error ) ;
167
- run ( opt. process , cli. exit_on_error ) ;
193
+ if let Some ( process) = opt. process {
194
+ run ( process, cli. exit_on_error ) ;
195
+ }
168
196
process:: exit ( 1 ) ;
169
197
}
170
198
}
171
199
}
172
200
// Set command handler
173
201
Commands :: Set ( opt) => {
174
- unsafe { env:: set_var ( opt. key , opt. value ) } ;
175
- run ( opt. process , cli. exit_on_error ) ;
202
+ if let Err ( err) = validate_var_name ( & opt. key ) {
203
+ error ( & err, cli. exit_on_error ) ;
204
+ process:: exit ( 1 ) ;
205
+ }
206
+
207
+ if opt. global {
208
+ if let Err ( err) = globalenv:: set_var ( & opt. key , & opt. value ) {
209
+ error ( & format ! ( "can't globally set variable: {}" , err) , cli. exit_on_error ) ;
210
+ process:: exit ( 1 ) ;
211
+ }
212
+ } else {
213
+ unsafe { env:: set_var ( opt. key , opt. value ) } ;
214
+ }
215
+ if let Some ( process) = opt. process {
216
+ run ( process, cli. exit_on_error ) ;
217
+ }
176
218
}
177
219
// Delete command handler
178
220
Commands :: Delete ( opt) => {
179
221
// Check if variable exists
180
222
match env:: var ( & opt. key ) {
181
- Ok ( _) => unsafe { env:: remove_var ( & opt. key ) } ,
223
+ Ok ( _) => {
224
+ if opt. global {
225
+ if let Err ( err) = globalenv:: unset_var ( & opt. key ) {
226
+ error ( & format ! ( "can't globally delete variable: {}" , err) , cli. exit_on_error ) ;
227
+ }
228
+ } else {
229
+ unsafe { env:: remove_var ( & opt. key ) }
230
+ }
231
+ } ,
182
232
_ => warning ( "variable doesn't exists" ) ,
183
233
}
184
- run ( opt. process , cli. exit_on_error ) ;
234
+ if let Some ( process) = opt. process {
235
+ run ( process, cli. exit_on_error ) ;
236
+ }
185
237
}
186
238
}
187
239
}
0 commit comments