Switch everything to StructOpt (#277)

* Add types for Rojo's subcommands

* Flesh out CLI types

* Port everything to structopt instead of clap
This commit is contained in:
Lucien Greathouse
2019-12-12 14:30:47 -08:00
committed by GitHub
parent 8b1e85fbb4
commit 47c7f63d75
9 changed files with 296 additions and 235 deletions

View File

@@ -1,27 +1,27 @@
use std::{
fs::File,
io::{self, BufWriter, Write},
path::PathBuf,
};
use failure::Fail;
use crate::{
cli::BuildCommand,
common_setup,
project::ProjectLoadError,
vfs::{FsError, RealFetcher, Vfs, WatchMode},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputKind {
enum OutputKind {
Rbxmx,
Rbxlx,
Rbxm,
Rbxl,
}
fn detect_output_kind(options: &BuildOptions) -> Option<OutputKind> {
let extension = options.output_file.extension()?.to_str()?;
fn detect_output_kind(options: &BuildCommand) -> Option<OutputKind> {
let extension = options.output.extension()?.to_str()?;
match extension {
"rbxlx" => Some(OutputKind::Rbxlx),
@@ -32,13 +32,6 @@ fn detect_output_kind(options: &BuildOptions) -> Option<OutputKind> {
}
}
#[derive(Debug)]
pub struct BuildOptions {
pub fuzzy_project_path: PathBuf,
pub output_file: PathBuf,
pub output_kind: Option<OutputKind>,
}
#[derive(Debug, Fail)]
pub enum BuildError {
#[fail(display = "Could not detect what kind of file to create")]
@@ -72,22 +65,19 @@ fn xml_encode_config() -> rbx_xml::EncodeOptions {
rbx_xml::EncodeOptions::new().property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown)
}
pub fn build(options: &BuildOptions) -> Result<(), BuildError> {
let output_kind = options
.output_kind
.or_else(|| detect_output_kind(options))
.ok_or(BuildError::UnknownOutputKind)?;
pub fn build(options: BuildCommand) -> Result<(), BuildError> {
let output_kind = detect_output_kind(&options).ok_or(BuildError::UnknownOutputKind)?;
log::debug!("Hoping to generate file of type {:?}", output_kind);
log::trace!("Constructing in-memory filesystem");
let vfs = Vfs::new(RealFetcher::new(WatchMode::Disabled));
let (_maybe_project, tree) = common_setup::start(&options.fuzzy_project_path, &vfs);
let (_maybe_project, tree) = common_setup::start(&options.project, &vfs);
let root_id = tree.get_root_id();
log::trace!("Opening output file for write");
let mut file = BufWriter::new(File::create(&options.output_file)?);
let mut file = BufWriter::new(File::create(&options.output)?);
match output_kind {
OutputKind::Rbxmx => {

View File

@@ -1,17 +1,12 @@
use std::path::PathBuf;
use failure::Fail;
use crate::project::{Project, ProjectInitError};
use crate::{
cli::{InitCommand, InitKind},
project::{Project, ProjectInitError},
};
#[derive(Debug, Fail)]
pub enum InitError {
#[fail(
display = "Invalid project kind '{}', valid kinds are 'place' and 'model'",
_0
)]
InvalidKind(String),
#[fail(display = "Project init error: {}", _0)]
ProjectInitError(#[fail(cause)] ProjectInitError),
}
@@ -20,23 +15,16 @@ impl_from!(InitError {
ProjectInitError => ProjectInitError,
});
#[derive(Debug)]
pub struct InitOptions<'a> {
pub fuzzy_project_path: PathBuf,
pub kind: Option<&'a str>,
}
pub fn init(options: &InitOptions) -> Result<(), InitError> {
pub fn init(options: InitCommand) -> Result<(), InitError> {
let (project_path, project_kind) = match options.kind {
Some("place") | None => {
let path = Project::init_place(&options.fuzzy_project_path)?;
InitKind::Place => {
let path = Project::init_place(&options.path)?;
(path, "place")
}
Some("model") => {
let path = Project::init_model(&options.fuzzy_project_path)?;
InitKind::Model => {
let path = Project::init_model(&options.path)?;
(path, "model")
}
Some(invalid) => return Err(InitError::InvalidKind(invalid.to_string())),
};
println!(

View File

@@ -1,6 +1,5 @@
use std::{
io::{self, Write},
path::PathBuf,
sync::Arc,
};
@@ -8,6 +7,7 @@ use failure::Fail;
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
use crate::{
cli::ServeCommand,
project::ProjectLoadError,
serve_session::ServeSession,
vfs::{RealFetcher, Vfs, WatchMode},
@@ -16,12 +16,6 @@ use crate::{
const DEFAULT_PORT: u16 = 34872;
#[derive(Debug)]
pub struct ServeOptions {
pub fuzzy_project_path: PathBuf,
pub port: Option<u16>,
}
#[derive(Debug, Fail)]
pub enum ServeError {
#[fail(display = "Couldn't load project: {}", _0)]
@@ -32,10 +26,10 @@ impl_from!(ServeError {
ProjectLoadError => ProjectLoad,
});
pub fn serve(options: &ServeOptions) -> Result<(), ServeError> {
pub fn serve(options: ServeCommand) -> Result<(), ServeError> {
let vfs = Vfs::new(RealFetcher::new(WatchMode::Enabled));
let session = Arc::new(ServeSession::new(vfs, &options.fuzzy_project_path));
let session = Arc::new(ServeSession::new(vfs, &options.project));
let port = options
.port

View File

@@ -1,10 +1,9 @@
use std::path::PathBuf;
use failure::Fail;
use reqwest::header::{ACCEPT, CONTENT_TYPE, COOKIE, USER_AGENT};
use crate::{
auth_cookie::get_auth_cookie,
cli::UploadCommand,
common_setup,
vfs::{RealFetcher, Vfs, WatchMode},
};
@@ -29,24 +28,16 @@ impl_from!(UploadError {
reqwest::Error => Http,
});
#[derive(Debug)]
pub struct UploadOptions<'a> {
pub fuzzy_project_path: PathBuf,
pub auth_cookie: Option<String>,
pub asset_id: u64,
pub kind: Option<&'a str>,
}
pub fn upload(options: UploadOptions) -> Result<(), UploadError> {
pub fn upload(options: UploadCommand) -> Result<(), UploadError> {
let cookie = options
.auth_cookie
.cookie
.or_else(get_auth_cookie)
.ok_or(UploadError::NeedAuthCookie)?;
log::trace!("Constructing in-memory filesystem");
let vfs = Vfs::new(RealFetcher::new(WatchMode::Disabled));
let (_maybe_project, tree) = common_setup::start(&options.fuzzy_project_path, &vfs);
let (_maybe_project, tree) = common_setup::start(&options.project, &vfs);
let root_id = tree.get_root_id();
let mut buffer = Vec::new();