Skip to content

Commit

Permalink
Add rustfmt to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Oct 10, 2024
1 parent 05a7f39 commit 78a9f91
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:

- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
components: clippy,rustfmt

- uses: Swatinem/rust-cache@v2

Expand Down
3 changes: 3 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
reorder_imports = true
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ esac

cargo generate --path "${TEMPLATE_DIR}" -n test -d program_type="${PROG_TYPE}" ${ADDITIONAL_ARGS}
pushd test
cargo fmt --all -- --check
cargo build --package test
cargo build --package test --release
# We cannot run clippy over the whole workspace at once due to feature unification. Since both test
Expand Down
1 change: 0 additions & 1 deletion {{project-name}}-ebpf/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ use aya_ebpf::{
programs::SkMsgContext,
};
use aya_log_ebpf::info;

use {{crate_name}}_common::SockKey;

#[map]
Expand Down
20 changes: 11 additions & 9 deletions {{project-name}}/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ use aya::programs::UProbe;
{%- when "sock_ops" -%}
use aya::programs::{SockOps, links::CgroupAttachMode};
{%- when "sk_msg" -%}
use aya::maps::SockHash;
use aya::programs::SkMsg;
use aya::{maps::SockHash, programs::SkMsg};
use {{crate_name}}_common::SockKey;
{%- when "xdp" -%}
use anyhow::Context;
use anyhow::Context as _;
use aya::programs::{Xdp, XdpFlags};
{%- when "classifier" -%}
use aya::programs::{tc, SchedClassifier, TcAttachType};
Expand All @@ -39,12 +38,12 @@ use aya::programs::SocketFilter;
{%- when "raw_tracepoint" -%}
use aya::programs::RawTracePoint;
{%- endcase %}
use aya::{include_bytes_aligned, Ebpf};
use aya_log::EbpfLogger;
{% if program_types_with_opts contains program_type -%}
use clap::Parser;
{% endif -%}
use log::{info, warn, debug};

#[rustfmt::skip]
use log::{debug, info, warn};
use tokio::signal;

{% if program_types_with_opts contains program_type -%}
Expand Down Expand Up @@ -85,8 +84,11 @@ async fn main() -> Result<(), anyhow::Error> {
// runtime. This approach is recommended for most real-world use cases. If you would
// like to specify the eBPF program at runtime rather than at compile-time, you can
// reach for `Bpf::load_file` instead.
let mut ebpf = Ebpf::load(include_bytes_aligned!(concat!(env!("OUT_DIR"), "/{{project-name}}")))?;
if let Err(e) = EbpfLogger::init(&mut ebpf) {
let mut ebpf = aya::Ebpf::load(aya::include_bytes_aligned!(concat!(
env!("OUT_DIR"),
"/test"
)))?;
if let Err(e) = aya_log::EbpfLogger::init(&mut ebpf) {
// This can happen if you remove all log statements from your eBPF program.
warn!("failed to initialize eBPF logger: {}", e);
}
Expand Down Expand Up @@ -115,7 +117,7 @@ async fn main() -> Result<(), anyhow::Error> {
program.load()?;
program.attach(cgroup, CgroupAttachMode::default())?;
{%- when "sk_msg" -%}
let sock_map: SockHash::<_, SockKey> = ebpf.map("{{sock_map}}").unwrap().try_into()?;
let sock_map: SockHash<_, SockKey> = ebpf.map("{{sock_map}}").unwrap().try_into()?;
let map_fd = sock_map.fd().try_clone()?;

let prog: &mut SkMsg = ebpf.program_mut("{{crate_name}}").unwrap().try_into()?;
Expand Down

0 comments on commit 78a9f91

Please sign in to comment.