Skip to content

Commit

Permalink
feat: introduce first super basic init command
Browse files Browse the repository at this point in the history
This adds a new `init` command to initialize a Vibranium project in either the
current directory or a given optional path location:

```
$ vibranium init --path ../../path/to/project
```

If the directories in `path` don't exist, they'll be created recursively.
This is a super basic implementation and doesn't account for property interaction
for cases like trying to initialize a Vibranium project within a Vibranium
 project. If the needed directories already exist, Vibranium will just move
on.
  • Loading branch information
0x-r4bbit committed Jan 30, 2019
1 parent 46f2fd2 commit 990e75d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
42 changes: 33 additions & 9 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
extern crate clap;
extern crate vibranium;

use std::io;
use std::process::exit;
use std::env;
use std::process;
use std::path::PathBuf;

use clap::{App, SubCommand, Arg};

use vibranium::Vibranium;
use vibranium::blockchain::NodeConfig;

const DEFAULT_NODE_CLIENT: &str = "parity";

type Error = Box<std::error::Error>;

fn main() {
if let Err(e) = run() {
eprintln!("{}", e);
process::exit(1);
}
}

fn run() -> Result<(), Error> {
let matches = App::new("Vibranium CLI")
.version(crate_version!())
.author(crate_authors!())
Expand All @@ -28,6 +40,15 @@ fn main() {
.help("Specifies node specific options that will be passed down to the client")
.multiple(true)
.raw(true))
)
.subcommand(SubCommand::with_name("init")
.about("Initializes a Vibranium project inside the current directory")
.arg(Arg::with_name("path")
.short("p")
.long("path")
.value_name("PATH")
.help("Specifies path to directory in which to initialize Vibranium project")
.takes_value(true))
).get_matches();

if let ("node", Some(cmd)) = matches.subcommand() {
Expand All @@ -45,12 +66,15 @@ fn main() {
client_options: &client_options,
};

if let Err(err) = vibranium.start_node(config) {
match err.kind() {
io::ErrorKind::NotFound => eprintln!("Couldn't find client: '{}', are you sure it's installed?", client),
_ => eprintln!("Something went wrong. {}", err)
}
exit(1);
}
vibranium.start_node(config)?;
}

if let ("init", Some(cmd)) = matches.subcommand() {
println!("Initializing empty Vibranium project...");
let vibranium = Vibranium::new();
let path = cmd.value_of("path").map(|p| Ok(PathBuf::from(p))).unwrap_or_else(|| env::current_dir())?;

vibranium.init_project(path).and_then(|_| Ok(println!("Done.")))?
}
Ok(())
}
23 changes: 23 additions & 0 deletions src/code_generator/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::path::{PathBuf};
use std::fs;

pub struct CodeGenerator;

const VIBRANIUM_PROJECT_DIRECTORY: &str = ".vibranium";
const DEFAULT_CONTRACTS_DIRECTORY: &str = "contracts";

impl CodeGenerator {
pub fn new() -> CodeGenerator {
CodeGenerator
}

pub fn generate_project(&self, project_path: PathBuf) -> Result<(), std::io::Error> {
for directory in vec![VIBRANIUM_PROJECT_DIRECTORY, DEFAULT_CONTRACTS_DIRECTORY] {
let path = project_path.join(directory);
if !path.exists() {
fs::create_dir_all(path)?;
}
}
Ok(())
}
}
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pub mod blockchain;
pub mod code_generator;

use std::io;
use std::process::ExitStatus;
use std::path::PathBuf;

#[derive(Debug)]
pub struct Vibranium;
Expand All @@ -11,10 +14,15 @@ impl Vibranium {
}
}

pub fn start_node(&self, config: blockchain::NodeConfig) -> Result<ExitStatus, std::io::Error> {
pub fn start_node(&self, config: blockchain::NodeConfig) -> Result<ExitStatus, io::Error> {
let node = blockchain::Node::new(config);
node.start()
.map(|mut process| process.wait())
.and_then(|status| status)
}

pub fn init_project(&self, path: PathBuf) -> Result<(), io::Error> {
let generator = code_generator::CodeGenerator::new();
generator.generate_project(path)
}
}

0 comments on commit 990e75d

Please sign in to comment.