Skip to content

Instantly share code, notes, and snippets.

@adamhjk
adamhjk / sample.rs
Created March 23, 2015 22:42
sysadmin crate?
extern crate sysadmin;
use sysadmin::{file};
// Use docopt.rs to generate the cli arguments, process ARGV,
// and set up a main() that runs the run function. Checks the
// results and runs at_exit with nice error messages and a variety
// of error codes
cli!(
"Usage: wc [--lines] <file>
@adamhjk
adamhjk / config.rs
Created February 24, 2015 22:26
This used to work..
use rustc_serialize::Encodable;
use toml;
#[derive(RustcEncodable, Clone)]
pub struct Config {
pub server: Option<String>,
pub user: Option<String>,
pub enterprise: Option<String>,
pub organization: Option<String>,
pub project: Option<String>,
struct Config {
server: Option<String>,
user: Option<String>
}
impl Config {
get(&self, key: &str) -> Result<String, Error> {
match self.get(key) {
Some(v) => return Ok(v),
None => Err(Error{kind: Kind::MissingValue, description: Some(format!("Missing value for {}", key))})
Compiling delivery v0.0.1 (file:///Users/adam/src/opscode/delivery/opscode/delivery-cli)
/Users/adam/src/opscode/delivery/opscode/delivery-cli/src/utils/say.rs:11:12: 11:25 error: wrong number of lifetime parameters: expected 1, found 0 [E0107]
/Users/adam/src/opscode/delivery/opscode/delivery-cli/src/utils/say.rs:11 guard: JoinGuard<()>
^~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `delivery`.
To learn more, run the command again with --verbose.
#![feature(plugin)]
extern crate regex;
#[plugin] extern crate regex_macros;
extern crate "rustc-serialize" as rustc_serialize;
#[macro_use] extern crate docopt;
#[plugin] extern crate docopt_macros;
#[macro_use] extern crate log;
#![feature(phase)]
#![feature(old_orphan_check)]
extern crate regex;
#[macro_use] extern crate regex_macros;
extern crate "rustc-serialize" as rustc_serialize;
extern crate docopt;
#[macro_use] extern crate docopt_macros;
#[macro_use] extern crate log;
use std::os;
@adamhjk
adamhjk / chef.rs
Created November 19, 2014 00:56
Chef in rust
extern crate chef;
use chef::{client};
use chef::resource::{Package, Service, Template};
// Total Strawman
fn main() {
let mut chef = client.new();
chef.r(
Package{
fn current_branch(path: &Path) -> String {
let head_ref = head_ref(path);
println!("The ref for HEAD is {}", head_ref);
let r = regex!(r"refs/heads/(.+)");
let caps_result = r.captures(head_ref.as_slice());
let branch: String = match caps_result {
Some(cap) => { String::from_str(cap.at(1)) },
None => { panic!("You must be somewhere on refs/heads, and you aren't") }
};
branch
func current_branch(git_dir string) string {
head_bytes, err := ioutil.ReadFile(path.Join(git_dir, "HEAD"))
head := string(head_bytes)
if err != nil {
log.Fatal("Cannot open HEAD from ", git_dir)
}
r, _ := regexp.Compile("ref: ref/heads/(.+)")
match := r.FindStringSubmatch(head)
branch := match[1]
return branch
func dot_git_dir(dir string) string {
git_dir := path.Join(dir, ".git")
_, dir_error := os.Stat(git_dir)
if dir_error != nil {
if git_dir == "/.git" {
log.Fatal("Cannot find a .git directory")
}
return dot_git_dir(path.Dir(dir))
} else {
return git_dir