aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: ad067a350d7f66c7474e6f1e92fbaa48158f1cbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

use structopt::clap::{App, AppSettings, Arg, ArgMatches, Shell, SubCommand};

use crate::{config::Config, subs::run};

pub struct Cli;
impl Cli {
  pub fn setup() -> ArgMatches<'static> {
    let matches = Self::cli().get_matches();

    Self::calc_log_level(&matches);
    std::env::set_var("DATABASE_URL", "whirl.sqlite3");

    matches
  }

  pub async fn execute(matches: ArgMatches<'_>) {
    if matches.is_present("run") {
      run().await;
    } else if let Some(cmd) = matches.subcommand_matches("config") {
      if cmd.is_present("show") {
        println!("{:#?}", Config::get());
      }
    } else if let Some(shell) = matches.subcommand_matches("completions") {
      if shell.is_present("powershell") {
        Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::PowerShell, ".");
      } else if shell.is_present("bash") {
        Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Bash, ".");
      } else if shell.is_present("elvish") {
        Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Elvish, ".");
      } else if shell.is_present("zsh") {
        Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Zsh, ".");
      } else if shell.is_present("fish") {
        Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Fish, ".");
      }
      debug!("generated shell completions");
    }
  }

  fn calc_log_level(matches: &ArgMatches<'_>) {
    let mut log_level = "whirl=error,whirl=warn,whirl=trace".to_string();
    if matches.is_present("debug") || Config::get().whirlsplash.log_level >= 2 {
      log_level += ",whirl=debug";
    }
    if matches.is_present("trace") || Config::get().whirlsplash.log_level >= 3 {
      log_level += ",whirl=trace";
    }
    std::env::set_var("RUST_LOG", log_level);
  }

  fn cli<'a, 'b>() -> App<'a, 'b> {
    App::new(env!("CARGO_PKG_NAME"))
      .about(env!("CARGO_PKG_DESCRIPTION"))
      .version(env!("CARGO_PKG_VERSION"))
      .author(env!("CARGO_PKG_AUTHORS"))
      .settings(&[AppSettings::SubcommandRequiredElseHelp])
      .subcommands(vec![
        SubCommand::with_name("run").about("Start the WorldServer"),
        SubCommand::with_name("config")
          .setting(AppSettings::SubcommandRequiredElseHelp)
          .subcommands(vec![SubCommand::with_name("show")]),
        SubCommand::with_name("completions")
          .setting(AppSettings::SubcommandRequiredElseHelp)
          .about("Generate shell completions")
          .subcommands(vec![
            SubCommand::with_name("powershell"),
            SubCommand::with_name("bash"),
            SubCommand::with_name("elvish"),
            SubCommand::with_name("zsh"),
            SubCommand::with_name("fish"),
          ]),
      ])
      .args(&[
        Arg::with_name("debug").short("d").long("debug"),
        Arg::with_name("trace").short("t").long("trace"),
      ])
  }
}