Skip to content

Commit ef820e2

Browse files
alexng353claude
andcommitted
Remove .envx file fallback on variable get failure
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 422309a commit ef820e2

2 files changed

Lines changed: 3 additions & 96 deletions

File tree

src/utils/kvpair.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use anyhow::{Context, Result};
2-
use pgp::composed::{Message, SignedSecretKey};
32
use serde::{Deserialize, Serialize};
43
use std::fmt;
54
use std::str::FromStr;
65

7-
use super::key::UnlockedKey;
8-
96
#[derive(Debug, Deserialize, Serialize, Clone)]
107
pub struct KVPair {
118
pub key: String,
@@ -26,30 +23,6 @@ impl KVPair {
2623
}
2724
}
2825

29-
pub fn read_kvpairs_from_file(
30-
file_name: &str,
31-
key: &UnlockedKey,
32-
) -> Result<Vec<KVPair>> {
33-
let msg = Message::from_file(&file_name)?;
34-
35-
let ssk = SignedSecretKey::try_from(key)?;
36-
let mut decrypted = msg
37-
.decrypt(&key.password.clone().into(), &ssk)
38-
.context("Decrypting the message")?;
39-
40-
if decrypted.is_compressed() {
41-
decrypted = decrypted
42-
.decompress()
43-
.context("Failed to decompress message")?;
44-
}
45-
46-
decrypted
47-
.as_data_string()?
48-
.split("\n")
49-
.map(|s| KVPair::from_str(s))
50-
.collect::<Result<Vec<KVPair>>>()
51-
}
52-
5326
impl fmt::Display for KVPair {
5427
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5528
write!(f, "{}={}", self.key, self.value)

src/utils/magic_variables.rs

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,17 @@
1-
use std::io::Write;
1+
use crate::{sdk::SDK, utils::variable::ToKVPair};
22

3-
use anyhow::{bail, Context};
4-
use home::home_dir;
5-
use pgp::composed::SignedPublicKey;
3+
use super::{key::UnlockedKey, kvpair::KVPair};
64

7-
use crate::{
8-
sdk::SDK,
9-
utils::{
10-
kvpair::read_kvpairs_from_file, rpgp::encrypt, variable::ToKVPair,
11-
},
12-
};
13-
14-
use super::{
15-
key::{Key, UnlockedKey},
16-
kvpair::KVPair,
17-
};
18-
19-
/// Magically get variables from the API or default to ~/.envx/<fingerprint>.envx file
205
pub async fn get_variables_magic(
216
project_id: &str,
227
key: &UnlockedKey,
238
all: bool,
249
) -> anyhow::Result<Vec<KVPair>> {
25-
let kvpairs = if all {
10+
if all {
2611
SDK::get_variables(&project_id, &key)
2712
.await
2813
.map(|v| v.to_kvpair())
2914
} else {
3015
SDK::get_variables_pruned(&project_id, &key).await
31-
};
32-
33-
match kvpairs {
34-
Ok(variables) => {
35-
write_variables_magic(project_id, &key.key, &variables).await?;
36-
Ok(variables)
37-
}
38-
Err(e) => {
39-
let home_dir =
40-
home_dir().context("Failed to get home directory")?;
41-
let config_dir = home_dir.join(".config/envx");
42-
let envx_file = config_dir.join(format!("{}.envx", &project_id));
43-
let envx_file = envx_file
44-
.to_str()
45-
.ok_or(anyhow::anyhow!("Failed to convert path to string"))?;
46-
47-
match read_kvpairs_from_file(envx_file, &key) {
48-
Ok(kvpairs) => Ok(kvpairs),
49-
Err(err) => {
50-
eprintln!(
51-
"Error occurred while getting variables: {}",
52-
err
53-
);
54-
bail!("Failed to read .envx file: {}", e);
55-
}
56-
}
57-
}
5816
}
5917
}
60-
61-
async fn write_variables_magic(
62-
project_id: &str,
63-
key: &Key,
64-
kvpairs: &Vec<KVPair>,
65-
) -> anyhow::Result<()> {
66-
let stringified_kvpairs = kvpairs
67-
.iter()
68-
.map(|kv| kv.to_string())
69-
.collect::<Vec<String>>()
70-
.join("\n");
71-
72-
let spk = [SignedPublicKey::try_from(key)?];
73-
let msg = encrypt(&stringified_kvpairs, &spk)?;
74-
75-
let envx_file = home_dir()
76-
.context("Failed to get home directory")?
77-
.join(format!(".config/envx/{}.envx", &project_id));
78-
79-
let mut file = std::fs::File::create(envx_file)?;
80-
file.write_all(msg.as_bytes())?;
81-
82-
Ok(())
83-
}

0 commit comments

Comments
 (0)