Skip to content
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

chore(vdev): Rewrite scripts in vdev #16661

Merged
merged 15 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ jobs:
- uses: actions/checkout@v3
- run: sudo -E bash scripts/environment/bootstrap-ubuntu-20.04.sh
- run: bash scripts/environment/prepare.sh
- run: make test-vrl
- run: cargo vdev test-vrl

test-vrl-wasm:
name: VRL WASM - Linux
Expand Down
22 changes: 0 additions & 22 deletions scripts/check-component-docs.sh

This file was deleted.

12 changes: 0 additions & 12 deletions scripts/install-git-hooks.sh

This file was deleted.

16 changes: 0 additions & 16 deletions scripts/release-github.sh

This file was deleted.

28 changes: 0 additions & 28 deletions scripts/signoff-git-hook.sh

This file was deleted.

14 changes: 0 additions & 14 deletions scripts/test-vrl.sh

This file was deleted.

31 changes: 31 additions & 0 deletions vdev/src/commands/check/component_docs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use anyhow::{Result, Ok};
use crate::git;


/// Check that component documentation is up-to-date
#[derive(clap::Args, Debug)]
#[command()]
pub struct Cli {}

impl Cli {
pub fn exec(self) -> Result<()> {
let files: Vec<String> = git::get_modified_files()?;
let dirty_component_files: Vec<String> = files
.iter()
.cloned()
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
.filter(|file| file.starts_with("website/cue/reference/components"))
.collect();

// If it is not empty, there are out-of-sync component Cue files in the current branch.
if !dirty_component_files.is_empty() {
println!("Found out-of-sync component Cue files in this branch:");
for file in dirty_component_files {
println!(" - {file}");
}
println!("Run `make generate-component-docs` locally to update your branch and commit/push the changes.");
std::process::exit(1);
}

Ok(())
}
}
7 changes: 1 addition & 6 deletions vdev/src/commands/check/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
crate::cli_subcommands! {
"Check parts of the Vector code base..."
component_docs,
mod component_docs,
mod component_features,
mod deny,
docs,
Expand All @@ -15,11 +15,6 @@ crate::cli_subcommands! {

// These should eventually be migrated to Rust code

crate::script_wrapper! {
component_docs = "Check that component documentation is up-to-date"
=> "check-component-docs.sh"
}

crate::script_wrapper! {
docs = "Check that all /docs files are valid"
=> "check-docs.sh"
Expand Down
67 changes: 67 additions & 0 deletions vdev/src/commands/meta/install_git_hooks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use anyhow::{Result, Ok};
use std::fs::File;
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
use std::path::Path;
use std::process::Command;
use crate::git;
use crate::app::CommandExt as _;
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved

const SIGNOFF_HOOK: &str = r#"#!/bin/bash
set -euo pipefail

# Automatically sign off your commits.
#
# Installation:
#
# cp scripts/signoff-git-hook.sh .git/hooks/commit-msg
#
# It's also possible to symlink the script, however that's a security hazard and
# is not recommended.

NAME="$(git config user.name)"
EMAIL="$(git config user.email)"

if [ -z "$NAME" ]; then
echo "empty git config user.name"
exit 1
fi

if [ -z "$EMAIL" ]; then
echo "empty git config user.email"
exit 1
fi

git interpret-trailers --if-exists doNothing --trailer \
"Signed-off-by: $NAME <$EMAIL>" \
--in-place "$1"

"#;

/// Install a Git commit message hook that verifies
/// that all commits have been signed off.
#[derive(clap::Args, Debug)]
#[command()]
pub struct Cli {}

impl Cli {
pub fn exec(self) -> Result<()> {
let git_dir = git::get_git_dir()?;

// Create a new directory named hooks in the .git directory if it
// doesn't already exist.
let mut command = Command::new("mkdir");
command.in_repo();
command.args(["-p", &format!("{git_dir}/hooks")]);
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved

command.check_run()?;

let hook_path = Path::new(&git_dir).join("hooks").join("commit-msg");
let mut file = File::create(hook_path)?;
file.write_all(SIGNOFF_HOOK.as_bytes())?;
file.metadata()?.permissions().set_mode(0o755);
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved
println!("Created signoff script in {git_dir}/hooks/commit-msg");

Ok(())
}
}
1 change: 1 addition & 0 deletions vdev/src/commands/meta/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
crate::cli_subcommands! {
"Collection of meta-utilities..."
mod starship,
mod install_git_hooks,
}
1 change: 1 addition & 0 deletions vdev/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ cli_commands! {
mod run,
mod status,
mod test,
mod test_vrl,
mod version,
}

Expand Down
31 changes: 31 additions & 0 deletions vdev/src/commands/release/github.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use anyhow::{Result, Ok};
use std::process::Command;
use std::env;
use crate::app::CommandExt as _;
use crate::util;

/// Uploads target/artifacts to GitHub releases
#[derive(clap::Args, Debug)]
#[command()]
pub struct Cli {
}

impl Cli {
pub fn exec(self) -> Result<()> {
let version = env::var("VECTOR_VERSION").or_else(|_| util::read_version())?;
let mut command = Command::new("gh");
command.in_repo();
command.args(["release",
"--repo",
"vectordotdev/vector",
"create",
&format!("v{version}"),
"--title",
&format!("v{version}"),
"--notes",
&format!("[View release notes](https://vector.dev/releases/{version})"),
"target/artifacts/*"]);
command.check_run()?;
Ok(())
}
}
6 changes: 1 addition & 5 deletions vdev/src/commands/release/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ crate::cli_subcommands! {
mod channel,
commit,
docker,
github,
mod github,
homebrew,
mod prepare,
push,
Expand All @@ -23,10 +23,6 @@ crate::script_wrapper! {
docker = "Build the Vector docker images and optionally push it to the registry"
=> "build-docker.sh"
}
crate::script_wrapper! {
github = "Determine the appropriate release channel (nightly or latest) based on Git HEAD"
=> "release-github.sh"
}
crate::script_wrapper! {
homebrew = "Releases latest version to the vectordotdev homebrew tap"
=> "release-homebrew.sh"
Expand Down
22 changes: 22 additions & 0 deletions vdev/src/commands/test_vrl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use anyhow::{Context as _, Result};

use crate::{app};
use std::{env, path::PathBuf};

/// Run the Vector Remap Language test suite
#[derive(clap::Args, Debug)]
#[command()]
pub struct Cli {}

impl Cli {
pub fn exec(self) -> Result<()> {
let path: PathBuf = [app::path(), "lib", "vrl", "tests"].into_iter().collect();
env::set_current_dir(path).context("Could not change directory")?;

app::exec(
"cargo",
["run"].into_iter(),
false,
)
}
}
18 changes: 18 additions & 0 deletions vdev/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ pub fn list_files() -> Result<Vec<String>> {
.collect())
}

// Get a list of files that have been modified, as a vector of strings
pub fn get_modified_files() -> Result<Vec<String>> {
let args = vec![
"ls-files",
"--full-name",
"--modified",
"--others",
"--exclude-standard",
];
Ok(capture_output(&args)?.lines().map(str::to_owned).collect())
}

// Gets path of the current Git repository's .git directory by the git rev-parse --git-dir command.
pub fn get_git_dir() -> Result<String> {
let output = capture_output(&["rev-parse", "--git-dir"])?;
Ok(output.trim_end().to_string())
}
jonathanpv marked this conversation as resolved.
Show resolved Hide resolved

fn capture_output(args: &[&str]) -> Result<String> {
Command::new("git").in_repo().args(args).capture_output()
}
Expand Down