diff options
author | Simon Garrelou <simon@sixfoisneuf.fr> | 2023-04-27 20:54:22 +0200 |
---|---|---|
committer | Simon Garrelou <simon@sixfoisneuf.fr> | 2023-04-27 20:54:22 +0200 |
commit | cf7ef1afee44691a13fb3636ca79e59f3309d75f (patch) | |
tree | 8860103052fe746f47c987b6a4c46e44d6b2efda | |
parent | b22f4785655c2ea8a9c06a6fada9363891c40d4e (diff) | |
download | wgmgr-cf7ef1afee44691a13fb3636ca79e59f3309d75f.tar.gz wgmgr-cf7ef1afee44691a13fb3636ca79e59f3309d75f.zip |
replace panics with Result
-rw-r--r-- | src/wg/config.rs | 2 | ||||
-rw-r--r-- | src/wg/peer.rs | 23 |
2 files changed, 13 insertions, 12 deletions
diff --git a/src/wg/config.rs b/src/wg/config.rs index 4a07287..2dac6a8 100644 --- a/src/wg/config.rs +++ b/src/wg/config.rs | |||
@@ -193,4 +193,4 @@ fn config_value(line: &str) -> Result<&str> { | |||
193 | Err(anyhow!("line does not seem to contain a key-value pair")) | 193 | Err(anyhow!("line does not seem to contain a key-value pair")) |
194 | } | 194 | } |
195 | } | 195 | } |
196 | } \ No newline at end of file | 196 | } |
diff --git a/src/wg/peer.rs b/src/wg/peer.rs index c374ca0..1f9a173 100644 --- a/src/wg/peer.rs +++ b/src/wg/peer.rs | |||
@@ -6,7 +6,7 @@ use std::path::{Path}; | |||
6 | use std::process::{Command, Stdio}; | 6 | use std::process::{Command, Stdio}; |
7 | use std::io::Write as ioWrite; | 7 | use std::io::Write as ioWrite; |
8 | 8 | ||
9 | use anyhow::{Context, Result}; | 9 | use anyhow::{Context, Result, anyhow}; |
10 | 10 | ||
11 | use super::config::WireguardConfig; | 11 | use super::config::WireguardConfig; |
12 | 12 | ||
@@ -52,38 +52,39 @@ impl Peer { | |||
52 | public_key: String::from(public_key) | 52 | public_key: String::from(public_key) |
53 | }; | 53 | }; |
54 | 54 | ||
55 | let mut f = File::create(p.private_key_path()).context(format!("could not create file {}", p.private_key_path()))?; | 55 | let pk_path = p.private_key_path()?; |
56 | write!(f, "{}", private_key).expect("could not write private key to file"); | 56 | let mut f = File::create(pk_path.clone()).context(format!("could not create file {}", pk_path))?; |
57 | write!(f, "{}", private_key).context("could not write private key to file")?; | ||
57 | 58 | ||
58 | 59 | ||
59 | Ok(p) | 60 | Ok(p) |
60 | } | 61 | } |
61 | 62 | ||
62 | fn private_key_folder(&self) -> String { | 63 | fn private_key_folder(&self) -> Result<String> { |
63 | let pk_folder = String::from("./private_keys/"); | 64 | let pk_folder = String::from("./private_keys/"); |
64 | 65 | ||
65 | let pk_path = Path::new(pk_folder.as_str()); | 66 | let pk_path = Path::new(pk_folder.as_str()); |
66 | if !pk_path.exists() { | 67 | if !pk_path.exists() { |
67 | fs::create_dir(pk_path).expect("Could not create private keys folder"); | 68 | fs::create_dir(pk_path).context(format!("creating {} folder", pk_folder))?; |
68 | } | 69 | } |
69 | 70 | ||
70 | if !pk_path.is_dir() { | 71 | if !pk_path.is_dir() { |
71 | panic!("Error: the private key folder exists but is not a directory") | 72 | return Err(anyhow!("Error: the private key folder exists but is not a directory")); |
72 | } | 73 | } |
73 | 74 | ||
74 | pk_folder | 75 | Ok(pk_folder) |
75 | } | 76 | } |
76 | 77 | ||
77 | fn private_key_path(&self) -> String { | 78 | fn private_key_path(&self) -> Result<String> { |
78 | let folder_name = self.private_key_folder(); | 79 | let folder_name = self.private_key_folder()?; |
79 | let pk_folder = Path::new(folder_name.as_str()); | 80 | let pk_folder = Path::new(folder_name.as_str()); |
80 | let pk_path = pk_folder.join(self.name.clone()); | 81 | let pk_path = pk_folder.join(self.name.clone()); |
81 | 82 | ||
82 | String::from(pk_path.to_str().unwrap()) | 83 | Ok(String::from(pk_path.to_str().unwrap())) |
83 | } | 84 | } |
84 | 85 | ||
85 | pub fn private_key(&self) -> Result<String> { | 86 | pub fn private_key(&self) -> Result<String> { |
86 | let pk_folder = self.private_key_folder(); | 87 | let pk_folder = self.private_key_folder()?; |
87 | let pk_path = join_paths(&[pk_folder, self.name.clone()])?; | 88 | let pk_path = join_paths(&[pk_folder, self.name.clone()])?; |
88 | let pk = read_to_string(pk_path)?; | 89 | let pk = read_to_string(pk_path)?; |
89 | 90 | ||