Skip to content

Add print version command to the wallet #1192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions wallet/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2023 RBB S.r.l
// opensource@mintlayer.org
// SPDX-License-Identifier: MIT
// Licensed under the MIT License;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::process::Command;

fn main() {
let git_head_hash = Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.map(|out| String::from_utf8_lossy(&out.stdout).trim().to_string());

let git_tree_clean = Command::new("git")
.args(["status", "--untracked-files=no", "--porcelain"])
.output()
.map(|out| String::from_utf8_lossy(&out.stdout).trim().to_string());

if let Ok(git_hash) = git_head_hash {
println!("cargo:rustc-env=GIT_HEAD_HASH={}", git_hash);
}
if let Ok(git_tree_clean) = git_tree_clean {
println!("cargo:rustc-env=GIT_TREE_CLEAN={}", git_tree_clean);
}
}
1 change: 1 addition & 0 deletions wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
pub mod account;
mod key_chain;
pub mod send_request;
pub mod version;
pub mod wallet;
pub mod wallet_events;

Expand Down
44 changes: 44 additions & 0 deletions wallet/src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2023 RBB S.r.l
// opensource@mintlayer.org
// SPDX-License-Identifier: MIT
// Licensed under the MIT License;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// Get the Wallet version and optionally git hash
pub fn get_version() -> String {
let git_head_hash = env!("GIT_HEAD_HASH");
let git_tree_clean = env!("GIT_TREE_CLEAN");
let version = env!("CARGO_PKG_VERSION");

let version_string = version;

// If the git hash is not available, we don't want to print anything
let git_hash_string = if git_head_hash.trim().is_empty() {
"".to_string()
} else {
format!("(HEAD hash: {})", git_head_hash)
};

// If the git tree is clean, we don't want to print anything
let git_tree_clean_string = if git_tree_clean.trim().is_empty() {
""
} else {
"(dirty)"
};

[version_string, &git_hash_string, git_tree_clean_string]
.iter()
.filter(|s| !s.is_empty())
.map(|s| s.to_string())
.collect::<Vec<String>>()
.join(" ")
}
7 changes: 6 additions & 1 deletion wallet/wallet-cli-lib/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use common::{
use crypto::key::{hdkd::u31::U31, PublicKey};
use p2p_types::{bannable_address::BannableAddress, ip_or_socket_address::IpOrSocketAddress};
use serialization::{hex::HexEncode, hex_encoded::HexEncoded};
use wallet::{account::Currency, wallet_events::WalletEventsNoOp};
use wallet::{account::Currency, version::get_version, wallet_events::WalletEventsNoOp};
use wallet_controller::{NodeInterface, NodeRpcClient, PeerId, DEFAULT_ACCOUNT_INDEX};

use crate::{errors::WalletCliError, CliController};
Expand Down Expand Up @@ -325,6 +325,9 @@ pub enum WalletCommand {
address: IpOrSocketAddress,
},

/// Print the version of the software and optionally the git commit hash
Version,

/// Quit the REPL
Exit,

Expand Down Expand Up @@ -1211,6 +1214,8 @@ impl CommandHandler {
Ok(ConsoleCommand::Print(addresses_table.to_string()))
}

WalletCommand::Version => Ok(ConsoleCommand::Print(get_version())),

WalletCommand::Exit => Ok(ConsoleCommand::Exit),
WalletCommand::History => Ok(ConsoleCommand::PrintHistory),
WalletCommand::ClearScreen => Ok(ConsoleCommand::ClearScreen),
Expand Down