WIP started working on changing config in execution folder

This commit is contained in:
Thomas Eppers 2021-08-31 01:40:29 +02:00
parent 88cd2122e4
commit 73e57dc11d
3 changed files with 67 additions and 31 deletions

View File

@ -27,6 +27,16 @@ pub enum State {
SelectService,
}
impl State {
fn next(&self) -> State {
match self {
State::EditRepo => State::SelectTag,
State::SelectTag => State::SelectService,
State::SelectService => State::EditRepo,
}
}
}
impl Ui {
pub fn run(repo_id: &str) {
let mut ui = Ui {
@ -72,26 +82,23 @@ impl Ui {
//handle input
match receiver.try_recv() {
Ok(Key::Char('\t')) => {
ui.state = match ui.state {
State::EditRepo => State::SelectTag,
State::SelectTag => State::SelectService,
State::SelectService => State::EditRepo,
};
}
Ok(Key::Ctrl('q')) => break 'core, //quit program without saving
Ok(Key::Ctrl('s')) => {
if ui.state == State::SelectTag {
//TODO save currently selected tag
}
}
Ok(Key::Char('\n')) => {
if ui.state == State::EditRepo {
Ok(Key::Char('\t')) => ui.state = ui.state.next(),
Ok(Key::Ctrl('s')) => ui.services.save(),
Ok(Key::Char('\n')) => match ui.state {
State::EditRepo => {
ui.repo.confirm();
ui.tags =
tag_list::TagList::new_with_result(tags::Tags::get_tags(ui.repo.get()));
}
}
State::SelectTag => {
let mut repo = ui.services.extract_repo().unwrap();
let tag = ui.tags.get().unwrap();
repo.push_str(&tag);
ui.services.change_current_line(repo);
}
_ => (),
},
Ok(Key::Char(key)) => {
if ui.state == State::EditRepo {
ui.tags = tag_list::TagList::new_line("Editing Repository");
@ -109,7 +116,7 @@ impl Ui {
Ok(Key::Up) => {
if ui.state == State::SelectService && ui.services.find_previous_match() {
match ui.services.extract_repo() {
Err(_) => (), //TODO handle
Err(_) => ui.tags = tag_list::TagList::new_line("no image found"),
Ok(s) => ui.repo.set(s),
}
}
@ -119,7 +126,7 @@ impl Ui {
Ok(Key::Down) => {
if ui.state == State::SelectService && ui.services.find_next_match() {
match ui.services.extract_repo() {
Err(_) => (), //TODO handle
Err(_) => ui.tags = tag_list::TagList::new_line("no image found"),
Ok(s) => ui.repo.set(s),
}
}

View File

@ -1,13 +1,15 @@
// use std::fs::File;
// use std::io::BufWriter;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use regex::Regex;
use termion::event::Key;
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders, List, ListState};
use crate::ui::State;
#[derive(Debug)]
pub enum Error {
NoneSelected,
Parsing(String),
@ -21,18 +23,20 @@ pub struct ServiceSwitcher {
impl ServiceSwitcher {
pub fn new() -> Self {
let list: Vec<String> = vec![
String::from("dies"),
String::from("ist"),
String::from(" image: rocketchat/rocket.chat:latest"),
String::from("ein"),
String::from("test"),
String::from(" image: sdfsfdsf:latest"),
];
let list = match File::open("docker-compose.yml") {
Err(e) => vec![format!("no docker-compose.yml: {}", e)],
Ok(file) => {
let buf = BufReader::new(file);
buf.lines()
.map(|l| l.expect("Could not parse line"))
.collect()
}
};
Self {
list,
state: ListState::default(),
regex: Regex::new(r"^ *image *:.*").unwrap(),
regex: Regex::new(r"( *image *): *(.*):([.*]??) *").unwrap(),
}
}
@ -127,15 +131,33 @@ impl ServiceSwitcher {
false
}
//return the repository from currently selected row
pub fn extract_repo(&self) -> Result<String, Error> {
let regex = Regex::new(r"( *image *): *(.*[:.*]?) *").unwrap();
match self.state.selected() {
None => return Err(Error::NoneSelected),
Some(i) => {
let caps = regex.captures(&self.list[i]).unwrap();
let caps = match self.regex.captures(&self.list[i]) {
None => return Err(Error::Parsing(String::from("Nothing found"))),
Some(cap) => cap,
};
let result: String = caps.get(2).unwrap().as_str().to_string();
return Ok(result);
}
}
}
pub fn change_current_line(&mut self, repo_with_tag: String) {
match self.state.selected() {
None => (),
Some(i) => self.list[i] = repo_with_tag,
}
}
pub fn save(&self) {
let name = "docker-compose.yml";
let mut file = File::open(name).unwrap();
for line in &self.list {
file.write_all(line.as_bytes()).unwrap();
}
}
}

View File

@ -32,6 +32,13 @@ impl TagList {
}
}
pub fn get(&self) -> Option<String> {
match self.state.selected() {
None => None,
Some(i) => Some(self.list[i].clone()),
}
}
pub fn render(&mut self, state: &State) -> (List, &mut ListState) {
let border_style = if state == &State::SelectTag {
Style::default().fg(Color::Green)