Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
692 changes: 692 additions & 0 deletions vdev/Cargo.lock

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions vdev/Cargo.toml
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]
58 changes: 58 additions & 0 deletions vdev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# vdev

Comment thread
ofek marked this conversation as resolved.
-----

This is the command line tooling for Vector development.

Table of Contents:

- [Installation](#installation)
- [Configuration](#configuration)
- [Repository](#repository)
- [Starship](#starship)

## Installation

Comment thread
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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"]
```
134 changes: 134 additions & 0 deletions vdev/src/app.rs
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())
);
}
}
Comment thread
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())
);
}
}
}
55 changes: 55 additions & 0 deletions vdev/src/commands/build.rs
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may also find script/features interesting, to pull the required features out of a config file based on the components that are in use.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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(())
}
}
52 changes: 52 additions & 0 deletions vdev/src/commands/cli.rs
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),
}
}
}
30 changes: 30 additions & 0 deletions vdev/src/commands/config/cli.rs
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
Comment thread
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),
}
}
}
17 changes: 17 additions & 0 deletions vdev/src/commands/config/find.rs
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(())
}
}
3 changes: 3 additions & 0 deletions vdev/src/commands/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod cli;
pub mod find;
pub mod set;
Loading