Skip to content

Commit 49358df

Browse files
Merge pull request #1192 from mintlayer/feature/wallet-print-version
Add print version command to the wallet
2 parents af0c789 + bd7528e commit 49358df

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

Diff for: wallet/build.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2023 RBB S.r.l
2+
// opensource@mintlayer.org
3+
// SPDX-License-Identifier: MIT
4+
// Licensed under the MIT License;
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
use std::process::Command;
17+
18+
fn main() {
19+
let git_head_hash = Command::new("git")
20+
.args(["rev-parse", "HEAD"])
21+
.output()
22+
.map(|out| String::from_utf8_lossy(&out.stdout).trim().to_string());
23+
24+
let git_tree_clean = Command::new("git")
25+
.args(["status", "--untracked-files=no", "--porcelain"])
26+
.output()
27+
.map(|out| String::from_utf8_lossy(&out.stdout).trim().to_string());
28+
29+
if let Ok(git_hash) = git_head_hash {
30+
println!("cargo:rustc-env=GIT_HEAD_HASH={}", git_hash);
31+
}
32+
if let Ok(git_tree_clean) = git_tree_clean {
33+
println!("cargo:rustc-env=GIT_TREE_CLEAN={}", git_tree_clean);
34+
}
35+
}

Diff for: wallet/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
pub mod account;
1717
mod key_chain;
1818
pub mod send_request;
19+
pub mod version;
1920
pub mod wallet;
2021
pub mod wallet_events;
2122

Diff for: wallet/src/version.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2023 RBB S.r.l
2+
// opensource@mintlayer.org
3+
// SPDX-License-Identifier: MIT
4+
// Licensed under the MIT License;
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/// Get the Wallet version and optionally git hash
17+
pub fn get_version() -> String {
18+
let git_head_hash = env!("GIT_HEAD_HASH");
19+
let git_tree_clean = env!("GIT_TREE_CLEAN");
20+
let version = env!("CARGO_PKG_VERSION");
21+
22+
let version_string = version;
23+
24+
// If the git hash is not available, we don't want to print anything
25+
let git_hash_string = if git_head_hash.trim().is_empty() {
26+
"".to_string()
27+
} else {
28+
format!("(HEAD hash: {})", git_head_hash)
29+
};
30+
31+
// If the git tree is clean, we don't want to print anything
32+
let git_tree_clean_string = if git_tree_clean.trim().is_empty() {
33+
""
34+
} else {
35+
"(dirty)"
36+
};
37+
38+
[version_string, &git_hash_string, git_tree_clean_string]
39+
.iter()
40+
.filter(|s| !s.is_empty())
41+
.map(|s| s.to_string())
42+
.collect::<Vec<String>>()
43+
.join(" ")
44+
}

Diff for: wallet/wallet-cli-lib/src/commands/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use common::{
2929
use crypto::key::{hdkd::u31::U31, PublicKey};
3030
use p2p_types::{bannable_address::BannableAddress, ip_or_socket_address::IpOrSocketAddress};
3131
use serialization::{hex::HexEncode, hex_encoded::HexEncoded};
32-
use wallet::{account::Currency, wallet_events::WalletEventsNoOp};
32+
use wallet::{account::Currency, version::get_version, wallet_events::WalletEventsNoOp};
3333
use wallet_controller::{NodeInterface, NodeRpcClient, PeerId, DEFAULT_ACCOUNT_INDEX};
3434

3535
use crate::{errors::WalletCliError, CliController};
@@ -325,6 +325,9 @@ pub enum WalletCommand {
325325
address: IpOrSocketAddress,
326326
},
327327

328+
/// Print the version of the software and optionally the git commit hash
329+
Version,
330+
328331
/// Quit the REPL
329332
Exit,
330333

@@ -1211,6 +1214,8 @@ impl CommandHandler {
12111214
Ok(ConsoleCommand::Print(addresses_table.to_string()))
12121215
}
12131216

1217+
WalletCommand::Version => Ok(ConsoleCommand::Print(get_version())),
1218+
12141219
WalletCommand::Exit => Ok(ConsoleCommand::Exit),
12151220
WalletCommand::History => Ok(ConsoleCommand::PrintHistory),
12161221
WalletCommand::ClearScreen => Ok(ConsoleCommand::ClearScreen),

0 commit comments

Comments
 (0)