switched out regex in ServiceSwitcher to repo functions
This commit is contained in:
parent
fadbe48b05
commit
48c45a1372
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -541,6 +541,7 @@ name = "query-docker-tags"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"lazy_static",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"serde",
|
||||
|
@ -14,6 +14,7 @@ chrono = "0.4.19"
|
||||
tui = "0.16"
|
||||
termion = "1.5"
|
||||
regex = "1.5.4"
|
||||
lazy_static = "1.4.0"
|
||||
|
||||
[profile.release]
|
||||
lto = "yes"
|
||||
|
38
src/repo.rs
38
src/repo.rs
@ -16,9 +16,18 @@ pub enum Repo {
|
||||
Project(String),
|
||||
}
|
||||
|
||||
// pub fn extract_yaml(repo: &str) -> (String, String, String, String) {
|
||||
// let regex = regex::Regex::new(r"( *image *:) *([^/])??/([^/:]):?(.*)?");
|
||||
// }
|
||||
/// check if yaml line matches and returns the split of repo string and rest
|
||||
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> {
|
||||
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"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -4,10 +4,10 @@ use std::io::BufRead;
|
||||
use std::io::BufReader;
|
||||
use std::io::Write;
|
||||
|
||||
use regex::Regex;
|
||||
use tui::style::{Color, Style};
|
||||
use tui::widgets::{Block, Borders, List, ListState};
|
||||
|
||||
use crate::repo;
|
||||
use crate::ui::State;
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -28,7 +28,6 @@ impl fmt::Display for Error {
|
||||
pub struct ServiceSwitcher {
|
||||
list: Vec<String>,
|
||||
state: ListState,
|
||||
regex: Regex,
|
||||
changed: bool,
|
||||
}
|
||||
|
||||
@ -47,7 +46,6 @@ impl ServiceSwitcher {
|
||||
Self {
|
||||
list,
|
||||
state: ListState::default(),
|
||||
regex: Regex::new(r"( *image *): *([^:]*):?([^:]?) *").unwrap(),
|
||||
changed: false,
|
||||
}
|
||||
}
|
||||
@ -103,7 +101,7 @@ impl ServiceSwitcher {
|
||||
}
|
||||
|
||||
//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));
|
||||
return true;
|
||||
}
|
||||
@ -135,7 +133,7 @@ impl ServiceSwitcher {
|
||||
}
|
||||
|
||||
//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));
|
||||
return true;
|
||||
}
|
||||
@ -152,14 +150,10 @@ impl ServiceSwitcher {
|
||||
pub fn extract_repo(&self) -> Result<String, Error> {
|
||||
match self.state.selected() {
|
||||
None => return Err(Error::NoneSelected),
|
||||
Some(i) => {
|
||||
let caps = match self.regex.captures(&self.list[i]) {
|
||||
Some(i) => match repo::match_yaml_image(&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);
|
||||
}
|
||||
Some((_, repo)) => return Ok(repo.to_string()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -167,16 +161,10 @@ impl ServiceSwitcher {
|
||||
pub fn change_current_line(&mut self, repo_with_tag: String) {
|
||||
match self.state.selected() {
|
||||
None => (),
|
||||
Some(i) => {
|
||||
let caps = match self.regex.captures(&self.list[i]) {
|
||||
Some(i) => match repo::match_yaml_image(&self.list[i]) {
|
||||
None => return,
|
||||
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;
|
||||
}
|
||||
Some((front, _)) => self.list[i] = format!("{}{}", front, repo_with_tag),
|
||||
},
|
||||
}
|
||||
self.changed = true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user