Skip to content

Commit d6d774a

Browse files
committed
feat: add network validator and fix warnings
1 parent bc5e1e5 commit d6d774a

6 files changed

Lines changed: 85 additions & 9 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ anyhow = "1.0"
2323
thiserror = "1.0"
2424
regex = "1.10"
2525
serde_json = "1.0"
26+
reqwest = { version = "0.11", features = ["blocking", "rustls-tls"], default-features = false }
2627

2728
[dev-dependencies]
2829
tempfile = "3.10"

src/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub struct Config {
1414
pub ports: Vec<u16>,
1515
#[serde(default)]
1616
pub files: Vec<FileCheck>,
17+
#[serde(default)]
18+
pub network: Vec<NetworkCheck>,
1719
}
1820

1921
#[derive(Debug, Deserialize, Serialize, Clone)]
@@ -45,6 +47,13 @@ pub struct FileCheck {
4547
pub permissions: Option<u32>,
4648
}
4749

50+
#[derive(Debug, Deserialize, Serialize, Clone)]
51+
pub struct NetworkCheck {
52+
pub url: String,
53+
#[serde(default)]
54+
pub status_code: Option<u16>,
55+
}
56+
4857
fn default_true() -> bool {
4958
true
5059
}

src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ ports:
7171
files:
7272
- path: .env
7373
required: false
74+
network:
75+
- url: https://github.com
76+
status_code: 200
7477
"#;
7578

7679
std::fs::write(config_path, default_config)?;

src/validators/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod tool;
66
pub mod env;
77
pub mod port;
88
pub mod file;
9+
pub mod network;
910

1011
#[derive(Debug, Clone, Serialize)]
1112
#[serde(rename_all = "lowercase")]
@@ -79,5 +80,11 @@ pub fn run_all_validations(config: &Config) -> Result<Vec<ValidationResult>> {
7980
results.extend(validator.validate()?);
8081
}
8182

83+
// Validate network
84+
for network_check in &config.network {
85+
let validator = network::NetworkValidator::new(network_check.clone());
86+
results.extend(validator.validate()?);
87+
}
88+
8289
Ok(results)
8390
}

src/validators/network.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use crate::config::NetworkCheck;
2+
use crate::validators::{ValidationResult, Validator};
3+
use anyhow::Result;
4+
use reqwest::blocking::Client;
5+
use std::time::Duration;
6+
7+
pub struct NetworkValidator {
8+
check: NetworkCheck,
9+
client: Client,
10+
}
11+
12+
impl NetworkValidator {
13+
pub fn new(check: NetworkCheck) -> Self {
14+
let client = Client::builder()
15+
.timeout(Duration::from_secs(5))
16+
.build()
17+
.unwrap_or_else(|_| Client::new());
18+
19+
Self { check, client }
20+
}
21+
}
22+
23+
impl Validator for NetworkValidator {
24+
fn validate(&self) -> Result<Vec<ValidationResult>> {
25+
let mut results = Vec::new();
26+
27+
let url = &self.check.url;
28+
let expected_status = self.check.status_code.unwrap_or(200);
29+
30+
match self.client.get(url).send() {
31+
Ok(response) => {
32+
let status = response.status().as_u16();
33+
34+
if status == expected_status {
35+
results.push(ValidationResult::success(format!(
36+
"Successfully connected to {} (Status: {})",
37+
url, status
38+
)));
39+
} else {
40+
results.push(ValidationResult::error(
41+
format!("Connected to {} but got status {}", url, status),
42+
Some(format!("Expected status code {}", expected_status)),
43+
));
44+
}
45+
}
46+
Err(e) => {
47+
results.push(ValidationResult::error(
48+
format!("Failed to connect to {}", url),
49+
Some(format!("Error: {}", e)),
50+
));
51+
}
52+
}
53+
54+
Ok(results)
55+
}
56+
}

tests/integration_tests.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::Write;
55

66
#[test]
77
fn test_cli_help() {
8-
let mut cmd = Command::cargo_bin("envcheck").unwrap();
8+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_envcheck"));
99
cmd.arg("--help");
1010
cmd.assert()
1111
.success()
@@ -14,7 +14,7 @@ fn test_cli_help() {
1414

1515
#[test]
1616
fn test_cli_config_not_found() {
17-
let mut cmd = Command::cargo_bin("envcheck").unwrap();
17+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_envcheck"));
1818
cmd.arg("--config").arg("non_existent_file.yaml");
1919
cmd.assert()
2020
.failure()
@@ -41,7 +41,7 @@ files:
4141
path_str
4242
).unwrap();
4343

44-
let mut cmd = Command::cargo_bin("envcheck").unwrap();
44+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_envcheck"));
4545
cmd.arg("--config").arg(file.path());
4646

4747
// Node check might fail if not installed in CI environment, but PATH and file should pass
@@ -61,7 +61,7 @@ ports:
6161
"#
6262
).unwrap();
6363

64-
let mut cmd = Command::cargo_bin("envcheck").unwrap();
64+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_envcheck"));
6565
cmd.arg("--config").arg(file.path());
6666
cmd.assert()
6767
.success()
@@ -73,7 +73,7 @@ fn test_cli_init() {
7373
let temp_dir = tempfile::tempdir().unwrap();
7474
let config_path = temp_dir.path().join(".envcheck.yaml");
7575

76-
let mut cmd = Command::cargo_bin("envcheck").unwrap();
76+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_envcheck"));
7777
cmd.current_dir(temp_dir.path()).arg("init");
7878
cmd.assert().success();
7979

@@ -95,7 +95,7 @@ env_vars:
9595
"#
9696
).unwrap();
9797

98-
let mut cmd = Command::cargo_bin("envcheck").unwrap();
98+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_envcheck"));
9999
cmd.arg("--config").arg(file.path()).arg("--json");
100100

101101
let output = cmd.assert().success().get_output().stdout.clone();
@@ -122,15 +122,15 @@ env_vars:
122122
).unwrap();
123123

124124
// Test failure
125-
let mut cmd = Command::cargo_bin("envcheck").unwrap();
125+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_envcheck"));
126126
cmd.arg("--config").arg(file.path())
127127
.env("TEST_REGEX_VAR", "abc");
128128
cmd.assert()
129129
.failure()
130130
.stdout(predicate::str::contains("TEST_REGEX_VAR is set but does not match pattern"));
131131

132132
// Test success
133-
let mut cmd = Command::cargo_bin("envcheck").unwrap();
133+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_envcheck"));
134134
cmd.arg("--config").arg(file.path())
135135
.env("TEST_REGEX_VAR", "123");
136136
cmd.assert()
@@ -155,7 +155,7 @@ files:
155155
dir_path
156156
).unwrap();
157157

158-
let mut cmd = Command::cargo_bin("envcheck").unwrap();
158+
let mut cmd = Command::new(env!("CARGO_BIN_EXE_envcheck"));
159159
cmd.arg("--config").arg(file.path());
160160
cmd.assert()
161161
.success()

0 commit comments

Comments
 (0)