comparison rust/src/main.rs @ 598:d4fbfb5c46ff rust

broken update of versions of things
author Matt Johnston <matt@ucc.asn.au>
date Tue, 07 Feb 2017 21:56:58 +0800
parents a440eafa84a9
children f71cf1ad745f
comparison
equal deleted inserted replaced
597:a440eafa84a9 598:d4fbfb5c46ff
1 #![feature(plugin)]
2 #![plugin(docopt_macros)]
3
4 #![feature(proc_macro)]
5
6 extern crate tokio_core; 1 extern crate tokio_core;
7 extern crate futures; 2 extern crate futures;
8 #[macro_use] 3 #[macro_use]
9 extern crate log; 4 extern crate log;
10 extern crate env_logger; 5 extern crate env_logger;
16 extern crate serde; 11 extern crate serde;
17 12
18 extern crate toml; 13 extern crate toml;
19 14
20 extern crate docopt; 15 extern crate docopt;
21
22 16
23 use std::io; 17 use std::io;
24 18
25 use tokio_core::reactor::Core; 19 use tokio_core::reactor::Core;
26 use futures::{Stream,Sink,Future}; 20 use futures::{Stream,Sink,Future};
81 let all = all_fridge.select(all_readings); 75 let all = all_fridge.select(all_readings);
82 76
83 core.run(all).ok(); 77 core.run(all).ok();
84 } 78 }
85 79
86 docopt!(Args, " 80 const USAGE: &'static str = "\
87 Wort Temperature 81 Wort Temperature
88 Matt Johnston 2017 [email protected] 82 Matt Johnston 2017 [email protected]
89 Usage: wort-templog [--help] [--new] [--daemon] [--debug] [--test] [--defconf] [--nowait] 83 Usage: wort-templog [--help] [--new] [--daemon] [--debug] [--test] [--defconf] [--thisconf] [--nowait]
90 84
91 Options: 85 Options:
92 -h, --help 86 -h, --help
93 --new Replace existing running instance 87 --new Replace existing running instance
94 -D, --daemon Run in background 88 -D, --daemon Run in background
95 -d, --debug 89 -d, --debug
96 -t, --test Use fake sensors etc 90 -t, --test Use fake sensors etc
97 --nowait Skip initial fridge wait 91 --nowait Skip initial fridge wait
98 --defconf Print default config (customise in tempserver.conf) 92 --defconf Print default config (customise in tempserver.conf)
99 --thisconf Print used config 93 --thisconf Print used config
100 "); 94 ";
95
96 #[derive(RustcDecodable)]
97 struct Args {
98 new: bool,
99 daemon: bool,
100 debug: bool,
101 test: bool,
102 defconf: bool,
103 thisconf: bool,
104 nowait: bool,
105 }
101 106
102 fn setup_log(debug: bool) { 107 fn setup_log(debug: bool) {
103 let loglevel = if debug { 108 let loglevel = if debug {
104 log::LogLevelFilter::Debug 109 log::LogLevelFilter::Debug
105 } else { 110 } else {
117 builder.format(format).filter(Some("wort_templog"), loglevel); 122 builder.format(format).filter(Some("wort_templog"), loglevel);
118 builder.init().unwrap(); 123 builder.init().unwrap();
119 } 124 }
120 125
121 fn handle_args() -> Args { 126 fn handle_args() -> Args {
122 let args: Args = Args::docopt().decode().unwrap_or_else(|e| e.exit()); 127 let args: Args = docopt::Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit());
123 128
124 if args.flag_defconf { 129 if args.defconf {
125 println!("Default configuration:\n{}\n\n{}", 130 println!("Default configuration:\n{}\n\n{}",
126 "(custom options go in tempserver.conf)", 131 "(custom options go in tempserver.conf)",
127 config::Config::new().to_toml_string()); 132 config::Config::new().to_toml_string());
128 std::process::exit(0); 133 std::process::exit(0);
129 } 134 }
130
131 args
132 }
133
134 fn load_config(config: &mut Config) {
135
136 } 135 }
137 136
138 fn main() { 137 fn main() {
139 let mut config = config::Config::new(); 138 let mut config = config::Config::new();
140 139
141 let args = handle_args(); 140 let args = handle_args();
142 setup_log(args.flag_debug); 141 setup_log(args.debug);
143 //env_logger::init().unwrap(); 142 //env_logger::init().unwrap();
144 143
145 info!("wort-templog"); 144 info!("wort-templog");
146 debug!("debug mode"); 145 debug!("debug mode");
147 146
148 load_config(&mut config); 147 let conf_filename = "tempserver.conf";
148 println!("parse config");
149 config.parse(conf_filename)
150 .unwrap_or_else(|e| {
151 panic!("Couldn't parse {}: {}", conf_filename, e);
152 });
149 153
150 let config = config; 154 if args.thisconf {
151 155 println!("Current configuration:\n\n{}",
152 if args.flag_thisconf {
153 println!("current configuration:\n\n{}",
154 config.to_toml_string()); 156 config.to_toml_string());
155 } 157 }
156 158
157 159 run(&config, args.nowait, args.test);
158 run(&config, args.flag_nowait, args.flag_test);
159 } 160 }
160 161