Skip to content

Commit

Permalink
+ format check in pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiko Alexander Weber committed May 7, 2020
1 parent 0c86d0e commit d1508a2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 50 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Build
- name: Build program
run: cargo build
- name: Test
- name: Check formatting
run: cargo fmt --all -- --check
- name: Execute tests
run: cargo test
- name: Scan
- name: Scan code
run: |
rustup component add clippy &&
cargo clippy --all-targets --all-features -- -D warnings
Expand Down
18 changes: 9 additions & 9 deletions src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ pub struct PrintArguments {
pub enum ShellTrust {
None,
Prompt,
Ultimate
Ultimate,
}

pub struct ClapArgumentLoader {
}
pub struct ClapArgumentLoader {}

impl ClapArgumentLoader {
pub async fn load_from_cli() -> std::io::Result<Command> {
Expand Down Expand Up @@ -51,25 +50,26 @@ impl ClapArgumentLoader {
Some(x) => {
let config_file = x.value_of("config").unwrap().to_owned();
let config = std::fs::read_to_string(config_file)?;

let shell_trust = match x.value_of("shell-trust") {
Some(x) => match x {
"none" => ShellTrust::None,
"prompt" => ShellTrust::Prompt,
"ultimate" => ShellTrust::Ultimate,
_ => ShellTrust::None
_ => ShellTrust::None,
},
None => ShellTrust::None,
};

Ok(Command::Print(PrintArguments {
configuration: config,
shell_trust
shell_trust,
}))
}
None => {
Err(std::io::Error::new(std::io::ErrorKind::Other, "could not resolve subcommand", ))
}
None => Err(std::io::Error::new(
std::io::ErrorKind::Other,
"could not resolve subcommand",
)),
}
}
}
10 changes: 5 additions & 5 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ pub struct Config {
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all="snake_case")]
#[serde(rename_all = "snake_case")]
pub struct Template {
pub content: Content,
pub values: std::collections::BTreeMap<String, ValueDefinition>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all="snake_case")]
#[serde(rename_all = "snake_case")]
pub enum Content {
File(String),
Inline(String),
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all="snake_case")]
#[serde(rename_all = "snake_case")]
pub enum ValueDefinition {
Static(String),
Prompt(String),
Shell(String),
Select{text: String, options: Vec<String>},
Check{text: String, options: Vec<String>},
Select { text: String, options: Vec<String> },
Check { text: String, options: Vec<String> },
}

impl Config {
Expand Down
32 changes: 17 additions & 15 deletions src/execute/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::args::ShellTrust;
use crate::config::ValueDefinition;
use async_trait::async_trait;
use std::io::{Result, Error, ErrorKind};
use crate::args::{ShellTrust};
use crate::config::{ValueDefinition};
use std::io::{Error, ErrorKind, Result};

#[async_trait]
pub trait Execute {
Expand All @@ -15,8 +15,8 @@ impl Execute for ValueDefinition {
ValueDefinition::Static(v) => Ok(v.to_owned()),
ValueDefinition::Prompt(v) => prompt(v).await,
ValueDefinition::Shell(cmd) => shell(cmd, shell_trust).await,
ValueDefinition::Select{text, options} => select(text, options).await,
ValueDefinition::Check{text, options} => check(text, options).await,
ValueDefinition::Select { text, options } => select(text, options).await,
ValueDefinition::Check { text, options } => check(text, options).await,
}
}
}
Expand All @@ -30,26 +30,27 @@ async fn prompt(text: &str) -> Result<String> {

async fn shell(command: &str, shell_trust: &ShellTrust) -> Result<String> {
match shell_trust {
ShellTrust::None => {
return Err(Error::new(ErrorKind::Other, "no shell trust"))
},
ShellTrust::None => return Err(Error::new(ErrorKind::Other, "no shell trust")),
ShellTrust::Prompt => {
let exec = dialoguer::Confirmation::new()
.with_text(&format!("You are about to run a shell command. The command is:\n{}\nDo you confirm the execution?", command))
.interact()?;
if !exec {
return Err(Error::new(ErrorKind::Other, "user declined command execution"));
}
},
ShellTrust::Ultimate => {},
if !exec {
return Err(Error::new(
ErrorKind::Other,
"user declined command execution",
));
}
}
ShellTrust::Ultimate => {}
}

let output = std::process::Command::new("sh")
.arg("-c")
.arg(command)
.output()?;
if output.status.code().unwrap() != 0 {
return Err(Error::new(ErrorKind::Other, "failed to run command"))
return Err(Error::new(ErrorKind::Other, "failed to run command"));
}
Ok(String::from_utf8(output.stdout).unwrap())
}
Expand All @@ -67,7 +68,8 @@ async fn check(prompt: &str, options: &[String]) -> Result<String> {
let indices = dialoguer::Checkboxes::new()
.with_prompt(prompt)
.items(options)
.interact().unwrap();
.interact()
.unwrap();

match indices.len() {
0usize => Ok("".to_owned()),
Expand Down
45 changes: 27 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@ extern crate serde;
extern crate serde_json;
extern crate serde_yaml;

use futures::executor::block_on;
use std::collections::BTreeMap;
use std::io::{Result, Write};
use std::collections::{BTreeMap};
use futures::executor::{block_on};

pub mod args;
use args::ShellTrust;
pub mod config;
use config::{Config, Template, Content};
use config::{Config, Content, Template};
pub mod execute;
use execute::{Execute};
use execute::Execute;

async fn select_template(config: &Config) -> Result<&Template> {
let keys: Vec<String> = config.templates.keys().map(
|t| t.to_owned()
).collect();
let keys: Vec<String> = config.templates.keys().map(|t| t.to_owned()).collect();
let selection = dialoguer::Select::new()
.items(keys.as_slice())
.default(0)
.paged(false)
.interact()?;

match config.templates.get(&keys[selection]) {
Some(x) => Ok(x),
None => Err(std::io::Error::new(std::io::ErrorKind::Other, "failed")),
}
}

async fn get_values(template: &Template, shell_trust: &ShellTrust) -> Result<BTreeMap<String, String>> {
async fn get_values(
template: &Template,
shell_trust: &ShellTrust,
) -> Result<BTreeMap<String, String>> {
let mut values = BTreeMap::new();
for value in &template.values {
values.insert(value.0.to_owned(), value.1.execute(shell_trust).await?);
Expand All @@ -41,18 +42,27 @@ async fn get_values(template: &Template, shell_trust: &ShellTrust) -> Result<BTr
}

async fn replace(template: &str, values: &BTreeMap<String, String>) -> Result<String> {
fn recursive_add(namespace: &mut std::collections::VecDeque<String>, parent: &mut serde_json::Value, value: &str) {
fn recursive_add(
namespace: &mut std::collections::VecDeque<String>,
parent: &mut serde_json::Value,
value: &str,
) {
let current_namespace = namespace.pop_front().unwrap();
match namespace.len() {
0 => {
parent.as_object_mut().unwrap().entry(&current_namespace)
parent
.as_object_mut()
.unwrap()
.entry(&current_namespace)
.or_insert(serde_json::Value::String(value.to_owned()));
}
_ => {
let p = parent.as_object_mut().unwrap().entry(&current_namespace)
let p = parent
.as_object_mut()
.unwrap()
.entry(&current_namespace)
.or_insert(serde_json::Value::Object(serde_json::Map::new()));
recursive_add(
namespace, p, value);
recursive_add(namespace, p, value);
}
}
}
Expand Down Expand Up @@ -121,7 +131,8 @@ templates:
options:
- Default
- Security
"###.to_owned()
"###
.to_owned()
}

async fn async_main() -> Result<()> {
Expand All @@ -133,9 +144,7 @@ async fn async_main() -> Result<()> {
std::fs::write("./.complate/config.yml", default_config().await)?;
Ok(())
}
crate::args::Command::Print(x) => {
print(x).await
}
crate::args::Command::Print(x) => print(x).await,
}
}

Expand Down

0 comments on commit d1508a2

Please sign in to comment.