Skip to content

Commit

Permalink
Merge pull request #42 from afh/export-envs
Browse files Browse the repository at this point in the history
Add --envs option to export
  • Loading branch information
humblepenguinn authored Feb 26, 2024
2 parents 9126e60 + fcba8b0 commit 95878ee
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "envio"
version = "0.5.1"
rust-version = "1.76.0"
rust-version = "1.75.0"
description = "A Modern And Secure CLI Tool For Managing Environment Variables"
edition = "2021"
authors = ["Humble Penguin <humblepenguinn@gmail.com>"]
Expand Down
2 changes: 2 additions & 0 deletions completions/_envio
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ _arguments "${_arguments_options[@]}" \
_arguments "${_arguments_options[@]}" \
'-f+[]:FILE: ' \
'--file-to-export-to=[]:FILE: ' \
'*-e+[]:ENVS: ' \
'*--envs=[]:ENVS: ' \
'-h[Print help]' \
'--help[Print help]' \
':profile_name:' \
Expand Down
2 changes: 2 additions & 0 deletions completions/_envio.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Register-ArgumentCompleter -Native -CommandName 'envio' -ScriptBlock {
'envio;export' {
[CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'f')
[CompletionResult]::new('--file-to-export-to', 'file-to-export-to', [CompletionResultType]::ParameterName, 'file-to-export-to')
[CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'e')
[CompletionResult]::new('--envs', 'envs', [CompletionResultType]::ParameterName, 'envs')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
break
Expand Down
10 changes: 9 additions & 1 deletion completions/envio.bash
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ _envio() {
return 0
;;
envio__export)
opts="-f -h --file-to-export-to --help <PROFILE_NAME>"
opts="-f -e -h --file-to-export-to --envs --help <PROFILE_NAME>"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand All @@ -179,6 +179,14 @@ _envio() {
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--envs)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-e)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
Expand Down
1 change: 1 addition & 0 deletions completions/envio.fish
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ complete -c envio -n "__fish_seen_subcommand_from list" -s h -l help -d 'Print h
complete -c envio -n "__fish_seen_subcommand_from update" -s e -l envs -r
complete -c envio -n "__fish_seen_subcommand_from update" -s h -l help -d 'Print help'
complete -c envio -n "__fish_seen_subcommand_from export" -s f -l file-to-export-to -r
complete -c envio -n "__fish_seen_subcommand_from export" -s e -l envs -r
complete -c envio -n "__fish_seen_subcommand_from export" -s h -l help -d 'Print help'
complete -c envio -n "__fish_seen_subcommand_from import" -s f -l file-to-import-from -r
complete -c envio -n "__fish_seen_subcommand_from import" -s u -l url -r
Expand Down
8 changes: 8 additions & 0 deletions src/bin/envio/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ pub enum Command {
profile_name: String,
#[arg(required = false, long = "file-to-export-to", short = 'f')]
file: Option<String>,
#[arg(
required = false,
long = "envs",
short = 'e',
value_delimiter = ' ',
num_args = 1..,
)]
envs: Option<Vec<String>>,
},
#[command(
name = "import",
Expand Down
36 changes: 33 additions & 3 deletions src/bin/envio/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Command {

let prompt = MultiSelect::new("Select the environment variables you want to keep in your new profile:", options.clone())
.with_default(&default_options)
.with_help_message("↑↓ to move, space to select one, → to all, ← to none, type to filter, enter to confirm")
.with_help_message("↑↓ to move, space to select/unselect one, → to all, ← to none, type to filter, enter to confirm")
.prompt();

if let Err(e) = prompt {
Expand Down Expand Up @@ -591,7 +591,11 @@ impl Command {
profile.push_changes();
}

Command::Export { profile_name, file } => {
Command::Export {
profile_name,
file,
envs,
} => {
if !check_profile(profile_name) {
println!("{}: Profile does not exist", "Error".red());
return;
Expand All @@ -615,7 +619,33 @@ impl Command {
return;
};

profile.export_envs(file_name);
if envs.is_none() {
let prompt = MultiSelect::new("Select the environment variables you want to export:", profile.envs.keys().collect())
.with_default(&(0..profile.envs.len()).collect::<Vec<usize>>())
.with_help_message("↑↓ to move, space to select/unselect one, → to all, ← to none, type to filter, enter to confirm")
.prompt();

if let Err(e) = prompt {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}

profile.export_envs(
file_name,
&Some(
prompt
.unwrap()
.iter()
.cloned()
.map(|s| s.to_owned())
.collect(),
),
);

return;
}

profile.export_envs(file_name, envs);
}

Command::Import {
Expand Down
24 changes: 21 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Profile {
@param file_name &str
*/
pub fn export_envs(&self, file_name: &str) {
pub fn export_envs(&self, file_name: &str, envs: &Option<Vec<String>>) {
let mut file = std::fs::OpenOptions::new()
.create(true)
.write(true)
Expand All @@ -170,8 +170,26 @@ impl Profile {
return;
}

for key in self.envs.keys() {
buffer = buffer + key + "=" + self.envs.get(key).unwrap() + "\n";
let mut keys: Vec<_> = self.envs.keys().cloned().collect::<Vec<String>>();
if let Some(envs) = envs {
if !envs.is_empty() {
keys = self
.envs
.keys()
.into_iter()
.filter(|item| envs.contains(item))
.cloned()
.collect::<Vec<String>>();
}

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

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

if let Err(e) = writeln!(file, "{}", buffer) {
Expand Down

0 comments on commit 95878ee

Please sign in to comment.