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
210
211
212
213
214
215
216
217
218
219
220
221
222
//! Command line modules: has one function which takes input parameters from commandline
//! and parses them.
use adbfbinlib::common::config;

static APP_TITLE: &str = concat!("The audiobook finder (", env!("CARGO_PKG_NAME"), ")");

static ARG_NET: &str = "net";
static ARG_TUI: &str = "tui";
static ARG_WEBUI: &str = "webui";
static ARG_KEEP_ALIVE: &str = "keep";
static ARG_BROWSER: &str = "browser";
static ARG_BROWSER_PORT: &str = "port";

static INPUT_FOLDERS: &str = "folders";

const AUTHORS: &'static str = env!("CARGO_PKG_AUTHORS");
const DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");

/// Get all start values which are passed from command line
///
/// Get all start values and returns the following tuple
/// ui_paths,
/// has_tui,
/// has_webui,
/// has_net,
/// keep_alive,
/// open_browser,
/// web_port,
/// has_ui,
pub fn get_start_values() -> (Vec<String>, bool, bool, bool, bool, bool, u16, bool) {
    let parse_args = clap::App::new(APP_TITLE)
        .version(config::net::VERSION)
        .author(AUTHORS)
        .about(DESCRIPTION)
        .long_about::<&str>(
            &[
                &DESCRIPTION,
                "\n\
                 It reads data from possibly multiple given path(s). Via local network it searches \
                 for other instances of the program, and will later exchange data securely.\n\
                 All information gathered will be used to find duplicates, versions of \
                 different quality, different tags for same content (spelling, \
                 incompleteness).\n\
                 For documentation see: ",
                &config::net::HOMEPAGE,
                "\n \
                 A program to learn, embrace, and love Rust! \n\
                 Have fun!",
            ]
            .concat(),
        )
        .arg(
            clap::Arg::with_name("config")
                .short("c")
                .long("config")
                .value_name("FILE")
                .help("Sets custom config file (not implemented yet)")
                .takes_value(true),
        )
        .arg(
            clap::Arg::with_name(ARG_TUI)
                .short("t")
                .long(ARG_TUI)
                .help("Run with TUI")
                .takes_value(false),
        )
        .arg(
            clap::Arg::with_name(ARG_WEBUI)
                .short("w")
                .long(ARG_WEBUI)
                .help("Run with-in a webui.")
                .takes_value(false),
        )
        .arg(
            clap::Arg::with_name(ARG_BROWSER_PORT)
                .short("p")
                .long(ARG_BROWSER_PORT)
                .help(
                    &vec![
                        "Define port for webui (only works with webui).\nDefault port is:"
                            .to_string(),
                        config::net::WEB_PORT_DEFAULT.to_string(),
                        " but please choose from 8080 to 8099)".to_string(),
                    ]
                    .join(""),
                )
                .takes_value(true),
        )
        .arg(
            clap::Arg::with_name(ARG_NET)
                .short("n")
                .long(ARG_NET)
                .help("With net search for other audiobookfinders running in local network.")
                .takes_value(false),
        )
        .arg(
            clap::Arg::with_name(ARG_KEEP_ALIVE)
                .short("k")
                .long(ARG_KEEP_ALIVE)
                .help(
                    "With keep alive process will continue even after search has been performed.\
                     This should be used and will be turned on automatically with net, webui\
                     browser option.",
                )
                .takes_value(false),
        )
        .arg(
            clap::Arg::with_name(ARG_BROWSER)
                .short("b")
                .long(ARG_BROWSER)
                .help("Shall browser not be openend automatically (only works with webui).")
                .takes_value(false),
        )
        .arg(
            clap::Arg::with_name(INPUT_FOLDERS)
                .help(
                    &[
                        "Sets multiple input folder(s) to be searched for audio files. (Max ",
                        &config::data::PATHS_MAX.to_string(),
                        " input folders will be used!)",
                    ]
                    .concat(),
                )
                .multiple(true)
                .required(false),
        )
        .get_matches();
    // tricky thing, but I really like that
    let all_pathes = if let Some(correct_input) = parse_args.values_of(INPUT_FOLDERS) {
        correct_input.collect()
    } else {
        vec!["."]
    };

    //
    // check argments if tui and net search is needed
    //
    let has_arg = |x: &str| parse_args.is_present(x);

    let has_tui = has_arg(ARG_TUI);
    let has_webui = has_arg(ARG_WEBUI);
    let has_net = has_arg(ARG_NET);
    let has_port = has_arg(ARG_BROWSER_PORT);
    let mut keep_alive = has_arg(ARG_KEEP_ALIVE);
    let open_browser = !has_arg(ARG_BROWSER);

    //
    // section for better user experience
    // todo: think it over
    if has_webui || has_net {
        keep_alive = true;
    }
    // not mutable
    let keep_alive = keep_alive;

    let web_port = {
        let web_default_string = config::net::WEB_PORT_DEFAULT.to_string();
        // when to write to console
        let has_to_write_console = !has_tui && has_webui;

        let parsed_value = parse_args
            .value_of(ARG_BROWSER_PORT)
            .unwrap_or_else(|| {
                if has_to_write_console && has_port {
                    println!(
                        "Port argument was bad, using default port {}!",
                        config::net::WEB_ADDR
                    );
                }
                &web_default_string
            })
            .parse::<u16>()
            .unwrap_or_else(|_| {
                if has_to_write_console && has_port {
                    println!(
                        "Invalid port input, using default port {}!",
                        &web_default_string
                    );
                }
                config::net::WEB_PORT_DEFAULT
            });
        if parsed_value < config::net::WEB_PORT_MIN || parsed_value > config::net::WEB_PORT_MAX {
            if has_to_write_console {
                let web_max_string = config::net::WEB_PORT_MAX.to_string();
                let web_min_string = config::net::WEB_PORT_MIN.to_string();
                println!(
                    "Port not in range {} .. {}, using default {}!",
                    &web_min_string, &web_max_string, &web_default_string
                );
            }
            config::net::WEB_PORT_DEFAULT
        } else {
            parsed_value
        }
    };

    // extended help for certain option combinations
    if !has_tui && has_webui && !open_browser {
        println!(
            "Open http://{}:{} to start!",
            config::net::WEB_ADDR,
            web_port
        );
        println!("The webui needs to get the start signal from there");
    }

    // either one will have a ui, representing data and error messages
    let has_ui = has_tui || has_webui;

    // 1) convert to strings
    let unchecked_strings = all_pathes.iter().map(|s| s.to_string()).collect();
    (
        unchecked_strings,
        has_tui,
        has_webui,
        has_net,
        keep_alive,
        open_browser,
        web_port,
        has_ui,
    )
}