From 01834e69d0dc57595767c223cdb1d242965700c9 Mon Sep 17 00:00:00 2001 From: everpcpc Date: Fri, 28 Apr 2023 10:58:17 +0800 Subject: [PATCH] chore(cli): better version with build info (#103) --- cli/Cargo.toml | 4 ++++ cli/build.rs | 21 +++++++++++++++++++++ cli/src/main.rs | 11 ++++++++++- cli/src/session.rs | 3 ++- 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 cli/build.rs diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 5053c734..41111851 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -22,6 +22,7 @@ humantime-serde = "1.1.1" indicatif = "0.17.3" itertools = "0.10.5" logos = "0.13.0" +once_cell = "1.17.1" rustyline = "11.0.0" serde = { version = "1.0.159", features = ["derive"] } serde_json = "1.0.95" @@ -39,6 +40,9 @@ tokio = { version = "1.26", features = [ toml = "0.7.3" url = { version = "2.3.1", default-features = false } +[build-dependencies] +vergen = { version = "8.1.3", features = ["build", "git", "gitcl"] } + [[bin]] name = "bendsql" path = "src/main.rs" diff --git a/cli/build.rs b/cli/build.rs new file mode 100644 index 00000000..bc3be6fb --- /dev/null +++ b/cli/build.rs @@ -0,0 +1,21 @@ +// Copyright 2023 Datafuse Labs. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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::error::Error; +use vergen::EmitBuilder; + +fn main() -> Result<(), Box> { + EmitBuilder::builder().all_build().all_git().emit()?; + Ok(()) +} diff --git a/cli/src/main.rs b/cli/src/main.rs index a4b0762f..712b5f37 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -25,6 +25,14 @@ use std::{collections::BTreeMap, io::stdin}; use anyhow::{anyhow, Result}; use clap::{CommandFactory, Parser, ValueEnum}; use config::{Config, OutputFormat}; +use once_cell::sync::Lazy; + +static VERSION: Lazy = Lazy::new(|| { + let version = option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"); + let sha = option_env!("VERGEN_GIT_DESCRIBE").unwrap_or("dev"); + let timestamp = option_env!("VERGEN_BUILD_TIMESTAMP").unwrap_or(""); + format!("{}-{}({})", version, sha, timestamp) +}); /// Supported file format and options: /// https://databend.rs/doc/sql-reference/file-format-options @@ -83,8 +91,9 @@ impl InputFormat { } #[derive(Debug, Parser, PartialEq)] +#[command(version = VERSION.as_str())] // disable default help flag since it would conflict with --host -#[command(author, version, about, disable_help_flag = true)] +#[command(author, about, disable_help_flag = true)] struct Args { #[clap(long, help = "Print help information")] help: bool, diff --git a/cli/src/session.rs b/cli/src/session.rs index 8b916349..21dfb0c6 100644 --- a/cli/src/session.rs +++ b/cli/src/session.rs @@ -34,6 +34,7 @@ use crate::config::OutputFormat; use crate::config::Settings; use crate::display::{format_write_progress, ChunkDisplay, FormatDisplay, ReplDisplay}; use crate::helper::CliHelper; +use crate::VERSION; pub struct Session { dsn: String, @@ -49,7 +50,7 @@ impl Session { let conn = new_connection(&dsn)?; let info = conn.info(); if is_repl { - println!("Welcome to BendSQL."); + println!("Welcome to BendSQL {}.", VERSION.as_str()); println!( "Trying connect to {}:{} as user {}.", info.host, info.port, info.user