-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
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
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,75 @@ | ||
#![allow(dead_code)] | ||
//! Handles the return message (command output) and return code from a CLI command. | ||
//! Most of the code here is temporary/experimental. | ||
//! https://github.com/informalsystems/ibc-rs/issues/500 will introduce a more permanent solution. | ||
use std::collections::HashMap; | ||
|
||
use serde::Serialize; | ||
|
||
pub fn on_exit(json: bool, out: CommandOutput) { | ||
// Handle the output message | ||
if json { | ||
println!("{}", serde_json::to_string(&out).unwrap()); | ||
} else { | ||
println!("status={:?}\nmsg={:?}", out.status, out.msg); | ||
} | ||
|
||
// The return code | ||
if out.status == CommandStatus::Error { | ||
std::process::exit(1); | ||
} else { | ||
std::process::exit(0); | ||
} | ||
} | ||
|
||
/// A CLI output with support for JSON serialization. | ||
#[derive(Serialize, Debug)] | ||
pub struct CommandOutput { | ||
/// The return status | ||
pub status: CommandStatus, | ||
|
||
/// An arbitrary message accompanying the output | ||
pub msg: Option<String>, | ||
|
||
/// Additional arbitrary key/values that may be present in the output | ||
/// TODO: at the moment this is not used! | ||
#[serde(flatten)] | ||
pub extra: HashMap<String, String>, | ||
} | ||
|
||
impl CommandOutput { | ||
pub fn new(status: CommandStatus) -> Self { | ||
CommandOutput { | ||
status, | ||
msg: None, | ||
extra: Default::default(), | ||
} | ||
} | ||
|
||
pub fn with_msg(self, msg: String) -> Self { | ||
CommandOutput { | ||
msg: Some(msg), | ||
..self | ||
} | ||
} | ||
|
||
pub fn with_extra(self, k: String, v: String) -> Self { | ||
let mut extra = self.extra.clone(); | ||
extra.insert(k, v); | ||
|
||
CommandOutput { extra, ..self } | ||
} | ||
} | ||
|
||
#[derive(Serialize, Debug, PartialEq, Eq)] | ||
pub enum CommandStatus { | ||
#[serde(rename(serialize = "success"))] | ||
Success, | ||
|
||
#[serde(rename(serialize = "info"))] | ||
Info, | ||
|
||
#[serde(rename(serialize = "error"))] | ||
Error, | ||
} |
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
pub mod application; | ||
pub mod commands; | ||
mod conclude; | ||
pub mod config; | ||
pub mod error; | ||
pub mod prelude; |