mirror of
https://github.com/sergi0g/cup.git
synced 2025-11-17 09:33:38 -05:00
Switch back to json
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use serde_json::json;
|
||||
use json::object;
|
||||
|
||||
use crate::utils::{sort_update_vec, to_json};
|
||||
|
||||
@@ -40,7 +40,7 @@ pub fn print_updates(updates: &[(String, Option<bool>)], icons: &bool) {
|
||||
}
|
||||
|
||||
pub fn print_raw_updates(updates: &[(String, Option<bool>)]) {
|
||||
println!("{}", serde_json::to_string(&to_json(updates)).unwrap());
|
||||
println!("{}", json::stringify(to_json(updates)));
|
||||
}
|
||||
|
||||
pub fn print_update(name: &str, has_update: &Option<bool>) {
|
||||
@@ -58,7 +58,7 @@ pub fn print_update(name: &str, has_update: &Option<bool>) {
|
||||
}
|
||||
|
||||
pub fn print_raw_update(name: &str, has_update: &Option<bool>) {
|
||||
let result = json!({"images": {name: has_update}});
|
||||
let result = object! {images: {[name]: *has_update}} ;
|
||||
println!("{}", result);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use check::{get_all_updates, get_update};
|
||||
use clap::{Parser, Subcommand};
|
||||
#[cfg(feature = "cli")]
|
||||
use formatting::{print_raw_update, print_raw_updates, print_update, print_updates, Spinner};
|
||||
#[cfg(feature = "cli")]
|
||||
use check::{get_all_updates, get_update};
|
||||
#[cfg(feature = "server")]
|
||||
use server::serve;
|
||||
use std::path::PathBuf;
|
||||
@@ -10,13 +10,13 @@ use utils::load_config;
|
||||
|
||||
pub mod check;
|
||||
pub mod docker;
|
||||
#[cfg(feature = "cli")]
|
||||
pub mod formatting;
|
||||
pub mod image;
|
||||
pub mod registry;
|
||||
pub mod utils;
|
||||
#[cfg(feature = "cli")]
|
||||
pub mod formatting;
|
||||
#[cfg(feature = "server")]
|
||||
pub mod server;
|
||||
pub mod utils;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::sync::Mutex;
|
||||
|
||||
use json::JsonValue;
|
||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||
use serde_json::Value;
|
||||
use ureq::Error;
|
||||
|
||||
use http_auth::parse_challenges;
|
||||
@@ -95,18 +95,13 @@ pub fn get_token(images: Vec<&Image>, auth_url: &str) -> String {
|
||||
error!("Token request failed!\n{}", e)
|
||||
}
|
||||
};
|
||||
let parsed_token_response: Value = match serde_json::from_str(&raw_response) {
|
||||
let parsed_token_response: JsonValue = match json::parse(&raw_response) {
|
||||
Ok(parsed) => parsed,
|
||||
Err(e) => {
|
||||
error!("Failed to parse server response\n{}", e)
|
||||
}
|
||||
};
|
||||
parsed_token_response
|
||||
.get("token")
|
||||
.unwrap()
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
parsed_token_response["token"].to_string()
|
||||
}
|
||||
|
||||
fn parse_www_authenticate(www_auth: &str) -> String {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
use std::sync::Arc;
|
||||
|
||||
use chrono::Local;
|
||||
use json::JsonValue;
|
||||
use liquid::{object, Object};
|
||||
use tokio::sync::Mutex;
|
||||
use xitca_web::{
|
||||
@@ -15,7 +16,7 @@ use xitca_web::{
|
||||
use crate::{
|
||||
check::get_all_updates,
|
||||
error,
|
||||
utils::{sort_update_vec, to_json, Config, JsonData},
|
||||
utils::{sort_update_vec, to_json},
|
||||
};
|
||||
|
||||
const RAW_TEMPLATE: &str = include_str!("static/template.liquid");
|
||||
@@ -24,7 +25,7 @@ const FAVICON_ICO: &[u8] = include_bytes!("static/favicon.ico");
|
||||
const FAVICON_SVG: &[u8] = include_bytes!("static/favicon.svg");
|
||||
const APPLE_TOUCH_ICON: &[u8] = include_bytes!("static/apple-touch-icon.png");
|
||||
|
||||
pub async fn serve(port: &u16, socket: Option<String>, config: Config) -> std::io::Result<()> {
|
||||
pub async fn serve(port: &u16, socket: Option<String>, config: JsonValue) -> std::io::Result<()> {
|
||||
let mut data = ServerData::new(socket, config).await;
|
||||
data.refresh().await;
|
||||
App::new()
|
||||
@@ -48,7 +49,7 @@ async fn home(data: StateRef<'_, Arc<Mutex<ServerData>>>) -> WebResponse {
|
||||
|
||||
async fn json(data: StateRef<'_, Arc<Mutex<ServerData>>>) -> WebResponse {
|
||||
WebResponse::new(ResponseBody::from(
|
||||
serde_json::to_string(&data.lock().await.json).unwrap(),
|
||||
json::stringify(data.lock().await.json.clone())
|
||||
))
|
||||
}
|
||||
|
||||
@@ -72,19 +73,19 @@ async fn apple_touch_icon() -> WebResponse {
|
||||
struct ServerData {
|
||||
template: String,
|
||||
raw_updates: Vec<(String, Option<bool>)>,
|
||||
json: JsonData,
|
||||
json: JsonValue,
|
||||
socket: Option<String>,
|
||||
config: Config,
|
||||
config: JsonValue,
|
||||
}
|
||||
|
||||
impl ServerData {
|
||||
async fn new(socket: Option<String>, config: Config) -> Self {
|
||||
async fn new(socket: Option<String>, config: JsonValue) -> Self {
|
||||
let mut s = Self {
|
||||
socket,
|
||||
template: String::new(),
|
||||
json: JsonData {
|
||||
metrics: HashMap::new(),
|
||||
images: HashMap::new(),
|
||||
json: json::object! {
|
||||
metrics: json::object! {},
|
||||
images: json::object! {},
|
||||
},
|
||||
raw_updates: Vec::new(),
|
||||
config,
|
||||
@@ -110,8 +111,8 @@ impl ServerData {
|
||||
.collect::<Vec<Object>>();
|
||||
self.json = to_json(&self.raw_updates);
|
||||
let last_updated = Local::now().format("%Y-%m-%d %H:%M:%S");
|
||||
let theme = match &self.config.theme {
|
||||
Some(t) => match t.as_str() {
|
||||
let theme = match &self.config["theme"].as_str() {
|
||||
Some(t) => match *t {
|
||||
"default" => "neutral",
|
||||
"blue" => "gray",
|
||||
_ => error!(
|
||||
@@ -122,7 +123,7 @@ impl ServerData {
|
||||
None => "neutral",
|
||||
};
|
||||
let globals = object!({
|
||||
"metrics": [{"name": "Monitored images", "value": self.json.metrics.get("monitored_images")}, {"name": "Up to date", "value": self.json.metrics.get("up_to_date")}, {"name": "Updates available", "value": self.json.metrics.get("update_available")}, {"name": "Unknown", "value": self.json.metrics.get("unknown")}],
|
||||
"metrics": [{"name": "Monitored images", "value": self.json["metrics"]["monitored_images"].as_usize()}, {"name": "Up to date", "value": self.json["metrics"]["up_to_date"].as_usize()}, {"name": "Updates available", "value": self.json["metrics"]["update_available"].as_usize()}, {"name": "Unknown", "value": self.json["metrics"]["unknown"].as_usize()}],
|
||||
"images": images,
|
||||
"style": STYLE,
|
||||
"last_updated": last_updated.to_string(),
|
||||
|
||||
40
src/utils.rs
40
src/utils.rs
@@ -1,8 +1,8 @@
|
||||
use std::{collections::HashMap, path::PathBuf};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use json::{object, JsonValue};
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// This macro is an alternative to panic. It prints the message you give it and exits the process with code 1, without printing a stack trace. Useful for when the program has to exit due to a user error or something unexpected which is unrelated to the program (e.g. a failed web request)
|
||||
#[macro_export]
|
||||
@@ -87,7 +87,7 @@ pub fn sort_update_vec(updates: &[(String, Option<bool>)]) -> Vec<(String, Optio
|
||||
}
|
||||
|
||||
/// Tries to load the config from the path provided and perform basic validation
|
||||
pub fn load_config(config_path: Option<PathBuf>) -> Config {
|
||||
pub fn load_config(config_path: Option<PathBuf>) -> JsonValue {
|
||||
let raw_config = match &config_path {
|
||||
Some(path) => std::fs::read_to_string(path),
|
||||
None => Ok(String::from("{\"theme\":\"default\"}")),
|
||||
@@ -98,25 +98,19 @@ pub fn load_config(config_path: Option<PathBuf>) -> Config {
|
||||
&config_path.unwrap().to_str().unwrap()
|
||||
)
|
||||
};
|
||||
match serde_json::from_str(&raw_config.unwrap()) {
|
||||
match json::parse(&raw_config.unwrap()) {
|
||||
Ok(v) => v,
|
||||
Err(e) => panic!("Failed to parse config!\n{}", e),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
pub authentication: Option<HashMap<String, String>>,
|
||||
pub theme: Option<String>,
|
||||
}
|
||||
|
||||
pub fn to_json(updates: &[(String, Option<bool>)]) -> JsonData {
|
||||
let mut json_data: JsonData = JsonData {
|
||||
metrics: HashMap::new(),
|
||||
images: HashMap::new(),
|
||||
pub fn to_json(updates: &[(String, Option<bool>)]) -> JsonValue {
|
||||
let mut json_data: JsonValue = object! {
|
||||
metrics: object! {},
|
||||
images: object! {}
|
||||
};
|
||||
updates.iter().for_each(|(image, has_update)| {
|
||||
let _ = json_data.images.insert(image.clone(), *has_update);
|
||||
let _ = json_data["images"].insert(image, *has_update);
|
||||
});
|
||||
let up_to_date = updates
|
||||
.iter()
|
||||
@@ -133,17 +127,9 @@ pub fn to_json(updates: &[(String, Option<bool>)]) -> JsonData {
|
||||
.filter(|&(_, value)| value.is_none())
|
||||
.collect::<Vec<&(String, Option<bool>)>>()
|
||||
.len();
|
||||
let _ = json_data.metrics.insert("monitored_images", updates.len());
|
||||
let _ = json_data.metrics.insert("up_to_date", up_to_date);
|
||||
let _ = json_data
|
||||
.metrics
|
||||
.insert("update_available", update_available);
|
||||
let _ = json_data.metrics.insert("unknown", unknown);
|
||||
let _ = json_data["metrics"].insert("monitored_images", updates.len());
|
||||
let _ = json_data["metrics"].insert("up_to_date", up_to_date);
|
||||
let _ = json_data["metrics"].insert("update_available", update_available);
|
||||
let _ = json_data["metrics"].insert("unknown", unknown);
|
||||
json_data
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct JsonData {
|
||||
pub metrics: HashMap<&'static str, usize>,
|
||||
pub images: HashMap<String, Option<bool>>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user