envio 0.6.0

A Modern And Secure CLI Tool For Managing Environment Variables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
use std::collections::HashMap;
use std::{io::Write, path::PathBuf};

use chrono::NaiveDate;
use serde::{Deserialize, Serialize};

use crate::utils::{get_configdir, truncate_identity_bytes};

use crate::crypto::EncryptionType;
use crate::error::{Error, Result};

/// Representation of an environment variable
#[derive(Serialize, Deserialize, Clone)]
pub struct Env {
    pub name: String,
    pub value: String,
    pub comment: Option<String>,
    pub expiration_date: Option<NaiveDate>,
}

impl Env {
    pub fn new(
        name: String,
        value: String,
        comment: Option<String>,
        expiration_date: Option<NaiveDate>,
    ) -> Env {
        Env {
            name,
            value,
            comment,
            expiration_date,
        }
    }

    pub fn from_key_value(key: String, value: String) -> Env {
        Env {
            name: key,
            value,
            comment: None,
            expiration_date: None,
        }
    }
}

/// Wrapper around a vector of `Env`
#[derive(Serialize, Deserialize, Clone)]
pub struct EnvVec {
    envs: Vec<Env>,
}

/// Build a `EnvVec` from a `Vec<Env>` or a `HashMap<String, String>`
impl From<Vec<Env>> for EnvVec {
    fn from(envs: Vec<Env>) -> Self {
        EnvVec { envs }
    }
}

impl From<HashMap<String, String>> for EnvVec {
    fn from(envs: HashMap<String, String>) -> Self {
        let mut env_vec = EnvVec::new();

        for (key, value) in envs {
            env_vec.push(Env::from_key_value(key, value));
        }

        env_vec
    }
}

/// Convert a `EnvVec` to a `Vec<Env>` or a `HashMap<String, String>`
impl From<EnvVec> for Vec<Env> {
    fn from(val: EnvVec) -> Self {
        val.envs
    }
}

impl From<EnvVec> for HashMap<String, String> {
    fn from(val: EnvVec) -> Self {
        let mut envs = HashMap::new();

        for e in val.envs {
            envs.insert(e.name, e.value);
        }

        envs
    }
}

impl Default for EnvVec {
    fn default() -> Self {
        Self::new()
    }
}

impl EnvVec {
    pub fn new() -> EnvVec {
        EnvVec { envs: Vec::new() }
    }

    /// Add a new environment variable to the `EnvVec`
    ///
    /// # Parameters
    /// - `env` - The environment variable to add. Has to be an instance of the
    ///   [Env](crate::Env) struct
    ///
    /// # Examples
    /// ```
    /// use envio::EnvVec;
    ///
    /// let mut envs = EnvVec::new();
    ///
    /// envs.push(envio::Env::new("NEW_ENV".to_string(), "NEW_VALUE".to_string()));
    ///
    /// ```
    pub fn push(&mut self, env: Env) {
        self.envs.push(env);
    }

    /// Remove an environment variable from the `EnvVec`
    ///
    /// # Parameters
    /// - `env` - The name of the environment variable to remove. Has to be an
    ///   instance of the [Env](crate::Env) struct
    ///
    /// # Examples
    ///
    /// ```
    /// use envio::EnvVec;
    ///
    /// let mut envs = EnvVec::new();
    ///
    /// envs.push(envio::Env::new("NEW_ENV".to_string(), "NEW_VALUE".to_string()));
    ///
    /// envs.remove("NEW_ENV");
    ///
    /// ```
    pub fn remove(&mut self, env: &str) {
        self.envs.retain(|e| e.name != env);
    }

    /// Return an iterator over the `EnvVec`
    pub fn iter(&self) -> std::slice::Iter<Env> {
        self.envs.iter()
    }

    /// Return a mutable iterator over the `EnvVec`
    pub fn iter_mut(&mut self) -> std::slice::IterMut<Env> {
        self.envs.iter_mut()
    }

    /// Return a vector of all the keys in the `EnvVec`
    ///
    /// # Returns
    /// - `Vec<String>`: A vector of all the keys in the `EnvVec`
    ///
    /// # Examples
    ///
    /// ```
    /// use envio::EnvVec;
    ///
    /// let mut envs = EnvVec::new();
    ///
    /// envs.push(envio::Env::new("NEW_ENV".to_string(), "NEW_VALUE".to_string()));
    ///
    /// let keys = envs.keys();
    ///
    /// for key in keys {
    ///    println!("{}", key);
    /// }
    /// ```
    pub fn keys(&self) -> Vec<String> {
        self.envs.iter().map(|e| e.name.clone()).collect()
    }

    /// Check to see if an environment variable with the given key exists in the
    /// `EnvVec`
    ///
    /// # Parameters
    /// - `key` - The key to check for
    ///
    /// # Returns
    /// - `bool`: indicating whether the key exists or not
    ///
    /// # Examples
    ///
    /// ```
    /// use envio::EnvVec;
    ///
    /// let mut envs = EnvVec::new();
    ///
    /// envs.push(envio::Env::new("NEW_ENV".to_string(), "NEW_VALUE".to_string()));
    ///
    /// let exists = envs.contains_key("NEW_ENV");
    ///
    /// if exists {
    ///   println!("The key exists");
    ///
    /// } else {
    ///  println!("The key does not exist");
    /// }
    /// ```
    pub fn contains_key(&self, key: &str) -> bool {
        self.envs.iter().any(|e| e.name == key)
    }

    /// Get the value of an environment variable with the given key
    ///
    /// # Parameters
    /// - `key` - The key of the environment variable
    ///
    /// # Returns
    /// - `Option<&String>`: The value of the environment variable if it exists
    ///
    /// # Examples
    ///
    /// ```
    /// use envio::EnvVec;
    ///
    /// let mut envs = EnvVec::new();
    ///
    /// envs.push(envio::Env::new("NEW_ENV".to_string(), "NEW_VALUE".to_string()));
    ///
    /// let value = envs.get("NEW_ENV");
    ///
    /// if let Some(v) = value {
    ///     println!("The value of the environment variable is: {}", v);
    /// } else {
    ///     println!("The environment variable does not exist");
    /// }
    /// ```
    pub fn get(&self, key: &str) -> Option<&String> {
        for e in self.envs.iter() {
            if e.name == key {
                return Some(&e.value);
            }
        }

        None
    }

    /// Check to see if the `EnvVec` is empty
    ///
    /// # Returns
    /// - `bool`: indicating whether the `EnvVec` is empty or not
    pub fn is_empty(&self) -> bool {
        self.envs.is_empty()
    }

    /// Get the number of environment variables in the `EnvVec`
    ///
    /// # Returns
    /// - `usize`: The number of environment variables in the `EnvVec`
    pub fn len(&self) -> usize {
        self.envs.len()
    }

    /// Retain only the environment variables that satisfy the given predicate
    ///
    /// # Parameters
    /// - `f` - The predicate to use
    ///
    /// # Examples
    ///
    /// ```
    /// use envio::EnvVec;
    ///
    /// let mut envs = EnvVec::new();
    ///
    /// envs.push(envio::Env::new("NEW_ENV".to_string(), "NEW_VALUE".to_string()));
    /// envs.push(envio::Env::new("NEW_ENV_2".to_string(), "NEW_VALUE_2".to_string()));
    ///
    /// envs.retain(|e| e.name == "NEW_ENV"); // Only keep the environment variable with the key "NEW_ENV"
    /// ```
    pub fn retain<F>(&mut self, f: F)
    where
        F: FnMut(&Env) -> bool,
    {
        self.envs.retain(f);
    }
}

/// Allow users to iterate over the `EnvVec` struct
impl IntoIterator for EnvVec {
    type Item = Env;
    type IntoIter = std::vec::IntoIter<Env>;

    fn into_iter(self) -> Self::IntoIter {
        self.envs.into_iter()
    }
}

impl<'a> IntoIterator for &'a EnvVec {
    type Item = &'a Env;
    type IntoIter = std::slice::Iter<'a, Env>;

    fn into_iter(self) -> Self::IntoIter {
        self.envs.iter()
    }
}

impl<'a> IntoIterator for &'a mut EnvVec {
    type Item = &'a mut Env;
    type IntoIter = std::slice::IterMut<'a, Env>;

    fn into_iter(self) -> Self::IntoIter {
        self.envs.iter_mut()
    }
}

/// This struct is a representation of a profile env file stored on the system.
/// For more information on what a profile is, see the
/// [profile](https://envio-cli.github.io/profiles) page on the official
/// website.
///
/// The way that envio loads a profile is it first reads the encrypted contents
/// of the profile file and then passes that to the
/// [from_content](Profile::from_content) method of the `Profile` struct along
/// with the name of the profile and the encryption type used to encrypt the
/// profile.
///
/// The encryption type can be deduced using the
/// [get_encryption_type](crate::crypto::get_encryption_type) function from the
/// `crypto` module. It is recommended to first take a look at the documentation
/// for the [crypto](crate::crypto) module before proceeding. Since depending on
/// the encryption type used, the user might have to provide a key to decrypt
/// the profile.
///
/// This method is pretty *technical* and is not meant to be used by the end
/// user. It is recommended to use the [load_profile](crate::load_profile) macro to
/// load a profile. It is a lot more user friendly and handles all the technical
/// details for you.
///
/// After successfully loading a profile users can then use the methods of the
/// `Profile` struct to interact with the environment variables stored in the
/// profile.
///
/// After the user has made the changes to the profile, they can then call the
/// [push_changes](Profile::push_changes) method to push the changes to the
/// profile file.
///
/// # Examples
///
/// ```
///   // Error handling is omitted for brevity
///   use envio::Profile;
///    
///   // Read the contents of the profile file
///   // A utility function is provided to do this or you can load the contents yourself
///   let encrypted_content = envio::utils::get_profile_content("my-profile").unwrap();
///
///   // Load the profile assuming the encryption type is `age`
///   let mut profile = Profile::from_content("my-profile", &encrypted_content, envio::crypto::get_encryption_type(&encrypted_content).unwrap()).unwrap();
///   
///   // Or use the load_profile macro
///   let mut profile = envio::load_profile!("my-profile").unwrap();
///
///   for (key, value) in profile.envs.iter() {
///    println!("{}={}", key, value);
///   }
///
///   // Add a new environment variable to the profile
///   profile.insert_env("NEW_ENV".to_string(), "new_value".to_string());
///   
///   // Push the changes to the profile file
///   profile.push_changes().unwrap();
/// ```
#[derive(Serialize, Deserialize)]
pub struct Profile {
    pub name: String,
    pub envs: EnvVec,
    pub profile_file_path: PathBuf,
    encryption_type: Box<dyn EncryptionType>,
}

impl Profile {
    pub fn new(
        name: String,
        envs: EnvVec,
        profile_file_path: PathBuf,
        encryption_type: Box<dyn EncryptionType>,
    ) -> Profile {
        Profile {
            name,
            envs,
            profile_file_path,
            encryption_type,
        }
    }
    /// Create a new profile object from the encrypted contents of a stored
    /// profile file
    ///  
    ///
    /// This method is not meant to be used by the end user. It is recommended
    /// to use the [load_profile](crate::load_profile) macro to load a profile.
    ///
    /// # Parameters
    /// - `name` - The name of the profile
    /// - `encrypted_content` - The encrypted contents of the profile file
    /// - `encryption_type` - The encryption type used to encrypt the profile
    ///
    /// `name` can either be the name of the profile or the absolute path to the
    /// profile file.
    ///
    /// # Returns
    /// - `Result<Profile>`: the profile object if the operation was successful or an error if it was not
    ///
    /// # Examples
    /// ```
    /// use envio::Profile;
    ///
    /// let encrypted_content = envio::utils::get_profile_content("my-profile").unwrap();
    ///
    /// let mut profile = match Profile::from_content("my-profile", &encrypted_content, envio::crypto::get_encryption_type(&encrypted_content).unwrap()) {
    ///     Ok(p) => p,
    ///     Err(e) => {
    ///         eprintln!("An error occurred: {}", e);
    ///         return;
    ///     }
    ///  };
    /// ```
    pub fn from_content(
        encrypted_content: &[u8],
        encryption_type: Box<dyn EncryptionType>,
    ) -> Result<Profile> {
        let truncated_content = truncate_identity_bytes(encrypted_content);

        let content = match encryption_type.decrypt(&truncated_content) {
            Ok(c) => c,
            Err(e) => {
                return Err(e);
            }
        };

        match bincode::deserialize(&content) {
            Ok(profile) => Ok(profile),
            Err(e) => Err(Error::Deserialization(e.to_string())),
        }
    }

    /// Check to see if a profile with the given name exists on the system
    ///
    /// # Parameters
    /// - `name` - The name of the profile
    ///
    /// # Returns
    /// - `bool`: indicating whether the profile exists or not
    ///
    /// # Examples
    /// ```
    /// use envio::Profile;
    ///
    /// let exists = Profile::does_exist("my-profile");
    ///
    /// if exists {
    ///    println!("The profile exists");
    /// } else {
    ///   println!("The profile does not exist");
    /// }
    pub fn does_exist(name: &str) -> bool {
        let configdir = get_configdir();

        let profile_path = configdir.join("profiles").join(format!("{}.env", name));

        if profile_path.exists() {
            return true;
        }

        false
    }

    /// Add a new environment variable to the profile
    ///
    /// # Parameters
    /// - `env` - The name of the environment variable
    /// - `env_value` - The value of the environment variable
    ///
    /// # Examples
    /// ```
    /// use envio::load_profile;
    ///
    /// let mut profile = load_profile!("my-profile").unwrap();
    ///
    /// profile.insert_env("NEW_ENV".to_string(), "new_value".to_string());
    ///
    /// profile.push_changes().unwrap();
    ///
    /// ```
    pub fn insert_env(&mut self, env: String, env_value: String) {
        self.envs.push(Env::from_key_value(env, env_value));
    }

    /// Edit an existing environment variable of the profile
    ///
    /// # Parameters
    /// - `env` - The name of the environment variable
    /// - `new_value` - The new value of the environment variable
    ///
    /// # Returns
    /// - `Result<()>`: indicating whether the operation was successful or not
    ///
    /// # Examples
    /// ```
    /// use envio::load_profile;
    ///
    /// let mut profile = load_profile!("my-profile").unwrap();
    ///
    /// profile.edit_env("NEW_ENV".to_string(), "new_value".to_string());
    ///
    /// profile.push_changes().unwrap();
    ///
    /// ```
    pub fn edit_env(&mut self, env: String, new_value: String) -> Result<()> {
        if self.envs.iter().any(|e| e.name == env) {
            for e in self.envs.iter_mut() {
                if e.name == env {
                    e.value = new_value;
                    return Ok(());
                }
            }
        }

        Err(Error::EnvDoesNotExist(env))
    }

    /// Remove an environment variable from the profile
    ///
    /// # Parameters
    /// - `env` - The name of the environment variable
    ///
    /// # Returns
    /// - `Result<()>`: indicating whether the operation was successful or not
    ///
    /// # Examples
    ///
    /// ```
    /// use envio::load_profile;
    ///  
    /// let mut profile = load_profile!("my-profile").unwrap();
    ///
    /// profile.remove_env("NEW_ENV");
    ///
    /// profile.push_changes().unwrap();
    ///
    /// ```
    pub fn remove_env(&mut self, env: &str) -> Result<()> {
        if self.envs.iter().any(|e| e.name == env) {
            self.envs.retain(|e| e.name != env);
            return Ok(());
        }

        Err(Error::EnvDoesNotExist(env.to_string()))
    }

    /// Get the value of an environment variable from the profile
    ///
    /// # Parameters
    /// - `env` - The name of the environment variable
    ///
    /// # Returns
    /// - `Option<&String>`: The value of the environment variable if it exists
    ///
    /// # Examples
    ///
    /// ```
    /// use envio::load_profile;
    ///
    /// let profile = load_profile!("my-profile").unwrap();
    ///
    /// let value = profile.get_env("NEW_ENV");
    ///
    /// if let Some(v) = value {
    ///    println!("The value of the environment variable is: {}", v);
    /// } else {
    ///   println!("The environment variable does not exist");
    /// }
    /// ```
    pub fn get_env(&self, env: &str) -> Option<&String> {
        for e in self.envs.iter() {
            if e.name == env {
                return Some(&e.value);
            }
        }

        None
    }

    pub fn get_envs_hashmap(&self) -> std::collections::HashMap<String, String> {
        let mut envs = std::collections::HashMap::new();

        for e in self.envs.iter() {
            envs.insert(e.name.clone(), e.value.clone());
        }

        envs
    }

    /// Push the changes made to the profile object to the profile file
    ///
    /// # Returns
    /// - `Result<()>`: indicating whether the operation was successful or not
    ///
    /// # Examples
    ///
    /// ```
    /// use envio::load_profile;
    ///
    /// let mut profile = load_profile!("my-profile").unwrap();
    ///
    /// profile.insert_env("NEW_ENV".to_string(), "new_value".to_string());
    ///
    /// profile.push_changes().unwrap();
    ///
    /// ```
    pub fn push_changes(&mut self) -> Result<()> {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(false)
            .create(true)
            .open(&self.profile_file_path)?;

        file.set_len(0)?;

        let serialized_data = match bincode::serialize(&self) {
            Ok(data) => data,
            Err(e) => {
                return Err(Error::Serialization(e.to_string()));
            }
        };

        let encrypted_data = match self.encryption_type.encrypt(&serialized_data) {
            Ok(data) => data,
            Err(e) => {
                return Err(e);
            }
        };

        file.write_all(&encrypted_data)?;

        file.flush()?;

        file.sync_all()?;

        Ok(())
    }
}

/// A macro which simplifies the process of loading a profile from the system
///
/// # Parameters
/// - `name` - The name of the profile
/// - `get_key` - A closure which returns the key used to decrypt the profile.
///   It is only required if the profile is encrypted using the `age` encryption
///   type. You can omit this parameter if the profile is encrypted using the
///  `gpg` encryption type. To figure out which encryption type is used, you can
/// use the [get_encryption_type](crate::crypto::get_encryption_type) function
/// from the `crypto` module.
///
/// `name` can either be the name of the profile or the absolute path to the
/// profile file.
///
/// <div class="warning">Please note that it is not recommended to hardcode the key in the closure. It is recommended to use a password manager to store the key and then retrieve it here or prompt the user to enter the key.</div>
///
/// # Returns
/// - `Result<Profile>`: the profile object if the operation was successful or
///   an error if it was not
///
/// # Examples
///
/// If the profile is encrypted using the `gpg` encryption type:
/// ```
/// use envio::load_profile;
///
/// let profile = match load_profile!("my-profile") {
///    Ok(p) => p,
///    Err(e) => {
///     eprintln!("An error occurred: {}", e);
///     return;
///    }
/// };
///
/// for (key, value) in profile.envs.iter() {
///   println!("{}={}", key, value);
/// }
///
/// ```
///
/// If the profile is encrypted using the `age` encryption type:
/// ```
/// use envio::load_profile;
///
/// let profile = match load_profile!("my-profile", || {
///    // This closure should return the key used to decrypt the profile
///    // It is recommended to use a password manager to store the key and then retrieve it here
///    // Or prompt the user to enter the key
///    "my-key".to_string()
/// }) {
///    Ok(p) => p,
///    Err(e) => {
///        eprintln!("An error occurred: {}", e);
///        return;
///    }
/// };
///
/// for (key, value) in profile.envs.iter() {
///   println!("{}={}", key, value);
/// }
///
/// ```
#[macro_export]
macro_rules! load_profile {
    ($name:expr $(, $get_key:expr)?) => {
        (||->envio::error::Result<envio::Profile> {
            use envio::Profile;
            use envio::crypto;
            use envio::utils;

            let encrypted_content = utils::get_profile_content($name)?;
            let mut encryption_type;

            match crypto::get_encryption_type(&encrypted_content) {
                Ok(t) => encryption_type = t,
                Err(e) => return Err(e.into()),
            }

            if encryption_type.as_string() == "age" {
                $(
                    let key = $get_key();
                    encryption_type.set_key(key);
                )?
            }

            match Profile::from_content(&encrypted_content, encryption_type) {
                Ok(profile) => return Ok(profile),
                Err(e) => return Err(e.into()),
            }
         })()
    };
}