-
Notifications
You must be signed in to change notification settings - Fork 2.2k
enhancement(dev): Add dedicated dev tool #14990
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
Changes from 2 commits
a1afed9
9ebac12
3b71558
9f44785
fb2163f
ec58410
4de3c12
3d1ffa6
9e3d6f4
fd833a7
a2e000c
f43fb51
b9d8583
9939e81
f12f024
89d1892
25f08a8
953937b
5b68809
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| [package] | ||
| name = "vdev" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| authors = ["Vector Contributors <vector@datadoghq.com>"] | ||
| license = "MPL-2.0" | ||
| readme = "README.md" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| anyhow = "1.0.66" | ||
| cached = "0.40.0" | ||
| clap = { version = "4.0.18", features = ["derive"] } | ||
| clap-verbosity-flag = "2.0.0" | ||
| confy = "0.5.1" | ||
| # remove this when stabilized https://doc.rust-lang.org/stable/std/path/fn.absolute.html | ||
| dunce = "1.0.3" | ||
| env_logger = "0.9.1" | ||
| home = "0.5.4" | ||
| log = "0.4.17" | ||
| os_info = { version = "3.5.1", default-features = false } | ||
| # watch https://github.com/epage/anstyle for official interop with Clap | ||
| owo-colors = { version = "3.5.0", features = ["supports-colors"] } | ||
| serde = { version = "1.0", features = ["derive"] } | ||
|
|
||
| # https://github.com/rust-lang/cargo/issues/6745#issuecomment-472667516 | ||
| [workspace] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # vdev | ||
|
|
||
| ----- | ||
|
|
||
| This is the command line tooling for Vector development. | ||
|
|
||
| Table of Contents: | ||
|
|
||
| - [Installation](#installation) | ||
| - [Configuration](#configuration) | ||
| - [Repository](#repository) | ||
| - [Starship](#starship) | ||
|
|
||
| ## Installation | ||
|
|
||
|
ofek marked this conversation as resolved.
|
||
| ```text | ||
| cargo install -f --path vdev | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| ### Repository | ||
|
|
||
| Setting the path to the repository explicitly allows the application to be used at any time no matter the current working directory. | ||
|
|
||
| ```text | ||
| vdev config set repo . | ||
| ``` | ||
|
|
||
| To test, enter your home directory and then run: | ||
|
|
||
| ```text | ||
| vdev exec ls | ||
| ``` | ||
|
|
||
| ### Starship | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a really nice touch! |
||
|
|
||
| A custom command for the [Starship](https://starship.rs) prompt is available. | ||
|
|
||
| ```toml | ||
| format = """ | ||
| ... | ||
| ${custom.vdev}\ | ||
| ... | ||
| $line_break\ | ||
| ... | ||
| $character""" | ||
|
|
||
| # <clipped> | ||
|
|
||
| [custom.vdev] | ||
| command = "vdev meta starship" | ||
| when = true | ||
| # Windows | ||
| # shell = ["cmd", "/C"] | ||
| # Other | ||
| # shell = ["sh", "--norc"] | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| use log::{Level, LevelFilter}; | ||
| use owo_colors::{ | ||
| OwoColorize, | ||
| Stream::{Stderr, Stdout}, | ||
| }; | ||
| use std::env; | ||
|
|
||
| use crate::config::{Config, ConfigFile}; | ||
| use crate::platform::Platform; | ||
| use crate::repo::core::Repository; | ||
|
|
||
| pub struct Application { | ||
| pub(crate) config_file: ConfigFile, | ||
| pub(crate) config: Config, | ||
| pub(crate) repo: Repository, | ||
| pub(crate) platform: Platform, | ||
| verbosity: LevelFilter, | ||
| } | ||
|
|
||
| impl Application { | ||
| pub fn new(verbosity: LevelFilter) -> Application { | ||
| let platform = Platform::new(); | ||
| let config_file = ConfigFile::new(); | ||
| let config_model = config_file.load(); | ||
|
|
||
| // Set the path to the repository for the entire application | ||
| let path = if !config_model.repo.is_empty() { | ||
| config_model.repo.to_string() | ||
| } else { | ||
| match env::current_dir() { | ||
| Ok(p) => p.display().to_string(), | ||
| Err(_) => ".".to_string(), | ||
| } | ||
| }; | ||
|
|
||
| Application { | ||
| config_file: config_file, | ||
| config: config_model, | ||
| repo: Repository::new(path), | ||
| platform: platform, | ||
| verbosity: verbosity, | ||
| } | ||
| } | ||
|
|
||
| pub fn exit(&self, code: i32) { | ||
| std::process::exit(code); | ||
| } | ||
|
|
||
| pub fn abort<T: AsRef<str>>(&self, text: T) { | ||
| self.display_error(text); | ||
| self.exit(1); | ||
| } | ||
|
|
||
| pub fn display<T: AsRef<str>>(&self, text: T) { | ||
| // Simply bold rather than bright white for terminals with white backgrounds | ||
| println!( | ||
| "{}", | ||
| text.as_ref().if_supports_color(Stdout, |text| text.bold()) | ||
| ); | ||
| } | ||
|
|
||
| #[allow(dead_code)] | ||
| pub fn display_trace<T: AsRef<str>>(&self, text: T) { | ||
| if Level::Trace <= self.verbosity { | ||
| eprintln!( | ||
| "{}", | ||
| text.as_ref().if_supports_color(Stderr, |text| text.bold()) | ||
| ); | ||
| } | ||
| } | ||
|
ofek marked this conversation as resolved.
Outdated
|
||
|
|
||
| #[allow(dead_code)] | ||
| pub fn display_debug<T: AsRef<str>>(&self, text: T) { | ||
| if Level::Debug <= self.verbosity { | ||
| eprintln!( | ||
| "{}", | ||
| text.as_ref().if_supports_color(Stderr, |text| text.bold()) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[allow(dead_code)] | ||
| pub fn display_info<T: AsRef<str>>(&self, text: T) { | ||
| if Level::Info <= self.verbosity { | ||
| eprintln!( | ||
| "{}", | ||
| text.as_ref().if_supports_color(Stderr, |text| text.bold()) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[allow(dead_code)] | ||
| pub fn display_success<T: AsRef<str>>(&self, text: T) { | ||
| if Level::Info <= self.verbosity { | ||
| eprintln!( | ||
| "{}", | ||
| text.as_ref() | ||
| .if_supports_color(Stderr, |text| text.bright_cyan()) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[allow(dead_code)] | ||
| pub fn display_waiting<T: AsRef<str>>(&self, text: T) { | ||
| if Level::Info <= self.verbosity { | ||
| eprintln!( | ||
| "{}", | ||
| text.as_ref() | ||
| .if_supports_color(Stderr, |text| text.bright_magenta()) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[allow(dead_code)] | ||
| pub fn display_warning<T: AsRef<str>>(&self, text: T) { | ||
| if Level::Warn <= self.verbosity { | ||
| eprintln!( | ||
| "{}", | ||
| text.as_ref() | ||
| .if_supports_color(Stderr, |text| text.bright_yellow()) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| pub fn display_error<T: AsRef<str>>(&self, text: T) { | ||
| if Level::Error <= self.verbosity { | ||
| eprintln!( | ||
| "{}", | ||
| text.as_ref() | ||
| .if_supports_color(Stderr, |text| text.bright_red()) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| use anyhow::Result; | ||
| use clap::Args; | ||
|
|
||
| use crate::app::Application; | ||
|
|
||
| /// Build Vector | ||
| #[derive(Args, Debug)] | ||
| #[command()] | ||
| pub struct Cli { | ||
| /// The build target e.g. x86_64-unknown-linux-musl | ||
| target: Option<String>, | ||
|
|
||
| /// Build with optimizations | ||
| #[arg(short, long)] | ||
| release: bool, | ||
|
|
||
| /// The feature to activate (multiple allowed) | ||
| #[arg(short = 'F', long)] | ||
| feature: Vec<String>, | ||
| } | ||
|
|
||
| impl Cli { | ||
| pub fn exec(&self, app: &Application) -> Result<()> { | ||
| let mut command = app.repo.command("cargo"); | ||
| command.args(["build", "--no-default-features"]); | ||
|
|
||
| if self.release { | ||
| command.arg("--release"); | ||
| } | ||
|
|
||
| command.arg("--features"); | ||
| if !self.feature.is_empty() { | ||
| command.args([self.feature.join(",")]); | ||
| } else { | ||
| if app.platform.windows() { | ||
| command.arg("default-msvc"); | ||
| } else { | ||
| command.arg("default"); | ||
| } | ||
| }; | ||
|
Comment on lines
+30
to
+41
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may also find
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah nice, I will incorporate that soon |
||
|
|
||
| if let Some(target) = self.target.as_deref() { | ||
| command.args(["--target", target]); | ||
| } else { | ||
| command.args(["--target", &app.platform.default_target()]); | ||
| }; | ||
|
|
||
| let status = command.status()?; | ||
| if !status.success() { | ||
| app.abort(format!("failed with exit code: {status}")); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| use anyhow::Result; | ||
| use clap::{Parser, Subcommand}; | ||
| use clap_verbosity_flag::{InfoLevel, Verbosity}; | ||
|
|
||
| use crate::app::Application; | ||
| use crate::commands; | ||
|
|
||
| /// Vector's unified dev tool | ||
| #[derive(Parser, Debug)] | ||
| #[ | ||
| command( | ||
| bin_name = "vdev", | ||
| author, | ||
| version, | ||
| about, | ||
| disable_help_subcommand = true, | ||
| long_about = None, | ||
| ) | ||
| ] | ||
| pub struct Cli { | ||
| #[clap(flatten)] | ||
| pub(crate) verbose: Verbosity<InfoLevel>, | ||
|
|
||
| #[command(subcommand)] | ||
| command: Commands, | ||
| } | ||
|
|
||
| #[derive(Subcommand, Debug)] | ||
| enum Commands { | ||
| /// Build Vector | ||
| Build(commands::build::Cli), | ||
| /// Manage the config file | ||
| Config(commands::config::cli::Cli), | ||
| /// Execute a command within the repository | ||
| Exec(commands::exec::Cli), | ||
| /// Collection of useful utilities | ||
| Meta(commands::meta::cli::Cli), | ||
| /// Show information about the current environment | ||
| Status(commands::status::Cli), | ||
| } | ||
|
|
||
| impl Cli { | ||
| pub fn exec(&self, app: &Application) -> Result<()> { | ||
| match &self.command { | ||
| Commands::Build(cli) => cli.exec(&app), | ||
| Commands::Config(cli) => cli.exec(&app), | ||
| Commands::Exec(cli) => cli.exec(&app), | ||
| Commands::Meta(cli) => cli.exec(&app), | ||
| Commands::Status(cli) => cli.exec(&app), | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| use anyhow::Result; | ||
| use clap::{Args, Subcommand}; | ||
|
|
||
| use crate::app::Application; | ||
| use crate::commands; | ||
|
|
||
| /// Manage the config file | ||
|
ofek marked this conversation as resolved.
Outdated
|
||
| #[derive(Args, Debug)] | ||
| #[command()] | ||
| pub struct Cli { | ||
| #[command(subcommand)] | ||
| command: Commands, | ||
| } | ||
|
|
||
| #[derive(Subcommand, Debug)] | ||
| enum Commands { | ||
| /// Locate the config file | ||
| Find(commands::config::find::Cli), | ||
| /// Modify the config file | ||
| Set(commands::config::set::cli::Cli), | ||
| } | ||
|
|
||
| impl Cli { | ||
| pub fn exec(&self, app: &Application) -> Result<()> { | ||
| match &self.command { | ||
| Commands::Find(cli) => cli.exec(&app), | ||
| Commands::Set(cli) => cli.exec(&app), | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| use anyhow::Result; | ||
| use clap::Args; | ||
|
|
||
| use crate::app::Application; | ||
|
|
||
| /// Locate the config file | ||
| #[derive(Args, Debug)] | ||
| #[command()] | ||
| pub struct Cli {} | ||
|
|
||
| impl Cli { | ||
| pub fn exec(&self, app: &Application) -> Result<()> { | ||
| app.display(format!("{}", app.config_file.path().display())); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub mod cli; | ||
| pub mod find; | ||
| pub mod set; |
Uh oh!
There was an error while loading. Please reload this page.