1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2024 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use std::io;
19
use std::io::IsTerminal;
20
use std::path::{Path, PathBuf};
21

            
22
use clap::ArgMatches;
23
use hurl_core::input::Input;
24

            
25
use super::OptionsError;
26
use crate::cli::options::{InputFormat, OutputFormat};
27

            
28
189
pub fn check(arg_matches: &ArgMatches) -> bool {
29
189
    has_flag(arg_matches, "check")
30
}
31

            
32
189
pub fn color(arg_matches: &ArgMatches) -> bool {
33
189
    if has_flag(arg_matches, "color") {
34
6
        true
35
183
    } else if has_flag(arg_matches, "no_color") || has_flag(arg_matches, "in_place") {
36
3
        false
37
    } else {
38
180
        io::stdout().is_terminal()
39
    }
40
}
41

            
42
189
pub fn input_format(arg_matches: &ArgMatches) -> Result<InputFormat, OptionsError> {
43
189
    match get_string(arg_matches, "input_format").unwrap().as_str() {
44
189
        "hurl" => Ok(InputFormat::Hurl),
45
3
        "curl" => Ok(InputFormat::Curl),
46
        v => Err(OptionsError::Error(format!("Invalid input format {v}"))),
47
    }
48
}
49

            
50
189
pub fn output_format(arg_matches: &ArgMatches) -> Result<OutputFormat, OptionsError> {
51
189
    match get_string(arg_matches, "output_format").unwrap().as_str() {
52
189
        "hurl" => Ok(OutputFormat::Hurl),
53
105
        "json" => Ok(OutputFormat::Json),
54
54
        "html" => Ok(OutputFormat::Html),
55
        v => Err(OptionsError::Error(format!("Invalid output format {v}"))),
56
    }
57
}
58

            
59
189
pub fn in_place(arg_matches: &ArgMatches) -> Result<bool, OptionsError> {
60
189
    if has_flag(arg_matches, "in_place") {
61
3
        if get_string(arg_matches, "input_format") != Some("hurl".to_string()) {
62
            Err(OptionsError::Error(
63
                "You can use --in-place only hurl format!".to_string(),
64
            ))
65
3
        } else if get_string(arg_matches, "input_files").is_none() {
66
            Err(OptionsError::Error(
67
                "You can not use --in-place with standard input stream!".to_string(),
68
            ))
69
        } else {
70
3
            Ok(true)
71
        }
72
    } else {
73
186
        Ok(false)
74
    }
75
}
76

            
77
/// Returns the input files from the positional arguments and input stream
78
189
pub fn input_files(arg_matches: &ArgMatches) -> Result<Vec<Input>, OptionsError> {
79
189
    let mut files = vec![];
80
189
    if let Some(filenames) = get_strings(arg_matches, "input_files") {
81
375
        for filename in &filenames {
82
189
            let filename = Path::new(filename);
83
189
            if !filename.exists() {
84
                return Err(OptionsError::Error(format!(
85
                    "error: Cannot access '{}': No such file or directory",
86
                    filename.display()
87
                )));
88
            }
89
189
            let file = Input::from(filename);
90
189
            files.push(file);
91
        }
92
    }
93
189
    if files.is_empty() && !io::stdin().is_terminal() {
94
3
        let input = match Input::from_stdin() {
95
3
            Ok(input) => input,
96
            Err(err) => return Err(OptionsError::Error(err.to_string())),
97
        };
98
3
        files.push(input);
99
    }
100
189
    Ok(files)
101
}
102

            
103
189
pub fn output_file(arg_matches: &ArgMatches) -> Option<PathBuf> {
104
189
    get_string(arg_matches, "output").map(|s| Path::new(&s).to_path_buf())
105
}
106

            
107
189
pub fn standalone(arg_matches: &ArgMatches) -> Result<bool, OptionsError> {
108
189
    if has_flag(arg_matches, "standalone") {
109
3
        if get_string(arg_matches, "output_format") != Some("html".to_string()) {
110
            Err(OptionsError::Error(
111
                "use --standalone option only with html output".to_string(),
112
            ))
113
        } else {
114
3
            Ok(true)
115
        }
116
    } else {
117
186
        Ok(false)
118
    }
119
}
120

            
121
1122
fn has_flag(matches: &ArgMatches, name: &str) -> bool {
122
1122
    matches.get_one::<bool>(name) == Some(&true)
123
}
124

            
125
576
pub fn get_string(matches: &ArgMatches, name: &str) -> Option<String> {
126
705
    matches.get_one::<String>(name).map(|x| x.to_string())
127
}
128

            
129
/// Returns an optional list of `String` from the command line `matches` given the option `name`.
130
189
pub fn get_strings(matches: &ArgMatches, name: &str) -> Option<Vec<String>> {
131
189
    matches
132
189
        .get_many::<String>(name)
133
252
        .map(|v| v.map(|x| x.to_string()).collect())
134
}