aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: b25df83042e9dcaba9f2b3742fd39355a8d50c9a (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
// Copyright (C) 2021-2021 Fuwn
// SPDX-License-Identifier: GPL-3.0-only

use std::str::FromStr;

use structopt::{
  clap,
  clap::{App, Arg, SubCommand},
};

use crate::nitrous::Nitrous;

#[derive(PartialEq)]
pub enum ProxyType {
  Http,
  Socks4,
  Socks5,
  Tor,
}
impl FromStr for ProxyType {
  type Err = &'static str;

  fn from_str(s: &str) -> Result<Self, Self::Err> {
    match s {
      "http" => Ok(Self::Http),
      "socks4" => Ok(Self::Socks4),
      "socks5" => Ok(Self::Socks5),
      "tor" => Ok(Self::Tor),
      _ => Err("no match"),
    }
  }
}

pub struct Cli;
impl Cli {
  pub async fn execute() {
    let matches = Self::cli().get_matches();

    let debug = matches.is_present("debug");

    match matches.subcommand() {
      ("generate", _) =>
        Nitrous::generate(
          matches
            .subcommand_matches("generate")
            .unwrap()
            .value_of("amount")
            .unwrap()
            .to_string()
            .parse::<usize>()
            .unwrap(),
          debug,
        ),
      ("check", _) =>
        Nitrous::check(
          {
            let argument = matches
              .subcommand_matches("check")
              .unwrap()
              .value_of("file");
            if argument.is_some() {
              argument.unwrap()
            } else if std::fs::File::open(".nitrous/codes.txt").is_err() {
              panic!("cannot open nitrous generated codes.txt");
            } else {
              ".nitrous/codes.txt"
            }
          },
          debug,
          ProxyType::from_str(
            matches
              .subcommand_matches("check")
              .unwrap()
              .value_of("proxy_type")
              .unwrap(),
          )
          .unwrap(),
          matches
            .subcommand_matches("check")
            .unwrap()
            .value_of("proxy_list")
            .unwrap_or("null"),
        )
        .await,
      ("clean", _) =>
        for dir in &[".nitrous/check/", ".nitrous/"] {
          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"))
      .setting(clap::AppSettings::SubcommandRequiredElseHelp)
      .subcommands(vec![
        SubCommand::with_name("generate")
          .alias("gen")
          .about("Generate a specified number Discord Nitro codes")
          .arg(
            Arg::with_name("amount")
              .required(true)
              .index(1)
              .takes_value(true),
          ),
        SubCommand::with_name("check")
          .about("Check a file of Discord Nitro codes for valid/ invalid codes")
          .long_about(
            "Check a file of Discord Nitro codes for valid/ invalid codes.\n\nIf a codes file is \
             not explicitly specified, the check routine will run on a default file value of \
             `./.nitrous/codes.txt`. If you would like to override this behaviour, specify your \
             file after the subcommand.",
          )
          .arg(
            Arg::with_name("file")
              .required(false)
              .takes_value(true)
              .long("file")
              .short("f"),
          )
          .args(&[
            Arg::with_name("proxy_type")
              .required(true)
              .takes_value(true)
              .index(1)
              .possible_values(&["http", "socks4", "socks5", "tor"]),
            Arg::with_name("proxy_list")
              .required_ifs(&[
                ("proxy_type", "http"),
                ("proxy_type", "socks4"),
                ("proxy_type", "socks5"),
              ])
              .takes_value(true)
              .index(2),
          ]),
        SubCommand::with_name("clean")
          .about("Delete Nitrous-generated files/ directories which are NOT critical."),
      ])
      .arg(
        Arg::with_name("debug")
          .long("debug")
          .short("d")
          .takes_value(false)
          .multiple(false)
          .global(true),
      )
  }
}