Skip to main content

amareleo_chain/
lib.rs

1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "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// http://www.apache.org/licenses/LICENSE-2.0
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
16use amareleo_chain_cli::{commands::CLI, helpers::Updater};
17
18use clap::Parser;
19use std::{env, process::exit};
20
21#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
22use tikv_jemallocator::Jemalloc;
23
24#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
25#[global_allocator]
26static GLOBAL: Jemalloc = Jemalloc;
27
28pub struct BuildInfo<'a> {
29    pub bin: &'a str,
30    pub version: &'a str,
31    pub repo: &'a str,
32    pub branch: &'a str,
33    pub commit: &'a str,
34    pub features: &'a str,
35}
36
37pub fn main_core(build: &BuildInfo) -> anyhow::Result<()> {
38    // A hack to avoid having to go through clap to display advanced version information.
39    check_for_version(build);
40
41    // Parse the given arguments.
42    let cli = CLI::parse();
43    // Run the updater.
44    println!("{}", Updater::print_cli(build.repo, build.bin));
45    // Run the CLI.
46    match cli.command.parse(build.repo, build.bin) {
47        Ok(output) => println!("{output}\n"),
48        Err(error) => {
49            // Print the top level error and then any additional context.
50            println!("⚠️  {error}");
51            for entry in error.chain().skip(1) {
52                println!("     ↳ {entry}");
53            }
54            println!();
55            exit(1);
56        }
57    }
58    Ok(())
59}
60
61/// Checks whether the version information was requested and - if so - display it and exit.
62fn check_for_version(build: &BuildInfo) {
63    if let Some(first_arg) = env::args().nth(1) {
64        if ["--version", "-V"].contains(&&*first_arg) {
65            println!("{} {} {} {} features=[{}]", build.bin, build.version, build.branch, build.commit, build.features);
66            exit(0);
67        }
68    }
69}