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

View File

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