-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-merge for PR #110 via VersionBot
Error handling with error-chain
- Loading branch information
Showing
14 changed files
with
369 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "network-manager" | ||
version = "0.8.6" | ||
version = "0.9.0" | ||
authors = ["Zahari Petkov <[email protected]>", "Aaron Brodersen <[email protected]>"] | ||
description = "Rust NetworkManager bindings" | ||
homepage = "https://github.com/resin-io-modules/network-manager" | ||
|
@@ -17,3 +17,7 @@ tokio-timer = "0.1" | |
bitflags = "0.9" | ||
ascii = "0.8" | ||
log = "0.3" | ||
|
||
[dependencies.error-chain] | ||
version = "0.11" | ||
default-features = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,97 @@ | ||
#[macro_use] | ||
extern crate error_chain; | ||
|
||
extern crate network_manager; | ||
|
||
use std::env; | ||
use std::process; | ||
use std::io::Write; | ||
|
||
use network_manager::{AccessPoint, Device, DeviceType, NetworkManager}; | ||
|
||
mod errors { | ||
use network_manager; | ||
|
||
error_chain! { | ||
links { | ||
NetworkManager(network_manager::errors::Error, network_manager::errors::ErrorKind); | ||
} | ||
|
||
errors { | ||
Runtime(info: String) { | ||
description("Runtime error") | ||
display("{}", info) | ||
} | ||
} | ||
} | ||
} | ||
|
||
use network_manager::{DeviceType, NetworkManager}; | ||
use errors::*; | ||
|
||
fn main() { | ||
if let Err(ref e) = run() { | ||
let stderr = &mut ::std::io::stderr(); | ||
let errmsg = "Error writing to stderr"; | ||
|
||
writeln!(stderr, "{}", e).expect(errmsg); | ||
|
||
for e in e.iter().skip(1) { | ||
writeln!(stderr, " caused by: {}", e).expect(errmsg); | ||
} | ||
|
||
::std::process::exit(1); | ||
} | ||
} | ||
|
||
fn run() -> Result<()> { | ||
let args: Vec<String> = env::args().collect(); | ||
|
||
if args.len() < 3 { | ||
if args.len() != 3 { | ||
println!("USAGE: create SSID PASSWORD"); | ||
process::exit(1); | ||
} | ||
|
||
let manager = NetworkManager::new(); | ||
|
||
let devices = manager.get_devices().unwrap(); | ||
let device_index = devices | ||
.iter() | ||
.position(|d| *d.device_type() == DeviceType::WiFi) | ||
.unwrap(); | ||
let wifi_device = devices[device_index].as_wifi_device().unwrap(); | ||
let device = find_device(&manager)?; | ||
|
||
let access_points = wifi_device.get_access_points().unwrap(); | ||
let wifi_device = device.as_wifi_device().unwrap(); | ||
|
||
let ap_index = access_points | ||
let access_points = wifi_device.get_access_points()?; | ||
|
||
let ap_index = find_access_point(&access_points, &args[1] as &str)?; | ||
|
||
wifi_device.connect(&access_points[ap_index], &args[2] as &str)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn find_device(manager: &NetworkManager) -> Result<Device> { | ||
let devices = manager.get_devices()?; | ||
|
||
let index = devices | ||
.iter() | ||
.position(|ap| ap.ssid().as_str().unwrap() == args[1]) | ||
.unwrap(); | ||
.position(|d| *d.device_type() == DeviceType::WiFi); | ||
|
||
wifi_device | ||
.connect(&access_points[ap_index], &args[2] as &str) | ||
.unwrap(); | ||
if let Some(index) = index { | ||
Ok(devices[index].clone()) | ||
} else { | ||
bail!(ErrorKind::Runtime("Cannot find a WiFi device".into())) | ||
} | ||
} | ||
|
||
fn find_access_point(access_points: &[AccessPoint], ssid: &str) -> Result<usize> { | ||
if let Some(index) = access_points.iter().position(|ap| same_ssid(ap, ssid)) { | ||
Ok(index) | ||
} else { | ||
bail!(ErrorKind::Runtime("Access point not found".into())) | ||
} | ||
} | ||
|
||
fn same_ssid(ap: &AccessPoint, ssid: &str) -> bool { | ||
if let Ok(ap_ssid) = ap.ssid().as_str() { | ||
ap_ssid == ssid | ||
} else { | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.