Skip to content

Commit

Permalink
refactor: merge imports from the same module into a single use
Browse files Browse the repository at this point in the history
This commits adds a imports_granularity configuration to
rustfmt.toml to merge imports from the same module into a single
use statement.

Signed-off-by: jiaxiao zhou <[email protected]>
  • Loading branch information
Mossaka committed Aug 18, 2023
1 parent da2cb4d commit 8ed5f3f
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 80 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jobs:
runs-on: "ubuntu-latest"
steps:
- uses: actions/checkout@v3
- uses: actions-rust-lang/setup-rust-toolchain@v1
- uses: Swatinem/rust-cache@v2
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt, clippy
- name: Setup build env
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fs::OpenOptions;
use std::io::Read;
use std::{fs::OpenOptions, os::fd::RawFd, path::PathBuf};
use std::os::fd::RawFd;
use std::path::PathBuf;

use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use libcontainer::workload::default::DefaultExecutor;
Expand Down
27 changes: 11 additions & 16 deletions crates/containerd-shim-wasm/src/libcontainer_instance/instance.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
//! Abstractions for running/managing a wasm/wasi instance that uses youki's libcontainer library.
use std::{path::PathBuf, thread};
use std::path::PathBuf;
use std::thread;

use anyhow::Context;
use chrono::Utc;
use libc::{SIGINT, SIGKILL};
use libcontainer::{
container::{Container, ContainerStatus},
signal::Signal,
};
use libcontainer::container::{Container, ContainerStatus};
use libcontainer::signal::Signal;
use log::error;
use nix::{
errno::Errno,
sys::wait::{waitid, Id as WaitID, WaitPidFlag, WaitStatus},
};

use crate::sandbox::InstanceConfig;
use crate::sandbox::{error::Error, instance::Wait, Instance};
use crate::sandbox::{
instance::ExitCode,
instance_utils::{get_instance_root, instance_exists},
};
use nix::errno::Errno;
use nix::sys::wait::{waitid, Id as WaitID, WaitPidFlag, WaitStatus};

use crate::sandbox::error::Error;
use crate::sandbox::instance::{ExitCode, Wait};
use crate::sandbox::instance_utils::{get_instance_root, instance_exists};
use crate::sandbox::{Instance, InstanceConfig};

/// LibcontainerInstance is a trait that gets implemented by a WASI runtime that
/// uses youki's libcontainer library as the container runtime.
Expand Down
8 changes: 3 additions & 5 deletions crates/containerd-shim-wasm/src/sandbox/instance_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! Common utilities for the containerd shims.
use std::{
fs::{self, File, OpenOptions},
io::ErrorKind,
path::{Path, PathBuf},
};
use std::fs::{self, File, OpenOptions};
use std::io::ErrorKind;
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Result};

Expand Down
21 changes: 8 additions & 13 deletions crates/containerd-shim-wasm/src/sandbox/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,24 @@ use std::env::current_dir;
use std::fs::File;
use std::os::unix::io::AsRawFd;
use std::path::Path;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::{Arc, RwLock};
use std::thread;

use anyhow::Context;
use containerd_shim::{
self as shim, api,
error::Error as ShimError,
protos::shim::shim_ttrpc::{create_task, Task},
protos::ttrpc::{Client, Server},
protos::TaskClient,
publisher::RemotePublisher,
TtrpcContext, TtrpcResult,
};
use containerd_shim::error::Error as ShimError;
use containerd_shim::protos::shim::shim_ttrpc::{create_task, Task};
use containerd_shim::protos::ttrpc::{Client, Server};
use containerd_shim::protos::TaskClient;
use containerd_shim::publisher::RemotePublisher;
use containerd_shim::{self as shim, api, TtrpcContext, TtrpcResult};
use nix::sched::{setns, unshare, CloneFlags};
use oci_spec::runtime;
use shim::Flags;
use ttrpc::context;

use super::error::Error;
use super::instance::Instance;
use super::oci;
use super::sandbox;
use super::{oci, sandbox};
use crate::services::sandbox_ttrpc::{Manager, ManagerClient};

/// Sandbox wraps an Instance and is used with the `Service` to manage multiple instances.
Expand Down
3 changes: 2 additions & 1 deletion crates/containerd-shim-wasm/src/sandbox/oci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::path::{Path, PathBuf};
use std::process;

use anyhow::Context;
use nix::{sys::signal, unistd::Pid};
use nix::sys::signal;
use nix::unistd::Pid;
pub use oci_spec::runtime::Spec;
use serde_json as json;

Expand Down
29 changes: 12 additions & 17 deletions crates/containerd-shim-wasm/src/sandbox/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
use std::collections::HashMap;
use std::env::current_dir;
use std::fs::{self, File};
use std::fs::{canonicalize, create_dir_all, OpenOptions};
use std::fs::{self, canonicalize, create_dir_all, File, OpenOptions};
use std::ops::Not;
use std::os::unix::io::AsRawFd;
use std::path::Path;
Expand All @@ -17,21 +16,17 @@ use cgroups_rs::cgroup::get_cgroups_relative_paths_by_pid;
use cgroups_rs::hierarchies::{self};
use cgroups_rs::{Cgroup, Subsystem};
use chrono::{DateTime, Utc};
use containerd_shim::{
self as shim, api,
error::Error as ShimError,
event::Event,
mount::mount_rootfs,
protos::events::task::{TaskCreate, TaskDelete, TaskExit, TaskIO, TaskStart},
protos::protobuf::well_known_types::timestamp::Timestamp,
protos::protobuf::{MessageDyn, MessageField},
protos::shim::shim_ttrpc::Task,
protos::types::task::Status,
publisher::RemotePublisher,
util::IntoOption,
util::{timestamp as new_timestamp, write_address},
warn, ExitSignal, TtrpcContext, TtrpcResult,
};
use containerd_shim::error::Error as ShimError;
use containerd_shim::event::Event;
use containerd_shim::mount::mount_rootfs;
use containerd_shim::protos::events::task::{TaskCreate, TaskDelete, TaskExit, TaskIO, TaskStart};
use containerd_shim::protos::protobuf::well_known_types::timestamp::Timestamp;
use containerd_shim::protos::protobuf::{MessageDyn, MessageField};
use containerd_shim::protos::shim::shim_ttrpc::Task;
use containerd_shim::protos::types::task::Status;
use containerd_shim::publisher::RemotePublisher;
use containerd_shim::util::{timestamp as new_timestamp, write_address, IntoOption};
use containerd_shim::{self as shim, api, warn, ExitSignal, TtrpcContext, TtrpcResult};
use log::{debug, error};
use nix::mount::{mount, MsFlags};
use nix::sched::{setns, unshare, CloneFlags};
Expand Down
9 changes: 4 additions & 5 deletions crates/containerd-shim-wasmedge/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{os::unix::io::RawFd, path::PathBuf};
use std::os::unix::io::RawFd;
use std::path::PathBuf;

use anyhow::Result;
use containerd_shim_wasm::sandbox::oci;
Expand All @@ -7,10 +8,8 @@ use libcontainer::workload::{Executor, ExecutorError};
use log::debug;
use nix::unistd::{dup, dup2};
use oci_spec::runtime::Spec;
use wasmedge_sdk::{
config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions},
params, VmBuilder,
};
use wasmedge_sdk::config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions};
use wasmedge_sdk::{params, VmBuilder};

const EXECUTOR_NAME: &str = "wasmedge";

Expand Down
14 changes: 5 additions & 9 deletions crates/containerd-shim-wasmedge/src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::io::ErrorKind;
use std::os::fd::IntoRawFd;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Condvar, Mutex};
use std::{
fs,
path::{Path, PathBuf},
};

use anyhow::Context;
use anyhow::Result;
use containerd_shim_wasm::libcontainer_instance::LibcontainerInstance;
use containerd_shim_wasm::libcontainer_instance::LinuxContainerExecutor;

use anyhow::{Context, Result};
use containerd_shim_wasm::libcontainer_instance::{LibcontainerInstance, LinuxContainerExecutor};
use containerd_shim_wasm::sandbox::error::Error;
use containerd_shim_wasm::sandbox::instance::ExitCode;
use containerd_shim_wasm::sandbox::instance_utils::maybe_open_stdio;
Expand Down
4 changes: 3 additions & 1 deletion crates/containerd-shim-wasmtime/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{fs::OpenOptions, os::fd::RawFd, path::PathBuf};
use std::fs::OpenOptions;
use std::os::fd::RawFd;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use containerd_shim_wasm::sandbox::oci;
Expand Down
6 changes: 2 additions & 4 deletions crates/containerd-shim-wasmtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::os::fd::IntoRawFd;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Condvar, Mutex};

use anyhow::Context;
use anyhow::Result;
use anyhow::{Context, Result};
use containerd_shim_wasm::libcontainer_instance::{LibcontainerInstance, LinuxContainerExecutor};
use containerd_shim_wasm::sandbox::error::Error;
use containerd_shim_wasm::sandbox::instance::ExitCode;
Expand Down Expand Up @@ -148,8 +147,7 @@ mod wasitest {
use containerd_shim_wasm::sandbox::instance::Wait;
use containerd_shim_wasm::sandbox::testutil::{has_cap_sys_admin, run_test_with_sudo};
use containerd_shim_wasm::sandbox::Instance;
use libc::SIGKILL;
use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use libc::{SIGKILL, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use nix::unistd::dup2;
use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder};
use tempfile::{tempdir, TempDir};
Expand Down
3 changes: 2 additions & 1 deletion crates/containerd-shim-wasmtime/src/oci_wasmtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::fs::OpenOptions;
use std::path::Path;

use anyhow::Context;
use containerd_shim_wasm::sandbox::{error::Error, oci};
use containerd_shim_wasm::sandbox::error::Error;
use containerd_shim_wasm::sandbox::oci;
use oci_spec::runtime::Spec;
use wasmtime_wasi::{Dir as WasiDir, WasiCtxBuilder};

Expand Down
10 changes: 6 additions & 4 deletions crates/oci-tar-builder/src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::fs::File;
use std::path::PathBuf;
use std::{env, fs};

use {
anyhow::Context, clap::Parser, oci_spec::image as spec, oci_tar_builder::Builder,
std::fs::File, std::path::PathBuf,
};
use anyhow::Context;
use clap::Parser;
use oci_spec::image as spec;
use oci_tar_builder::Builder;

pub fn main() {
let args = Args::parse();
Expand Down
6 changes: 5 additions & 1 deletion crates/wasi-demo-app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{env, fs::File, io::prelude::*, process, thread::sleep, time::Duration};
use std::fs::File;
use std::io::prelude::*;
use std::thread::sleep;
use std::time::Duration;
use std::{env, process};

fn main() {
let args: Vec<_> = env::args().collect();
Expand Down
5 changes: 4 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# create three groups for std, external and local crates
group_imports = "StdExternalCrate"
group_imports = "StdExternalCrate"

# https://rust-lang.github.io/rustfmt/?version=v1.4.38&search=#imports_granularity
imports_granularity = "Module"

0 comments on commit 8ed5f3f

Please sign in to comment.