summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-23 22:53:13 -0400
committerKjetil Orbekk <kjetil.orbekk@gmail.com>2017-05-23 22:53:13 -0400
commit92ab79b327e4b3e7fb8e7b8688b1c63a284ada1c (patch)
treea0da345714fa1201976475fc29a912ead9ac0aef /src
parentb22fc3f433f91e1c893122794415f0f61f08e95c (diff)
systemhttpd: Very basic unit listing
Diffstat (limited to 'src')
-rw-r--r--src/main.rs33
-rw-r--r--src/systemd/job.rs30
2 files changed, 44 insertions, 19 deletions
diff --git a/src/main.rs b/src/main.rs
index 5f9009f..3fe1b7f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,22 +8,21 @@ extern crate router;
extern crate env_logger;
#[macro_use]
extern crate horrorshow;
-
+extern crate systemhttp;
extern crate iron_sessionstorage;
+
use iron_sessionstorage::traits::*;
use iron_sessionstorage::SessionStorage;
use iron_sessionstorage::backends::SignedCookieBackend;
-
use iron::status;
use iron::modifiers::Header;
use iron::headers::ContentType;
use iron::{Iron, Request, IronResult, Response, Chain};
use router::Router;
-
use horrorshow::prelude::*;
use horrorshow::Raw;
-
use std::process::Command;
+use systemhttp::systemd::job;
struct Aaa(String);
@@ -39,7 +38,7 @@ impl iron_sessionstorage::Value for Aaa {
}
}
-fn render_message(message: &str) -> String {
+fn render_message(message: &str, units: &[job::Unit]) -> String {
(html!{
: Raw("<!DOCTYPE html>");
html {
@@ -48,6 +47,16 @@ fn render_message(message: &str) -> String {
p {
: message
}
+ h1 {
+ : "Units"
+ }
+ ol {
+ @ for unit in units {
+ li {
+ : &unit.name
+ }
+ }
+ }
}
}
})
@@ -66,15 +75,15 @@ fn hello(r: &mut Request) -> IronResult<Response> {
.unwrap()
.find("name")
.unwrap_or("World").to_owned();
- let output = Command::new("date").output().unwrap().stdout;
- let date = String::from_utf8_lossy(&output);
+
+ let jobs = job::get_jobs().unwrap();
let res = Ok(Response::with((status::Ok,
Header(ContentType::html()),
- render_message(&format!("Hello, {}. The time is {}. {}",
+ render_message(&format!("Hello, {} ({})",
name,
- date,
- session_value.0)))));
+ session_value.0),
+ &jobs))));
info!("Updating session value. Current value: {}", session_value.0);
session_value.0.push('a');
@@ -82,11 +91,7 @@ fn hello(r: &mut Request) -> IronResult<Response> {
res
}
-extern crate systemhttp;
fn main() {
- use systemhttp::systemd::job;
- let u = job::get_jobs().unwrap();
- println!("{:?}", u);
env_logger::init().unwrap();
let secret = b"secret2".to_vec();
diff --git a/src/systemd/job.rs b/src/systemd/job.rs
index 68fb18d..967bb42 100644
--- a/src/systemd/job.rs
+++ b/src/systemd/job.rs
@@ -5,8 +5,23 @@ use std::io;
#[derive(Debug)]
pub struct Unit {
- name: String,
- type_: String
+ pub name: String,
+ pub type_: String
+}
+
+pub fn parse_key_value(line: &str) -> Option<(String, String)> {
+ let pos = line.find('=');
+ pos.map(|pos| {
+ let (k, v) = line.split_at(pos);
+ (k.to_owned(), v[1..].to_owned())
+ })
+}
+
+#[test]
+fn test_parse_key_value() {
+ assert_eq!(None, parse_key_value("ab"));
+ assert_eq!(Some(("a".to_owned(), "b".to_owned())), parse_key_value("a=b"));
+ assert_eq!(Some(("a".to_owned(), "b=c".to_owned())), parse_key_value("a=b=c"));
}
pub fn get_jobs() -> io::Result<Vec<Unit>> {
@@ -14,10 +29,15 @@ pub fn get_jobs() -> io::Result<Vec<Unit>> {
let status = try!(Command::new("systemctl")
.args(&["show", "*"]).output());
+ let mut unit_info = HashMap::new();
for line in String::from_utf8_lossy(&status.stdout).split('\n') {
- let words = line.split('=').collect::<Vec<&str>>();
- if words.len() == 2 && words[0] == "Id" {
- units.push(Unit { name: words[1].to_owned(), type_: "Unknown".to_owned() });
+ if let Some((k, v)) = parse_key_value(line) {
+ unit_info.insert(k, v);
+ } else {
+ if let Some(id) = unit_info.remove("Id") {
+ units.push(Unit { name: id, type_: String::new() });
+ }
+ unit_info = HashMap::new();
}
}
return Ok(units);