Skip to content

Commit

Permalink
refactor create command into a subcommand, to allow for other types o…
Browse files Browse the repository at this point in the history
…f creations
  • Loading branch information
yeastplume committed Feb 21, 2020
1 parent 818218c commit 0a9a032
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/cmd/create/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2020 Revcore Technologies Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::Error;
use proj::{AloeVeraProject, Jsonable};

/// Arguments for the initial create project command
pub struct CreateProjectArgs {
pub id: Option<String>,
pub output_file: String,
}

pub fn create_project(args: &CreateProjectArgs) -> Result<(), Error> {
let id = match &args.id {
Some(l) => l,
None => "My Project",
};
info!("Creating new project file at: {}", args.output_file);

let proj = AloeVeraProject::new(id);
let json = proj.to_json()?;
crate::cmd::common::output_to_file(&args.output_file, &json.as_bytes())?;

Ok(())
}

/// Arguments for the initial create project command
pub struct CreateSDImageArgs {
pub output_file: String,
}

pub fn create_sd_image(args: &CreateSDImageArgs) -> Result<(), Error> {
info!(
"Creating new SDCard (FAT32 FS) image at: {}",
args.output_file
);

/*let proj = AloeVeraProject::new(id);
let json = proj.to_json()?;*/
//crate::cmd::common::output_to_file(&args.output_file, &json.as_bytes())?;

Ok(())
}
16 changes: 16 additions & 0 deletions src/cmd/create/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2020 Revcore Technologies Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod command;
pub mod parse;
49 changes: 49 additions & 0 deletions src/cmd/create/parse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 Revcore Technologies Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use super::command::{self, CreateProjectArgs, CreateSDImageArgs};
use crate::cmd::common;
use crate::{Error, ErrorKind};
use clap::ArgMatches;

pub fn parse_create_project_args(args: &ArgMatches) -> Result<CreateProjectArgs, Error> {
let output_file = common::parse_required(args, "output_file")?;
Ok(CreateProjectArgs {
id: args.value_of("id").map(|s| s.into()),
output_file: output_file.into(),
})
}

pub fn parse_create_sd_image_args(args: &ArgMatches) -> Result<CreateSDImageArgs, Error> {
let output_file = common::parse_required(args, "output_file")?;
Ok(CreateSDImageArgs {
output_file: output_file.into(),
})
}

pub fn execute_create_command(args: &ArgMatches) -> Result<(), Error> {
match args.subcommand() {
("project", Some(args)) => {
let a = arg_parse!(parse_create_project_args(args));
command::create_project(&a)
}
("sdimage", Some(args)) => {
let a = arg_parse!(parse_create_sd_image_args(args));
command::create_sd_image(&a)
}
_ => {
let msg = format!("Unknown sub command, use 'aloevera create --help' for details");
return Err(ErrorKind::ArgumentError(msg).into());
}
}
}
16 changes: 16 additions & 0 deletions util/src/fat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2020 Revcore Technologies Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Utilities to write to FAT Filesystem images, for eventual
//! transfer to SD Cards

0 comments on commit 0a9a032

Please sign in to comment.