aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl/src/cli.rs
blob: e9b6f0718f5ef33b22934a8cfc470d916c24fc5c (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

use {
  std::{io::Write, str::FromStr},
  structopt::clap::{App, AppSettings, Arg, SubCommand},
  whirl_config::Config,
};

enum RunType {
  Distributor,
  Hub,
  Api,
}
impl FromStr for RunType {
  type Err = &'static str;

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    match s {
      "distributor" => Ok(Self::Distributor),
      "hub" => Ok(Self::Hub),
      "api" => Ok(Self::Api),
      _ => Err("no match"),
    }
  }
}

// https://stackoverflow.com/questions/57888454/how-to-remove-duplicates-from-a-vector-of-structures
trait Dedup<T: PartialEq + Clone> {
  fn clear_duplicates(&mut self);
}
impl<T: PartialEq + Clone> Dedup<T> for Vec<T> {
  fn clear_duplicates(&mut self) {
    let mut already_seen = vec![];
    self.retain(|item| {
      if already_seen.contains(item) {
        false
      } else {
        already_seen.push(item.clone());

        true
      }
    });
  }
}

pub struct Cli;
impl Cli {
  /// # Panics
  /// Though Rust thinks a panic might happen because of the `unreachable`
  /// macro, all CLI subcommands are handled so it is trully unreachable.
  pub async fn execute() {
    let matches = Self::cli().get_matches();

    if Config::get().whirlsplash.log.test {
      error!("error");
      warn!("warn");
      info!("info");
      debug!("debug");
      trace!("trace");
    }

    debug!("attempting to create .whirl directory...");
    match std::fs::create_dir(".whirl/") {
      Ok(()) => debug!("successfully created .whirl directory"),
      Err(e) => debug!("error creating .whirl directory: {}", e),
    }

    match matches.subcommand() {
      ("run", Some(s_matches)) =>
        Self::run({
          // Make a vector of the passed types for the `run` sub-command, if the
          // vector cannot be unwrapped, the must mean that the user did not
          // pass any sub-server types meaning that the subcommand `run` was
          // called without any arguments, in other words: "run everything".
          let mut types = {
            s_matches.values_of("type").map_or_else(
              || vec!["distributor", "hub", "api"],
              std::iter::Iterator::collect,
            )
          };
          // Remove any duplicate sub-commands, we don't want to start two
          // instances of the same sub-server.
          types.clear_duplicates();

          let mut run_types = vec![];
          // Iterate over all of the types and push them to a vector that we'll
          // pass to the `run` method.
          while let Some(run_type) = types.last() {
            match run_type.to_owned() {
              "distributor" => run_types.push(RunType::Distributor),
              "hub" => run_types.push(RunType::Hub),
              "api" => run_types.push(RunType::Api),
              _ => {}
            }
            types.pop();
          }

          run_types
        })
        .await,
      ("config", Some(s_matches)) => match s_matches.subcommand() {
        ("show", _) => println!("{:#?}", Config::get()),
        ("generate", Some(s_s_matches)) => {
          if std::path::Path::new(".whirl/Config.toml").exists()
            && !s_s_matches.is_present("force")
          {
            info!(
              "a configuration file is already present, if you would like to \
               regenerate the configuration file, execute this sub-command \
               with the `--force` (`-f`) flag"
            );
          } else {
            let mut file = std::fs::File::create(".whirl/Config.toml")
              .expect("unable to create configuration file");
            file
              .write_all(include_bytes!(
                "../../whirl_config/Config.default.toml"
              ))
              .expect(
                "unable to write default configuration to generated \
                 configuration file",
              );
            info!("successfully generated a new configuration file");
          }
        }
        _ => unreachable!(),
      },
      ("clean", _) => {
        let cleanable_directories = vec![".whirl/log/"];
        for dir in cleanable_directories {
          let file_type = if dir.ends_with('/') { "directory" } else { "file" };
          info!("cleaning {}: {}", file_type, dir);
          if let Err(e) = std::fs::remove_dir_all(dir) {
            warn!("cannot delete {}: {}: {}", file_type, dir, e);
          }
        }
      }
      _ => unreachable!(),
    }
  }

  fn cli() -> App<'static, 'static> {
    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 or a selection of sub-servers.")
          .long_about(
            "Start the WorldServer by executing this sub-command WITHOUT any \
             arguments, start a selection of sub-servers by passing a \
             comma-separated list of sub-server types.",
          )
          .arg(
            Arg::with_name("type")
              .required(false)
              .takes_value(true)
              .index(1)
              .use_delimiter(true)
              .possible_values(&["distributor", "hub", "api"]),
          ),
        SubCommand::with_name("config")
          .setting(AppSettings::SubcommandRequiredElseHelp)
          .subcommands(vec![
            SubCommand::with_name("show"),
            SubCommand::with_name("generate")
              .alias("gen")
              .arg(Arg::with_name("force").short("f").long("force")),
          ]),
        SubCommand::with_name("clean").about(
          "Delete Whirl-generated files/ directories which are NOT critical. \
           E.g., .whirl/logs/",
        ),
      ])
      .args(&[
        Arg::with_name("debug").short("d").long("debug"),
        Arg::with_name("trace").short("t").long("trace"),
      ])
  }

  async fn run(mut server_type: Vec<RunType>) {
    #[allow(clippy::collection_is_never_read)]
    let mut threads = vec![];
    // Iterate over all of of the requested sub-servers and spawn one of each.
    while let Some(run_type) = server_type.last() {
      match run_type {
        RunType::Distributor => threads.push(whirl_server::make::distributor()),
        RunType::Hub => threads.push(whirl_server::make::hub()),
        RunType::Api => threads.push(whirl_api::make()),
      }
      server_type.pop();
    }

    if std::env::var("DISABLE_PROMPT").unwrap_or_else(|_| "false".to_string())
      == "true"
      || !Config::get().whirlsplash.prompt.enable
    {
      info!("starting with prompt disabled");
      loop {
        std::thread::sleep(std::time::Duration::default());
      }
    }

    whirl_prompt::Prompt::handle().await;
  }
}