Files
komodo/bin/periphery/src/helpers.rs
Maxwell Becker f9b2994d44 1.16.2 (#145)
* Env vars written using same quotes (single vs double) as the user passes

* fmt

* trim start matches '-'

* ts client version
2024-10-22 11:41:17 -07:00

92 lines
2.3 KiB
Rust

use anyhow::Context;
use komodo_client::{
entities::{EnvironmentVar, SearchCombinator},
parsers::QUOTE_PATTERN,
};
use crate::config::periphery_config;
pub fn git_token(
domain: &str,
account_username: &str,
) -> anyhow::Result<&'static str> {
periphery_config()
.git_providers
.iter()
.find(|provider| provider.domain == domain)
.and_then(|provider| {
provider.accounts.iter().find(|account| account.username == account_username).map(|account| account.token.as_str())
})
.with_context(|| format!("did not find token in config for git account {account_username} | domain {domain}"))
}
pub fn registry_token(
domain: &str,
account_username: &str,
) -> anyhow::Result<&'static str> {
periphery_config()
.docker_registries
.iter()
.find(|registry| registry.domain == domain)
.and_then(|registry| {
registry.accounts.iter().find(|account| account.username == account_username).map(|account| account.token.as_str())
})
.with_context(|| format!("did not find token in config for docker registry account {account_username} | domain {domain}"))
}
pub fn parse_extra_args(extra_args: &[String]) -> String {
let args = extra_args.join(" ");
if !args.is_empty() {
format!(" {args}")
} else {
args
}
}
pub fn parse_labels(labels: &[EnvironmentVar]) -> String {
labels
.iter()
.map(|p| {
if p.value.starts_with(QUOTE_PATTERN)
&& p.value.ends_with(QUOTE_PATTERN)
{
// If the value already wrapped in quotes, don't wrap it again
format!(" --label {}={}", p.variable, p.value)
} else {
format!(" --label {}=\"{}\"", p.variable, p.value)
}
})
.collect::<Vec<_>>()
.join("")
}
pub fn log_grep(
terms: &[String],
combinator: SearchCombinator,
invert: bool,
) -> String {
let maybe_invert = invert.then_some(" -v").unwrap_or_default();
match combinator {
SearchCombinator::Or => {
format!("grep{maybe_invert} -E '{}'", terms.join("|"))
}
SearchCombinator::And => {
format!(
"grep{maybe_invert} -P '^(?=.*{})'",
terms.join(")(?=.*")
)
}
}
}
pub fn interpolate_variables(
input: &str,
) -> svi::Result<(String, Vec<(String, String)>)> {
svi::interpolate_variables(
input,
&periphery_config().secrets,
svi::Interpolator::DoubleBrackets,
true,
)
}