Skip to content

Commit

Permalink
Merge pull request #134 from perryrh0dan/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
perryrh0dan authored Apr 30, 2021
2 parents 6ff7a10 + 37f7a10 commit d1d3aff
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 33 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tmpo"
description = "Command line utility to create new workspaces based on predefined templates"
version = "2.4.1"
version = "2.5.1"
authors = ["Thomas Pöhlmann <thomaspoehlmann96@googlemail.com>"]
edition = "2018"
license = "MIT"
Expand All @@ -13,7 +13,6 @@ serde_json = "1.0.62"
serde_yaml = "0.8.17"
dirs = "3.0.1"
log = "0.4.14"
log4rs = "1.0.0"
git2 = "0.13"
colored = "2.0.0"
dialoguer = "0.7.1"
Expand All @@ -27,6 +26,10 @@ base64 = "0.13.0"
semver = "0.11.0"
convert_case = "0.4.0"
linked_hash_set = "0.1.4"
chrono = "0.4.13"

[dependencies.log4rs]
version = "1.0.0"

[dependencies.reqwest]
version = "0.11.0"
Expand Down
13 changes: 13 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# What's new in Tmpo

### 2.5.1

### Fix

- Setup timebased rolling log files

## 2.5.0

### Feature

- Add template note. `note` property is printed after initialization of a template
- Make all repository add inputs available through args

## 2.4.1

### Fix
Expand Down
16 changes: 8 additions & 8 deletions media/logo_v3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 18 additions & 1 deletion src/action/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl Action {
let copy_options = CopyOptions {
template_name: template_name.to_owned(),
target: tmp_workspace_path.to_owned(),
render_context: render_context,
render_context: render_context.to_owned(),
};

// Copy the template
Expand Down Expand Up @@ -325,6 +325,23 @@ impl Action {
}
};

// Print success message
out::success::workspace_created(&workspace_name);

// Get Template info
let template_info = match repository.get_template_info(&template_name) {
Ok(info) => info,
Err(error) => {
log::error!("{}", error);
eprintln!("{}", error);
exit(1);
}
};

// Print template info
if template_info.is_some() {
let info = renderer::render(&template_info.unwrap(), &render_context);
out::success::workspace_info(&info);
}
}
}
55 changes: 37 additions & 18 deletions src/action/repository/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ impl Action {
let repository_authentication = args.value_of("authentication");
let repository_url = args.value_of("url");
let repository_branch = args.value_of("branch");
let username = args.value_of("username");
let password = args.value_of("password");
let token = args.value_of("token");

let mut git_options = git::Options::new();

Expand Down Expand Up @@ -125,24 +128,35 @@ impl Action {
// Get credentials for different auth types
match git_options.auth.clone().unwrap() {
git::AuthType::BASIC => {
git_options.username = match input::text("Enter your git username", false) {
Ok(value) => Some(value),
Err(error) => {
log::error!("{}", error);
eprintln!("{}", error);
exit(1);
// Get username
git_options.username = if username.is_none() {
match input::text("Enter your git username", false) {
Ok(value) => Some(value),
Err(error) => {
log::error!("{}", error);
eprintln!("{}", error);
exit(1);
}
}
} else {
Some(String::from(username.unwrap()))
};
git_options.password = match input::password("Enter your git password") {
Ok(value) => Some(value),
Err(error) => {
log::error!("{}", error);
eprintln!("{}", error);
exit(1);
// Get password
git_options.password = if password.is_none() {
match input::password("Enter your git password") {
Ok(value) => Some(value),
Err(error) => {
log::error!("{}", error);
eprintln!("{}", error);
exit(1);
}
}
} else {
Some(String::from(password.unwrap()))
}
}
git::AuthType::SSH => {
// Get ssh private key
git_options.token = match input::text("Enter your git username", false) {
Ok(value) => Some(value),
Err(error) => {
Expand All @@ -153,13 +167,18 @@ impl Action {
}
}
git::AuthType::TOKEN => {
git_options.token = match input::text("Enter your access token", false) {
Ok(value) => Some(value),
Err(error) => {
log::error!("{}", error);
eprintln!("{}", error);
exit(1);
// Get token
git_options.token = if token.is_none() {
match input::text("Enter your access token", false) {
Ok(value) => Some(value),
Err(error) => {
log::error!("{}", error);
eprintln!("{}", error);
exit(1);
}
}
} else {
Some(String::from(token.unwrap()))
}
}
git::AuthType::NONE => {
Expand Down
9 changes: 8 additions & 1 deletion src/logger/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::Local;
use log::LevelFilter;
use log4rs::{
append::{
Expand All @@ -21,7 +22,7 @@ pub fn init() {
.build();

// Logging to log file
let logfile_path = config::directory().join("log/output.log");
let logfile_path = config::directory().join(get_log_file_name());

let logfile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new(
Expand All @@ -47,3 +48,9 @@ pub fn init() {

log4rs::init_config(config).unwrap();
}

fn get_log_file_name() -> String {
let local = Local::now();
let timestamp = local.format("%Y-%m-%d").to_string();
return String::from("log/") + &timestamp + ".log";
}
21 changes: 21 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,27 @@ fn main() {
.takes_value(true)
.about("Remote repository branch")
.required(false),
)
.arg(
Arg::new("username")
.long("username")
.takes_value(true)
.about("Username for authentication")
.required(false),
)
.arg(
Arg::new("password")
.long("password")
.takes_value(true)
.about("Password for basic authentication")
.required(false),
)
.arg(
Arg::new("token")
.long("token")
.takes_value(true)
.about("Token for authentication")
.required(false),
),
)
.subcommand(
Expand Down
2 changes: 2 additions & 0 deletions src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct TemplateMeta {
pub extend: Option<Vec<String>>,
pub exclude: Option<Vec<String>>,
pub renderer: Option<Renderer>,
pub info: Option<String>,
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -157,6 +158,7 @@ impl TemplateMeta {
exclude: None,
values: None,
}),
info: None,
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/out/success.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub fn workspace_created(name: &str) {
println!("{}", text);
}

pub fn workspace_info(info: &str) {
println!("");
println!("{}", info)
}

pub fn template_created(path: &str) {
let text = format!("Created template: {}", path).green();
println!("{}", text);
Expand Down
24 changes: 23 additions & 1 deletion src/repository/custom_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::config::{Config, RepositoryOptions};
use crate::context::Context;
use crate::error::RunError;
use crate::git;
use crate::meta::{self, TemplateMeta, RepositoryMeta, Value};
use crate::meta::{self, RepositoryMeta, TemplateMeta, Value};
use crate::repository::{CopyOptions, Repository};
use crate::template;
use crate::template::Template;
Expand Down Expand Up @@ -48,6 +48,28 @@ impl Repository for CustomRepository {
Ok(())
}

fn get_template_info(&self, template_name: &str) -> Result<Option<String>, RunError> {
let template = self.get_template_by_name(template_name)?;

if template.meta.info.is_some() {
return Ok(template.meta.info.to_owned());
}

// Get list of all super templates
let super_templates = match self.get_super_templates(template, &mut HashSet::new()) {
Ok(templates) => templates,
Err(error) => return Err(error),
};

for template in super_templates.iter().rev() {
if template.meta.info.is_some() {
return Ok(template.meta.info.to_owned());
}
}

Ok(None)
}

fn get_template_values(&self, template_name: &str) -> Result<LinkedHashSet<Value>, RunError> {
let template = self.get_template_by_name(&template_name)?;

Expand Down
8 changes: 7 additions & 1 deletion src/repository/default_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ impl Repository for DefaultRepository {
Ok(())
}

fn get_template_info(&self, template_name: &str) -> Result<Option<String>, RunError> {
let template = self.get_template_by_name(template_name)?;

Ok(template.meta.info.to_owned())
}

fn get_template_values(&self, template_name: &str) -> Result<LinkedHashSet<Value>, RunError> {
let template = self.get_template_by_name(&template_name)?;
let template = self.get_template_by_name(template_name)?;

let mut values = LinkedHashSet::new();
values.extend(template.meta.get_values());
Expand Down
1 change: 1 addition & 0 deletions src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod default_repository;
pub trait Repository {
fn get_config(&self) -> RepositoryOptions;
fn copy_template(&self, ctx: &Context, opts: &CopyOptions) -> Result<(), RunError>;
fn get_template_info(&self, template_name: &str) -> Result<Option<String>, RunError>;
fn get_template_values(&self, template_name: &str) -> Result<LinkedHashSet<Value>, RunError>;
fn get_template_names(&self) -> Vec<String>;
fn get_template_by_name(&self, name: &str) -> Result<&template::Template, RunError>;
Expand Down

0 comments on commit d1d3aff

Please sign in to comment.