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
use crate::{Command, DocumentCommand, FileFormat, Options, TransformCommand};
use atelier_lib::assembler::SearchPath;
use somedoc::write::OutputFormat;
use std::error::Error;
use std::path::PathBuf;
use structopt::StructOpt;

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

#[derive(Debug, StructOpt)]
#[structopt(name = "cargo-atelier", about = "Tools for the Smithy IDL.")]
struct CommandLine {
    /// The level of logging to perform; from off to trace
    #[structopt(long, short = "v", parse(from_occurrences))]
    verbose: i8,

    #[cfg(feature = "color")]
    /// Turn off color in the output
    #[structopt(long)]
    no_color: bool,

    #[structopt(subcommand)]
    cmd: SubCommand,
}

#[derive(Debug, StructOpt)]
struct FileInput {
    /// A file, or directory containing files, to read.
    #[structopt(long, short)]
    in_file: Vec<PathBuf>,

    /// If set, the standard SMITHY_PATH environment variable is used as a search path.
    #[structopt(long, short, conflicts_with = "search_env")]
    default_search_env: bool,

    /// The name of an environment variable to use as a search path.
    #[structopt(long, short, conflicts_with = "default_search_env")]
    search_env: Option<String>,
}

#[derive(Debug, StructOpt)]
enum SubCommand {
    /// Run standard linter rules on a model file
    Lint {
        #[structopt(flatten)]
        file_input: FileInput,
    },
    /// Run standard validators on a model file
    Validate {
        #[structopt(flatten)]
        file_input: FileInput,
    },
    /// Convert model from one representation to another
    Convert {
        #[structopt(flatten)]
        file_input: FileInput,

        /// The file to write to [default: <stdout>]
        #[structopt(long, short)]
        out_file: Option<PathBuf>,

        /// The representation of the output file
        #[structopt(short, long, default_value = "json")]
        write_format: FileFormat,

        /// The namespace to write, if a format is constrained to one
        #[structopt(short, long)]
        namespace: Option<String>,
    },
    /// Create human-readable documentation from a model
    Document {
        #[structopt(flatten)]
        file_input: FileInput,

        /// The file to write to [default: <stdout>]
        #[structopt(long, short)]
        out_file: Option<PathBuf>,

        /// The documentation format supported by the `somedoc` crate
        #[structopt(short, long, default_value = "markdown")]
        write_format: OutputFormat,
    },
}

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

pub fn parse() -> Result<Command, Box<dyn Error>> {
    let args = CommandLine::from_iter(check_command_line());

    let options = Options {
        use_color: !args.no_color,
    };

    match args.cmd {
        SubCommand::Lint {
            file_input:
                FileInput {
                    in_file,
                    default_search_env,
                    search_env,
                },
        } => Ok(Command::Lint(
            in_file,
            make_search_path(default_search_env, search_env),
            options,
        )),
        SubCommand::Validate {
            file_input:
                FileInput {
                    in_file,
                    default_search_env,
                    search_env,
                },
        } => Ok(Command::Validate(
            in_file,
            make_search_path(default_search_env, search_env),
            options,
        )),
        SubCommand::Convert {
            file_input:
                FileInput {
                    in_file,
                    default_search_env,
                    search_env,
                },
            out_file,
            write_format,
            namespace,
        } => Ok(Command::Convert(
            TransformCommand {
                input_files: in_file,
                output_file: out_file,
                output_format: write_format,
                namespace,
            },
            make_search_path(default_search_env, search_env),
            options,
        )),
        SubCommand::Document {
            file_input:
                FileInput {
                    in_file,
                    default_search_env,
                    search_env,
                },
            out_file,
            write_format,
        } => Ok(Command::Document(
            DocumentCommand {
                input_files: in_file,
                output_file: out_file,
                output_format: write_format,
            },
            make_search_path(default_search_env, search_env),
            options,
        )),
    }
}

// ------------------------------------------------------------------------------------------------
// Private Functions
// ------------------------------------------------------------------------------------------------

fn check_command_line() -> Vec<String> {
    use std::env;
    let mut args = env::args().collect::<Vec<String>>();

    if let Some(command) = args.get(1) {
        // The following is true if this is run as a cargo sub-command.
        if command == "atelier" {
            args.remove(1);
        }
    }
    args
}

fn make_search_path(default_search_env: bool, search_env: Option<String>) -> Option<SearchPath> {
    if default_search_env {
        Some(SearchPath::default())
    } else {
        search_env.map(|search_env| SearchPath::from_env(&search_env))
    }
}