envio 0.4.0

Envio is a command-line tool that simplifies the management of environment variables across multiple profiles. It allows users to easily switch between different configurations and apply them to their current environment. Envio also encrypts sensitive environment variable values to ensure secure storage and transmission
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
//! `envio` is a library that simplifies the management of environment variables across multiple profiles
//!
//! It allows users to easily switch between different configurations and apply them to their current environment
//! `envio` also encrypts sensitive environment variable values to ensure secure storage and transmission of environment variables
//!
//! Here is a very simple example:
//! ```
//! // In this example we get the profile passed as an argument to the program
//! // and then print the environment variables in that profile
//!
//! use envio;
//!
//! let args: Vec<String> = std::env::args().collect();
//!
//! if args.len() != 2 {
//!    println!("Usage: <profile_name> <key>");
//!    return;
//! }
//!
//! let profile_name = args[1].to_string();
//! let key = &args[2]; // All profiles have a key that is used to encrypt the environment variables, this ensures that the environment variables are secure
//!
//!
//!  for (env_var, value) in &envio::get_profile(profile_name, key).unwrap().envs {
//!    println!("{}: {}", env_var, value);
//! }
//! ```
//!

pub mod crypto;
pub mod utils;

use std::{
    collections::HashMap,
    io::{Read, Write},
    path::{Path, PathBuf},
};

#[cfg(target_family = "windows")]
use std::process::Command;

use colored::Colorize;
use comfy_table::{Attribute, Cell, Table};

use crate::{
    crypto::{decrypt, encrypt},
    utils::{download_file, get_configdir, get_cwd},
};

/*
 * The Profile struct is used to store the environment variables of a profile
 * It also stores the name of the profile and the path to the profile file
*/
pub struct Profile {
    pub name: String,
    pub envs: HashMap<String, String>,
    pub profile_file_path: PathBuf,
    key: String,
}

impl Profile {
    /*
    * Create a new Profile object

    @param name String
    @param envs HashMap<String, String>
    @param profile_file_path PathBuf
    @param key String
    @return Profile
    */
    pub fn new(
        name: String,
        envs: HashMap<String, String>,
        profile_file_path: PathBuf,
        key: String,
    ) -> Self {
        Profile {
            name,
            envs,
            profile_file_path,
            key,
        }
    }

    /*
    * Add a new environment variable to the profile

    @param env String
    @param env_value String
    */
    pub fn add_env(&mut self, env: String, env_value: String) {
        self.envs.insert(env, env_value);
    }

    /*
    * Edit an existing environment variable of the profile
    * If the environment variable does not exists, it will print an error message

    @param env String
    @param new_value String
    */
    pub fn edit_env(&mut self, env: String, new_value: String) {
        if let std::collections::hash_map::Entry::Occupied(mut e) = self.envs.entry(env.clone()) {
            e.insert(new_value);
        } else {
            println!("{}: env `{}` does not exists", "Error".red(), env);
        }
    }

    /*
    * Remove an existing environment variable of the profile
    * If the environment variable does not exists, it will print an error message

    @param env String
    */
    pub fn remove_env(&mut self, env: String) {
        if self.envs.contains_key(&env) {
            self.envs.remove(&env);
        } else {
            println!("{}: env `{}` does not exists", "Error".red(), env);
        }
    }

    /*
     * List all the environment variables of the profile
     */
    pub fn list_envs(&self) {
        let mut table = Table::new();
        table.set_header(vec![
            Cell::new("Environment Variable").add_attribute(Attribute::Bold),
            Cell::new("Value").add_attribute(Attribute::Bold),
        ]);

        for key in self.envs.keys() {
            table.add_row(vec![key, self.envs.get(key).unwrap()]);
        }

        println!("{table}");
    }

    /*
    * Export the environment variables of the profile to a file

    * If the file does not exist, it will be created
    * If the file exists, it will be overwritten
    * If the profile does not have any environment variables, it will print an error message
    * The file will be created in the current working directory

    @param file_name String
    */
    pub fn export_envs(&self, file_name: String) {
        let mut file = std::fs::OpenOptions::new()
            .create(true)
            .write(true)
            .append(false)
            .open(get_cwd().join(file_name))
            .unwrap();

        let mut buffer = String::from("");

        if self.envs.is_empty() {
            println!("{}: No envs to export", "Error".red());
            return;
        }

        for key in self.envs.keys() {
            buffer = buffer + key + "=" + self.envs.get(key).unwrap() + "\n";
        }

        if let Err(e) = writeln!(file, "{}", buffer) {
            println!("{}: {}", "Error".red(), e);
        }

        println!("{}", "Exported envs".bold());
    }

    /*
     * Push the changes to the profile file
     */
    pub fn push_changes(&mut self) {
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .append(false)
            .open(&self.profile_file_path)
            .unwrap();

        if let Err(e) = file.set_len(0) {
            println!("{}: {}", "Error".red(), e);
        }

        let mut buffer = String::from("");

        for key in self.envs.keys() {
            buffer = buffer + key + "=" + self.envs.get(key).unwrap() + "\n";
        }

        let encrypted_data = encrypt(self.key.clone(), buffer);
        if let Err(e) = file.write_all(encrypted_data.as_slice()) {
            println!("{}: {}", "Error".red(), e);
        }

        if let Err(e) = file.flush() {
            println!("{}: {}", "Error".red(), e);
        }

        if let Err(e) = file.sync_all() {
            println!("{}: {}", "Error".red(), e);
        }
    }
}

/*
* Create a new profile
* If the profile already exists, it will print an error message

@param name String
@param envs Option<HashMap<String, String>>
@param user_key String
*/
pub fn create_profile(name: String, envs: Option<HashMap<String, String>>, user_key: &str) {
    if check_profile(name.clone()) {
        println!("{}: Profile already exists", "Error".red());
        return;
    }

    let envs = match envs {
        Some(env) => env,
        None => HashMap::new(),
    };

    let config_dir = get_configdir();
    let profile_dir = config_dir.join("profiles");

    if !profile_dir.exists() {
        println!(
            "{}",
            "Profiles directory does not exist creating it now..".bold()
        );
        std::fs::create_dir_all(&profile_dir).unwrap();
    }

    let profile_file = profile_dir.join(name + ".env");

    let mut file = if let Err(e) = std::fs::File::create(&profile_file) {
        println!("{}: {}", "Error".red(), e);
        return;
    } else {
        std::fs::File::create(&profile_file).unwrap()
    };

    if envs.is_empty() {
        if let Err(e) = file.write_all(encrypt(user_key.to_string(), "".to_string()).as_slice()) {
            println!("{}: {}", "Error".red(), e);
        }

        return;
    }

    let mut buffer = String::from("");

    for key in envs.keys() {
        buffer = buffer + key + "=" + envs.get(&key.to_string()).unwrap() + "\n";
    }

    if let Err(e) = file.write_all(encrypt(user_key.to_string(), buffer).as_slice()) {
        println!("{}: {}", "Error".red(), e);
    }

    println!("{}: Profile created", "Success".green());
}

/*
* Check if the profile exists

@param name String
@return bool
*/
pub fn check_profile(name: String) -> bool {
    let configdir = get_configdir();

    let profile_path = configdir.join("profiles").join(name + ".env");

    if profile_path.exists() {
        return true;
    }

    false
}

/*
* Delete a profile
* If the profile does not exist, it will print an error message

@param name String
*/
pub fn delete_profile(name: String) {
    if check_profile(name.clone()) {
        let configdir = get_configdir();
        let profile_path = configdir.join("profiles").join(name.clone() + ".env");

        match std::fs::remove_file(profile_path) {
            Ok(_) => println!("Deleted profile: {}", name),
            Err(e) => println!("{}: {}", "Error".red(), e),
        }
    } else {
        println!("{}: Profile does not exist", "Error".red());
    }
}

/*
 * List all the profiles
 */
pub fn list_profiles(raw: bool) {
    let configdir = get_configdir();
    let profile_dir = configdir.join("profiles");

    if !profile_dir.exists() {
        println!("{}: No profiles found", "Error".red());
        return;
    }

    let mut profiles = Vec::new();
    for entry in std::fs::read_dir(profile_dir).unwrap() {
        let entry = entry.unwrap();
        let path = entry.path();
        let file_name = path.file_name().unwrap().to_str().unwrap().to_owned();
        let profile_name = file_name.replace(".env", "");
        profiles.push(profile_name);
    }

    if raw {
        for profile in profiles {
            println!("{}", profile);
        }
        return;
    }

    let mut table = Table::new();
    table.set_header(vec![Cell::new("Profiles").add_attribute(Attribute::Bold)]);

    for profile in profiles {
        table.add_row(vec![profile]);
    }

    println!("{table}");
}

/*
* Download the profile from the url and save it to the config directory with the profile name
* passed

@param url String
@param profile_name String
*/
pub fn download_profile(url: String, profile_name: String) {
    println!("Downloading profile from {}", url);

    let location = if get_configdir()
        .join("profiles")
        .join(profile_name.clone() + ".env")
        .to_str()
        .is_none()
    {
        println!("{}: Could not get convert path to string", "Error".red());
        return;
    } else {
        get_configdir()
            .join("profiles")
            .join(profile_name.clone() + ".env")
            .to_str()
            .unwrap()
            .to_owned()
    };

    tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap_or_else(|e| {
            println!("{}: {}", "Error".red(), e);
            std::process::exit(1);
        })
        .block_on(download_file(url.as_str(), location.as_str()));

    println!("Downloaded profile: {}", profile_name);
}

/*
* Import a profile from a file

@param file_path String
@param profile_name String
*/
pub fn import_profile(file_path: String, profile_name: String) {
    if !Path::new(&file_path).exists() {
        println!("{}: File does not exist", "Error".red());
        return;
    }

    let mut file = std::fs::OpenOptions::new()
        .read(true)
        .open(&file_path)
        .unwrap();

    let mut contents = String::new();
    file.read_to_string(&mut contents).unwrap();

    let location = if get_configdir()
        .join("profiles")
        .join(profile_name.clone() + ".env")
        .to_str()
        .is_none()
    {
        println!("{}: Could not get convert path to string", "Error".red());
        return;
    } else {
        get_configdir()
            .join("profiles")
            .join(profile_name + ".env")
            .to_str()
            .unwrap()
            .to_owned()
    };

    let mut file = std::fs::OpenOptions::new()
        .write(true)
        .create(true)
        .open(location)
        .unwrap();

    if let Err(e) = file.write(contents.as_bytes()) {
        println!("{}: {}", "Error".red(), e);
    }
}

/*
* Returns a profile object from the profile name passed, it checks if the profile exists
* All profiles are stored in a directory called `profiles` in the config directory
*
* If the profile does not exist, it will return None
*
* If it exists, it will read the contents of the file and decrypt it using the key passed
* It will then return a profile object containing the name of the profile and a hashmap of the environment variables

@param profile_name String
@param key String

@return Option<Profile>
*/
pub fn get_profile(profile_name: String, key: &str) -> Option<Profile> {
    if !check_profile(profile_name.clone()) {
        println!("{}: Profile does not exist", "Error".red());
        return None;
    }

    let profile_file_path = home::home_dir()
        .unwrap()
        .join(".envio")
        .join("profiles")
        .join(format!("{}.env", profile_name));

    let mut file = std::fs::OpenOptions::new()
        .read(true)
        .open(&profile_file_path)
        .unwrap();

    let mut encrypted_contents = Vec::new();
    file.read_to_end(&mut encrypted_contents).unwrap();

    let content = decrypt(key.to_string(), &encrypted_contents);

    let mut envs = HashMap::new();

    for line in content.lines() {
        if line.is_empty() {
            continue;
        }

        let mut split = line.split('=');
        let key = split.next().unwrap();

        let value = split.next().unwrap();

        envs.insert(key.to_string(), value.to_string());
    }

    Some(Profile::new(
        profile_name,
        envs,
        profile_file_path,
        key.to_string(),
    ))
}

// Unix specific code
// Creates a shell script that can be sourced to set the environment variables
#[cfg(any(target_family = "unix"))]
pub fn create_shellscript(profile: &str) {
    let configdir = get_configdir();
    let shellscript_path = configdir.join("setenv.sh");

    let mut file = if let Ok(e) = std::fs::OpenOptions::new()
        .write(true)
        .append(false)
        .open(shellscript_path)
    {
        e
    } else {
        println!("{}: Could not open file", "Error".red());
        return;
    };

    let shellscript = format!(
        r#"#!/bin/bash
# This script was generated by envio and should not be modified!


TMP_FILE=$(mktemp)

set +e
ENV_VARS=$(envio list {} -- --no-pretty-print)
EXIT_CODE=$?
SHELL_NAME=$(basename "$SHELL")


if [ ! -f "$TMP_FILE" ]; then
    echo "Failed to create temp file"
    exit 1
fi

if [ $EXIT_CODE -ne 0 ]; then
    echo "Failed to load environment variables"
    rm "$TMP_FILE"
else
    case "$SHELL_NAME" in
        bash | zsh)
            echo "$ENV_VARS" | while IFS= read -r line; do
                echo "export $line" >> "$TMP_FILE"
            done
            ;;
        fish)
            echo "$ENV_VARS" | while IFS= read -r line; do
                echo "set -gX $line" >> "$TMP_FILE"
            done
            ;;
        *)
            echo "Unsupported shell: $SHELL_NAME"
            exit 1
            ;;
    esac

    if [ ! -s "$TMP_FILE" ]; then
        echo "Temp file is empty"
        exit 1
    fi

    source "$TMP_FILE"
    source "$TMP_FILE" &> /dev/null

    if [ $? -ne 0 ]; then
        echo "Failed to load environment variables from temp file"
        exit 1
    fi

    rm "$TMP_FILE"
fi
"#,
        profile
    );

    if let Err(e) = file.write_all(shellscript.as_bytes()) {
        println!("{}: {}", "Error".red(), e);
    }

    if let Err(e) = file.flush() {
        println!("{}: {}", "Error".red(), e);
    }

    if let Err(e) = file.sync_all() {
        println!("{}: {}", "Error".red(), e);
    }
}

// Unix specific code
// Returns the shell that is being used
// @return String
#[cfg(any(target_family = "unix"))]
pub fn get_shell_config() -> String {
    // Gets your default shell
    // This is used to determine which shell config file to edit
    let shell_env_value = if let Ok(e) = std::env::var("SHELL") {
        e
    } else {
        println!("{}: Could not get shell", "Error".red());
        std::process::exit(1);
    };

    let shell_as_vec = shell_env_value.split('/').collect::<Vec<&str>>();
    let shell = shell_as_vec[shell_as_vec.len() - 1];

    let mut shell_config = "";
    if shell.contains("bash") {
        shell_config = ".bashrc";
    } else if shell.contains("zsh") {
        shell_config = ".zshrc";
    } else if shell.contains("fish") {
        shell_config = ".config/fish/config.fish"
    }

    shell_config.to_string()
}

/*
 * Load the environment variables of the profile into the current session
 */
#[cfg(any(target_family = "unix"))]
pub fn load_profile(profile_name: &str) {
    if !check_profile(profile_name.to_string()) {
        println!("{}: Profile does not exist", "Error".red());
        return;
    }

    let shell_config = get_shell_config();

    create_shellscript(profile_name);

    if !shell_config.is_empty() {
        println!(
            "Reload your shell to apply changes or run `source {}`",
            format_args!("~/{}", shell_config)
        );
    } else {
        println!("Reload your shell to apply changes");
    }
}

/*
 * Windows specific code
*/

#[cfg(target_family = "windows")]
pub fn load_profile(profile: Profile) {
    for (env, value) in &profile.envs {
        Command::new("setx")
            .arg(env)
            .arg(value)
            .spawn()
            .expect("setx command failed");
    }

    println!("Reload your shell to apply changes");
}

/*
 * Unload the environment variables of the profile from the current session
 */
#[cfg(any(target_family = "unix"))]
pub fn unload_profile() {
    let file = std::fs::OpenOptions::new()
        .write(true)
        .append(false)
        .open(get_configdir().join("setenv.sh"))
        .unwrap();

    if let Err(e) = file.set_len(0) {
        println!("{}: {}", "Error".red(), e);
    }

    println!("Reload your shell to apply changes");
}

/*
 * Windows specific code
*/
#[cfg(target_family = "windows")]
pub fn unload_profile(profile: Profile) {
    for env in profile.envs.keys() {
        Command::new("REG")
            .arg("delete")
            .arg("HKCU\\Environment")
            .arg("/F")
            .arg("/V")
            .arg(format!("\"{}\"", env))
            .arg("")
            .spawn()
            .expect("setx command failed");
    }
    println!("Reload your shell to apply changes");
}