Skip to content

Commit

Permalink
fix #814
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Mar 10, 2025
1 parent e349028 commit 962aa31
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
10 changes: 5 additions & 5 deletions cli/src/commands/cms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
},
models::folder::{Ext, SourceExtension},
process,
utils::absolute_path,
utils::{absolute_path, new_custom_ini_file},
CmsCommands, Commands,
};

Expand Down Expand Up @@ -319,7 +319,7 @@ fn get_home() -> DaikokuResult<PathBuf> {
}

fn read(last_attempt: bool) -> DaikokuResult<Ini> {
let mut config = Ini::new();
let mut config = new_custom_ini_file();

match config.load(&get_path()?) {
Ok(_) => Ok(config),
Expand All @@ -332,7 +332,7 @@ fn read(last_attempt: bool) -> DaikokuResult<Ini> {
}

fn remove_cms() -> DaikokuResult<()> {
let mut config = Ini::new();
let mut config = new_custom_ini_file();
config.clear();
match config.write(&get_path()?) {
Ok(_) => {
Expand Down Expand Up @@ -574,7 +574,7 @@ pub(crate) fn create_api_folder(

created.push(item.human_readable_id.clone());

let mut config: Ini = Ini::new();
let mut config: Ini = new_custom_ini_file();
config.set(&"default", "id", Some(item._id.clone()));
let _ = config.write(file_path.clone().join(".daikoku_data"));

Expand Down Expand Up @@ -619,7 +619,7 @@ pub(crate) fn create_mail_folder(
.clone()
.join(get_mail_page_path(&item._id.clone().replace(".", "-"), is_root_mail).unwrap());

let mut config: Ini = Ini::new();
let mut config: Ini = new_custom_ini_file();

config.set(&"default", "id", Some(item._id.clone()));

Expand Down
16 changes: 4 additions & 12 deletions cli/src/commands/environments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
error::{DaikokuCliError, DaikokuResult},
logger,
},
utils::apply_credentials_mask,
utils::{apply_credentials_mask, new_custom_ini_file},
EnvironmentsCommands,
};
use configparser::ini::Ini;
Expand Down Expand Up @@ -71,7 +71,7 @@ fn get_secrets_path() -> DaikokuResult<String> {
}

fn read_environments() -> DaikokuResult<Ini> {
let mut config = Ini::new();
let mut config = new_custom_ini_file();

match config.load(&get_environments_path()?) {
Ok(_) => Ok(config),
Expand All @@ -80,7 +80,7 @@ fn read_environments() -> DaikokuResult<Ini> {
}

fn read_secrets() -> DaikokuResult<Ini> {
let mut config = Ini::new();
let mut config = new_custom_ini_file();

match config.load(&get_secrets_path()?) {
Ok(_) => Ok(config),
Expand Down Expand Up @@ -252,22 +252,14 @@ async fn configure(apikey: Option<String>, cookie: Option<String>) -> DaikokuRes
Ok(())
}

pub(crate) fn format_cookie(str: String) -> String {
if str.starts_with("daikoku-session=") {
str.to_string()
} else {
format!("daikoku-session={}", str)
}
}

pub(crate) fn read_cookie_from_environment(failed_if_not_present: bool) -> DaikokuResult<String> {
let config: Ini = read_environments()?;

if let Some(environment) = config.get("default", "environment") {
let secrets: Ini = read_secrets()?;
secrets
.get(&environment, "cookie")
.map(|cookie| Ok(format_cookie(cookie)))
.map(Ok)
.unwrap_or(if failed_if_not_present {
Err(DaikokuCliError::Configuration(
"Missing cookie on default environment. Run daikoku login".to_string(),
Expand Down
12 changes: 8 additions & 4 deletions cli/src/commands/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ async fn forward_api_call(

let status = status.as_u16();

println!("{:?}", _headers);

if status >= 300 && status < 400 {
Ok(Response::new(Full::new(Bytes::from(
"Authentication needed! Refresh this page once done",
Expand Down Expand Up @@ -405,8 +403,14 @@ async fn render_page(
let mut builder = reqwest::Client::new()
.post(url)
.header(header::HOST, host)
.header(header::CONTENT_TYPE, "application/json")
.body(body);
.header(header::CONTENT_TYPE, "application/json");


if let Ok(cookie) = read_cookie_from_environment(false) {
builder = builder.header(header::COOKIE, cookie);
}

builder = builder.body(body);

if authentication && page.authenticated() {
match read_cookie_from_environment(true) {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/models/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
str::FromStr,
};

use crate::logging::error::{DaikokuCliError, DaikokuResult};
use crate::{logging::error::{DaikokuCliError, DaikokuResult}, utils::new_custom_ini_file};

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub(crate) struct CmsFile {
Expand Down Expand Up @@ -158,7 +158,7 @@ fn read_api_folder(path: &PathBuf) -> DaikokuResult<Vec<CmsFile>> {
".daikoku_data".to_string(),
".metadata".to_string(),
);
let mut data = Ini::new();
let mut data = new_custom_ini_file();
let data = Ini::read(&mut data, daikoku_data_file.content).map_err(|_err| {
DaikokuCliError::FileSystem(format!(
"unable to read daikoku_data file in {:#?}",
Expand Down
7 changes: 7 additions & 0 deletions cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
path::{Path, PathBuf},
};

use configparser::ini::{Ini, IniDefault};
use http_body_util::BodyExt;
use std::io::Read;

Expand Down Expand Up @@ -79,3 +80,9 @@ pub(crate) fn apply_credentials_mask(credential: &String, show_full_credentials:
credential.as_str()[1..10].to_string() + "*******"
}
}

pub(crate) fn new_custom_ini_file() -> Ini {
let mut defaults: IniDefault = Default::default();
defaults.comment_symbols = vec![];
Ini::new_from_defaults(defaults)
}

0 comments on commit 962aa31

Please sign in to comment.