-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from jelmer/svp-client
Add svp client crate
- Loading branch information
Showing
5 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "svp-client" | ||
version = "0.1.0" | ||
authors = ["Jelmer Vernooij <[email protected]>"] | ||
license = "Apache-2.0" | ||
description = "Client for the silver-platter protocol" | ||
|
||
[lib] | ||
|
||
[dependencies] | ||
log = "0.4.20" | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_json = "1.0.113" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::Success; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug, serde::Serialize)] | ||
pub struct ChangelogBehaviour { | ||
update: bool, | ||
explanation: String, | ||
} | ||
|
||
#[derive(Debug, serde::Serialize)] | ||
pub struct Context { | ||
changelog: Option<ChangelogBehaviour>, | ||
} | ||
|
||
pub fn report_success( | ||
versions: HashMap<String, String>, | ||
value: Option<i32>, | ||
context: Option<serde_json::Value>, | ||
changelog: Option<(bool, String)>, | ||
) { | ||
if std::env::var("SVP_API").ok().as_deref() == Some("1") { | ||
let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap(); | ||
|
||
serde_json::to_writer( | ||
f, | ||
&Success { | ||
versions, | ||
value, | ||
context, | ||
debian: Some(Context { | ||
changelog: changelog.map(|cl| ChangelogBehaviour { | ||
update: cl.0, | ||
explanation: cl.1, | ||
}), | ||
}), | ||
}, | ||
) | ||
.unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//! # svp-client | ||
//! | ||
//! `svp-client` is a library to interact with the [SVP | ||
//! protocol](https://github.com/jelmer/silver-platter/blob/master/codemod-protocol.md), as supported by | ||
//! the `svp` command-line tool. | ||
use std::collections::HashMap; | ||
|
||
pub mod debian; | ||
|
||
#[derive(Debug, serde::Serialize)] | ||
struct Failure { | ||
/// The result code, e.g. "header-missing". | ||
result_code: String, | ||
|
||
/// The versions of the packages involved. | ||
versions: HashMap<String, String>, | ||
|
||
/// A human-readable description of the failure. | ||
description: String, | ||
|
||
/// Whether the failure is transient. | ||
transient: Option<bool>, | ||
} | ||
|
||
#[derive(Debug, serde::Serialize)] | ||
struct Success { | ||
versions: HashMap<String, String>, | ||
value: Option<i32>, | ||
context: Option<serde_json::Value>, | ||
debian: Option<debian::Context>, | ||
} | ||
|
||
/// Report a success to the SVP server. | ||
/// | ||
/// # Arguments | ||
/// * `versions` - A map of package names to versions. | ||
pub fn report_success( | ||
versions: HashMap<String, String>, | ||
value: Option<i32>, | ||
context: Option<serde_json::Value>, | ||
) { | ||
if std::env::var("SVP_API").ok().as_deref() == Some("1") { | ||
let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap(); | ||
|
||
serde_json::to_writer( | ||
f, | ||
&Success { | ||
versions, | ||
value, | ||
context, | ||
debian: None, | ||
}, | ||
) | ||
.unwrap(); | ||
} | ||
} | ||
|
||
pub fn report_nothing_to_do(versions: HashMap<String, String>, description: Option<&str>) -> ! { | ||
let description = description.unwrap_or("Nothing to do"); | ||
if std::env::var("SVP_API").ok().as_deref() == Some("1") { | ||
let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap(); | ||
|
||
serde_json::to_writer( | ||
f, | ||
&Failure { | ||
result_code: "nothing-to-do".to_string(), | ||
versions, | ||
description: description.to_string(), | ||
transient: None, | ||
}, | ||
) | ||
.unwrap(); | ||
} | ||
log::error!("{}", description); | ||
std::process::exit(0); | ||
} | ||
|
||
pub fn report_fatal( | ||
versions: HashMap<String, String>, | ||
code: &str, | ||
description: &str, | ||
hint: Option<&str>, | ||
transient: Option<bool>, | ||
) -> ! { | ||
if std::env::var("SVP_API").ok().as_deref() == Some("1") { | ||
let f = std::fs::File::create(std::env::var("SVP_RESULT").unwrap()).unwrap(); | ||
|
||
serde_json::to_writer( | ||
f, | ||
&Failure { | ||
result_code: code.to_string(), | ||
versions, | ||
description: description.to_string(), | ||
transient, | ||
}, | ||
) | ||
.unwrap(); | ||
} | ||
log::error!("{}", description); | ||
if let Some(hint) = hint { | ||
log::info!("{}", hint); | ||
} | ||
std::process::exit(1); | ||
} | ||
|
||
pub fn load_resume() -> Option<serde_json::Value> { | ||
if std::env::var("SVP_API").ok().as_deref() == Some("1") { | ||
if let Ok(resume_path) = std::env::var("SVP_RESUME") { | ||
let f = std::fs::File::open(resume_path).unwrap(); | ||
let resume: serde_json::Value = serde_json::from_reader(f).unwrap(); | ||
Some(resume) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn enabled() -> bool { | ||
std::env::var("SVP_API").ok().as_deref() == Some("1") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters