switched out regex in ServiceSwitcher to repo functions

This commit is contained in:
Thomas Eppers 2021-09-13 15:54:37 +02:00
parent fadbe48b05
commit 48c45a1372
4 changed files with 48 additions and 26 deletions

1
Cargo.lock generated
View File

@ -541,6 +541,7 @@ name = "query-docker-tags"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"lazy_static",
"regex", "regex",
"reqwest", "reqwest",
"serde", "serde",

View File

@ -14,6 +14,7 @@ chrono = "0.4.19"
tui = "0.16" tui = "0.16"
termion = "1.5" termion = "1.5"
regex = "1.5.4" regex = "1.5.4"
lazy_static = "1.4.0"
[profile.release] [profile.release]
lto = "yes" lto = "yes"

View File

@ -16,9 +16,18 @@ pub enum Repo {
Project(String), Project(String),
} }
// pub fn extract_yaml(repo: &str) -> (String, String, String, String) { /// check if yaml line matches and returns the split of repo string and rest
// let regex = regex::Regex::new(r"( *image *:) *([^/])??/([^/:]):?(.*)?"); pub fn match_yaml_image(input: &str) -> Option<(&str, &str)> {
// } lazy_static::lazy_static! {
static ref REGEX: regex::Regex = regex::Regex::new(r"^( +image *: *)([a-z0-9\./:]+)").unwrap();
}
let caps = match REGEX.captures(input) {
Some(caps) => caps,
None => return None,
};
Some((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()))
}
pub fn split_repo(repo: &str) -> Result<Repo, Error> { pub fn split_repo(repo: &str) -> Result<Repo, Error> {
let split_tag: Vec<&str> = repo.split(":").collect(); let split_tag: Vec<&str> = repo.split(":").collect();
@ -140,4 +149,27 @@ mod tests {
)) ))
); );
} }
#[test]
fn test_match_yaml_image() {
use crate::repo::match_yaml_image as test_fn;
assert_eq!(test_fn(""), None);
assert_eq!(test_fn("version: '2'"), None);
assert_eq!(test_fn("image: "), None);
assert_eq!(test_fn(" image: "), None);
assert_eq!(test_fn(" image: nginx"), Some((" image: ", "nginx")));
assert_eq!(
test_fn(" image: library/nginx"),
Some((" image: ", "library/nginx"))
);
assert_eq!(
test_fn(" image: ghcr.io/library/nginx"),
Some((" image: ", "ghcr.io/library/nginx"))
);
assert_eq!(test_fn("# image: nginx"), None);
assert_eq!(
test_fn(" image: nginx #comment"),
Some((" image: ", "nginx"))
);
}
} }

View File

@ -4,10 +4,10 @@ use std::io::BufRead;
use std::io::BufReader; use std::io::BufReader;
use std::io::Write; use std::io::Write;
use regex::Regex;
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::repo;
use crate::ui::State; use crate::ui::State;
#[derive(Debug)] #[derive(Debug)]
@ -28,7 +28,6 @@ impl fmt::Display for Error {
pub struct ServiceSwitcher { pub struct ServiceSwitcher {
list: Vec<String>, list: Vec<String>,
state: ListState, state: ListState,
regex: Regex,
changed: bool, changed: bool,
} }
@ -47,7 +46,6 @@ impl ServiceSwitcher {
Self { Self {
list, list,
state: ListState::default(), state: ListState::default(),
regex: Regex::new(r"( *image *): *([^:]*):?([^:]?) *").unwrap(),
changed: false, changed: false,
} }
} }
@ -103,7 +101,7 @@ impl ServiceSwitcher {
} }
//check if line matches //check if line matches
if self.regex.is_match(&self.list[i]) { if repo::match_yaml_image(&self.list[i]).is_some() {
self.state.select(Some(i)); self.state.select(Some(i));
return true; return true;
} }
@ -135,7 +133,7 @@ impl ServiceSwitcher {
} }
//check if line matches //check if line matches
if self.regex.is_match(&self.list[i]) { if repo::match_yaml_image(&self.list[i]).is_some() {
self.state.select(Some(i)); self.state.select(Some(i));
return true; return true;
} }
@ -152,14 +150,10 @@ impl ServiceSwitcher {
pub fn extract_repo(&self) -> Result<String, Error> { pub fn extract_repo(&self) -> Result<String, Error> {
match self.state.selected() { match self.state.selected() {
None => return Err(Error::NoneSelected), None => return Err(Error::NoneSelected),
Some(i) => { Some(i) => match repo::match_yaml_image(&self.list[i]) {
let caps = match self.regex.captures(&self.list[i]) { None => return Err(Error::Parsing(String::from("Nothing found"))),
None => return Err(Error::Parsing(String::from("Nothing found"))), Some((_, repo)) => return Ok(repo.to_string()),
Some(cap) => cap, },
};
let result: String = caps.get(2).unwrap().as_str().to_string();
return Ok(result);
}
} }
} }
@ -167,16 +161,10 @@ impl ServiceSwitcher {
pub fn change_current_line(&mut self, repo_with_tag: String) { pub fn change_current_line(&mut self, repo_with_tag: String) {
match self.state.selected() { match self.state.selected() {
None => (), None => (),
Some(i) => { Some(i) => match repo::match_yaml_image(&self.list[i]) {
let caps = match self.regex.captures(&self.list[i]) { None => return,
None => return, Some((front, _)) => self.list[i] = format!("{}{}", front, repo_with_tag),
Some(cap) => cap, },
};
let mut line = caps.get(1).unwrap().as_str().to_string();
line.push_str(": ");
line.push_str(&repo_with_tag);
self.list[i] = line;
}
} }
self.changed = true; self.changed = true;
} }