1extern crate ansi_term;
2extern crate serde;
3#[macro_use]
4extern crate serde_derive;
5extern crate serde_yaml;
6
7use cli::run_cli;
8use commands::{help_command, init, util::no_auto_complete};
9use config::file_config_source;
10use model::{Command, Internal, Section};
11use std::env::{args, var, VarError};
12use std::path::PathBuf;
13
14macro_rules! s {
15 () => (String::new());
16 ($($arg:tt)*) => (String::from($($arg)*));
17}
18
19mod commands;
20mod model;
21mod config;
22mod dto;
23mod util;
24mod workflow;
25mod cli;
26
27#[cfg(test)]
28mod test_helper;
29
30pub fn run() {
31 let args = build_args();
32 match get_commands_directory() {
33 Ok(commands_directory) => run_cli(commands_directory, args, file_config_source),
34 Err(_) => run_cli(PathBuf::new(), args, init_config_source)
35 }
36}
37
38fn get_commands_directory() -> Result<PathBuf, VarError> {
39 var("COMMANDS_DIRECTORY").map(PathBuf::from)
40}
41
42fn build_args() -> Vec<String> {
43 let mut args: Vec<String> = args().collect();
44
45 if let Ok(a) = var("CLI_NAME") {
46 args.remove(0);
47 args.insert(0, a)
48 }
49
50 args
51}
52
53fn init_config_source(_: &PathBuf) -> Vec<Section> {
54 let init_command = Command {
55 value: None,
56 internal: Internal {
57 execute: init::run_setup,
58 auto_complete: no_auto_complete,
59 },
60 description: s!("Initialise a new cli"),
61 alias: Some(s!("i")),
62 usage: Some(s!("[directory]")),
63 name: s!("init"),
64 min_args: None,
65 dependencies: None,
66 };
67
68 let commands = Section {
69 heading: s!("Commands"),
70 commands: vec![init_command, help_command()],
71 core: false
72 };
73
74 vec![commands]
75}