Skip to content

Commit

Permalink
Changed config to yaml and did some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerwood committed Nov 16, 2021
1 parent 71a6bf6 commit 0a09447
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 60 deletions.
31 changes: 19 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tokio = { version = "1.13.0", features = ["full"] }
serde_json = "1.0.69"
serde = { version = "1.0.130", features = ["derive"] }
structopt = "0.3.25"
colored = "2.0.0"
openssl = { version = "0.10.38", features = ["vendored"] }
comrak = "0.12.1"
futures = "0.3.17"
futures = "0.3.17"
serde_yaml = "0.8.21"
23 changes: 0 additions & 23 deletions confluence-config.json

This file was deleted.

13 changes: 13 additions & 0 deletions confluence-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
content:
- filePath: './path/to/markdown-1.md'
title: Markdown 1
pageId: '353468432'
contentType: page
labels:
- ci/cd
- filePath: './path/to/markdown-2.md'
title: Markdown 2
pageId: '353435649'
contentType: page
labels:
- ci/cd
49 changes: 28 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use colored::*;
use comrak::{markdown_to_html, ComrakOptions};
use futures::future::try_join_all;
use reqwest;
use serde::{Deserialize, Serialize};
use serde_yaml;
use std::error::Error;
use std::fs;
use std::io;
Expand Down Expand Up @@ -53,6 +53,12 @@ pub struct Label {
pub name: String,
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Pages {
content: Vec<Page>,
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Page {
Expand All @@ -67,13 +73,13 @@ pub struct Confluence {
user: String,
secret: String,
fqdn: String,
pages: Vec<Page>,
pages: Pages,
}

impl Confluence {
pub fn new(user: String, secret: String, fqdn: String, config_path: String) -> Self {
let pages = Self::get_config(&config_path).unwrap_or_else(|x| {
println!("{} [Error: {}]", "Could not read config file".red(), x);
println!("Could not read config file.\n[Error: {}]", x);
process::exit(1)
});
Self {
Expand All @@ -84,10 +90,10 @@ impl Confluence {
}
}

fn get_config(config_path: &str) -> Result<Vec<Page>, io::Error> {
fn get_config(config_path: &str) -> Result<Pages, Box<dyn Error>> {
let file = fs::File::open(config_path)?;
let reader = BufReader::new(file);
let pages = serde_json::from_reader(reader)?;
let pages = serde_yaml::from_reader(reader)?;
Ok(pages)
}

Expand All @@ -100,20 +106,20 @@ impl Confluence {
))
.basic_auth(&self.user, Some(&self.secret))
.send()
.await?
.json::<serde_json::Value>()
.await?;

// let version = response.get("version").ok_or(process::exit(1););
let version = match response.get("version") {
if !response.status().is_success() {
println!("[Reqwest Error] {}", response.status());
process::exit(2)
}

let json = response.json::<serde_json::Value>().await?;

let version = match json.get("version") {
Some(x) => x.get("number").unwrap().as_u64().unwrap(),
None => {
println!(
"{} [ID: {}]",
"Could not get the content version".red(),
page_id
);
process::exit(1)
println!("Could not get the content version [ID: {}]", page_id);
process::exit(3)
}
};
Ok(version + 1)
Expand Down Expand Up @@ -154,7 +160,7 @@ impl Confluence {
client
.put(format!(
"https://{}/wiki/rest/api/content/{}",
&self.fqdn, page.page_id
&self.fqdn, &page.page_id
))
.basic_auth(&self.user, Some(&self.secret))
.json(&payload)
Expand All @@ -163,9 +169,7 @@ impl Confluence {

println!(
"[ID:{}] :: Updated {} - {}",
page.page_id,
page.title.purple(),
page.file_path
page.page_id, page.title, page.file_path
);

Ok(())
Expand All @@ -174,11 +178,14 @@ impl Confluence {
pub async fn update_pages(&self) -> Result<(), Box<dyn Error>> {
let mut futures = Vec::<_>::new();

for page in self.pages.iter() {
for page in self.pages.content.iter() {
futures.push(self.make_reqwest(page));
}

try_join_all(futures).await?;
try_join_all(futures).await.unwrap_or_else(|x| {
println!("[Error] {}", x);
process::exit(4)
});
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(
name = "confluence-updater",
about = "Create or update a page in Confluence Cloud",
about = "Update content in Confluence Cloud",
author = "Patrick Kerwood <[email protected]>"
)]
enum Opt {
Expand Down Expand Up @@ -32,7 +32,7 @@ enum Opt {
#[structopt(
short,
long,
default_value = "./confluence-config.json",
default_value = "./confluence-config.yaml",
env = "CU_CONFIG_PATH",
help = "The path to the config file."
)]
Expand Down

0 comments on commit 0a09447

Please sign in to comment.