Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add command to display config #27

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/action/default/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::config;
use crate::out;

pub fn config(config: &config::Config) {
let config_path = config::config_location();

out::info::display_config(config, &config_path.to_str().unwrap());
}
5 changes: 2 additions & 3 deletions src/action/default/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod config;
pub mod init;
pub mod list;
pub mod update;
pub mod view;
pub mod update;
8 changes: 8 additions & 0 deletions src/action/repository/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::config::Config;
use crate::out;

pub fn list(config: &Config) {
let repositories = config.get_repositories();

out::info::list_repositories(&repositories);
}
1 change: 1 addition & 0 deletions src/action/repository/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod add;
pub mod list;
pub mod remove;
File renamed without changes.
4 changes: 3 additions & 1 deletion src/action/template/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod create;
pub mod test;
pub mod list;
pub mod test;
pub mod view;
File renamed without changes.
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn get_default_config() -> Config {
return config;
}

fn config_location() -> PathBuf {
pub fn config_location() -> PathBuf {
let mut dir = match dirs::home_dir() {
Some(path) => PathBuf::from(path),
None => PathBuf::from(""),
Expand Down
6 changes: 3 additions & 3 deletions src/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn do_fetch<'a>(
let auth = opts.auth.clone().unwrap();

if auth == "ssh" {
log::debug!("[git]: authentication using ssh");
log::info!("[git]: authentication using ssh");
// callbacks.credentials(|_url, username_from_url, _allowed_types| {
// git2::Cred::ssh_key(
// username_from_url.unwrap(),
Expand All @@ -124,7 +124,7 @@ fn do_fetch<'a>(
git2::Cred::userpass_plaintext(&token, "")
});
} else if auth == "basic" {
log::debug!("[git]: authentication using token");
log::info!("[git]: authentication using token");
if opts.username.is_none() || opts.password.is_none() {
log::error!("Username or Password is missing");
return Err(git2::Error::from_str("missing credentials"));
Expand All @@ -133,7 +133,7 @@ fn do_fetch<'a>(
git2::Cred::userpass_plaintext(&opts.username.clone().unwrap(), &opts.password.clone().unwrap())
});
} else {
log::debug!("[git]: no authentication");
log::info!("[git]: no authentication");
}

// Always fetch all tags.
Expand Down
78 changes: 45 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ mod action;
mod cli;
mod config;
mod git;
mod out;
mod logger;
mod meta;
mod out;
mod repository;
mod template;
mod utils;
mod logger;

use clap::{crate_version, App, AppSettings, Arg};

Expand Down Expand Up @@ -70,7 +70,7 @@ fn main() {
.long("remote")
.takes_value(true)
.help("Remote URL")
.required(false)
.required(false),
)
.arg(
Arg::with_name("replace")
Expand All @@ -80,70 +80,82 @@ fn main() {
),
)
.subcommand(
App::new("list")
.about("List all available templates")
.visible_alias("ls"),
App::new("config")
.about("View configuration"),
)
.subcommand(App::new("update").about("Update to the latest release"))
.subcommand(
App::new("view")
.about("View template details")
.visible_alias("v")
.arg(
Arg::with_name("repository")
.short('r')
.long("repository")
.takes_value(true)
.help("The repository to use")
.required(false),
)
.arg(
Arg::with_name("template")
.help("The name of the template to use for generation")
.required(false)
.index(1),
),
)
.subcommand(
App::new("repository")
.about("Maintain repositories")
.setting(AppSettings::ArgRequiredElseHelp)
.subcommand(App::new("add").about("Add new repository"))
.subcommand(App::new("list").about("List all available repository"))
.subcommand(App::new("remove").about("Remove a repository")),
)
.subcommand(
App::new("template")
.about("Maintain templates")
.setting(AppSettings::ArgRequiredElseHelp)
.subcommand(App::new("create").about("Create new template"))
.subcommand(App::new("list").about("List all available templates"))
.subcommand(
App::new("view")
.about("View template details")
.arg(
Arg::with_name("repository")
.short('r')
.long("repository")
.takes_value(true)
.help("Name of the repository")
.required(false),
)
.arg(
Arg::with_name("template")
.short('t')
.long("template")
.takes_value(true)
.help("Name of the template")
.required(false)
),
),
)
.get_matches();

match matches.subcommand() {
("config", Some(_config_matches)) => {
action::default::config::config(&config);
}
("init", Some(init_matches)) => {
action::default::init::init(&config, init_matches);
}
("list", Some(list_matches)) => {
action::default::list::list(&config, list_matches);
}
("update", Some(_update_matches)) => {
action::default::update::update(true);
}
("view", Some(view_matches)) => {
action::default::view::view(&config, view_matches);
}
("repository", Some(repository_matches)) => {
match repository_matches.subcommand() {
("add", Some(repo_add_matches)) => action::repository::add::add(&mut config, repo_add_matches),
("add", Some(repo_add_matches)) => {
action::repository::add::add(&mut config, repo_add_matches)
}
("remove", Some(repo_delete_matches)) => {
action::repository::remove::remove(&mut config, repo_delete_matches)
}
("list", Some(_list_matches)) => {
action::repository::list::list(&config);
}
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachabe!()
}
}
("template", Some(repository_matches)) => {
match repository_matches.subcommand() {
("create", Some(template_create_matches)) => action::template::create::create(&mut config, template_create_matches),
("create", Some(template_create_matches)) => {
action::template::create::create(&mut config, template_create_matches)
}
("view", Some(view_matches)) => {
action::template::view::view(&config, view_matches);
}
("list", Some(list_matches)) => {
action::template::list::list(&config, list_matches);
}
_ => unreachable!(), // If all subcommands are defined above, anything else is unreachabe!()
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/out/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ pub fn copy_template() {
println!("{}", text);
}

pub fn missing_token() {
let text = format!("Error fetching templates: authentication token is missing").red();
println!("{}", text);
}

pub fn unknown() {
let text = format!("Unknown error. See the logs for more details").red();
println!("{}", text);
Expand Down
12 changes: 12 additions & 0 deletions src/out/info.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::Config;
use crate::template;
use crate::utils;

Expand All @@ -14,6 +15,12 @@ pub fn list_templates(templates: &Vec<String>) {
}
}

pub fn list_repositories(repositories: &Vec<String>) {
for repository in repositories {
println!("{}", &utils::capitalize(repository));
}
}

pub fn display_template(template: &template::Template) {
println!("name: {}", template.name);
println!("path: {}", template.path);
Expand All @@ -28,6 +35,11 @@ pub fn display_template(template: &template::Template) {
}
}

pub fn display_config(config: &Config, config_path: &str) {
let text = format!("Config loaded from: {}", config_path).green();
println!("{}", text);
}

pub fn no_app_update() {
let text = format!("tmpo is already up to date").green();
println!("{}", text);
Expand Down
1 change: 1 addition & 0 deletions src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl Repository {
git::GitError::AddRemoteError => println!("Add Remote Error"),
},
};

match git::update(&directory, &self.config.git_options) {
Ok(()) => (),
Err(_e) => out::error::update_templates(),
Expand Down
13 changes: 12 additions & 1 deletion src/template/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs;
use std::fs::File;
use std::io::{Error, ErrorKind, Read, Write};
use log;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

Expand Down Expand Up @@ -234,6 +235,8 @@ fn run_script(script: &String, target: &Path) {
return;
}

log::info!("Run script: {}", script);

// Run before script if exists
let mut cmd = if cfg!(target_os = "windows") {
Command::new("cmd")
Expand All @@ -254,7 +257,15 @@ fn run_script(script: &String, target: &Path) {
.expect("failed to execute process")
};

let status = cmd.wait();
let status = match cmd.wait() {
Ok(status) => status,
Err(error) => {
log::error!("Script exited with error: {}", error);
return;
},
};

log::info!("Script exit status: {}", status);
}