Skip to content

Commit

Permalink
Merge pull request #1782 from etungsten/cargo-fmt-everywhere
Browse files Browse the repository at this point in the history
actions-workflow: add check for formatting
  • Loading branch information
etungsten authored Oct 25, 2021
2 parents e443386 + 0bf24fc commit e8b9038
Show file tree
Hide file tree
Showing 29 changed files with 157 additions and 89 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ jobs:
- run: rustup toolchain install 1.53.0 && rustup default 1.53.0
- run: cargo install --version 0.30.0 cargo-make
- run: cargo make -e BUILDSYS_VARIANT=${{ matrix.variant }} unit-tests
- run: cargo make -e BUILDSYS_VARIANT=${{ matrix.variant }} check-fmt
- run: cargo make -e BUILDSYS_VARIANT=${{ matrix.variant }} -e BUILDSYS_ARCH=${{ matrix.arch }} -e BUILDSYS_JOBS=12
39 changes: 39 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,45 @@ ${BUILDSYS_TOOLS_DIR}/docker-go \
'''
]

[tasks.check-fmt]
script = [
'''
rc=0
# For golang first-party source code
unformatted_files=$(${BUILDSYS_TOOLS_DIR}/docker-go \
--module-path ${BUILDSYS_SOURCES_DIR}/host-ctr \
--sdk-image ${BUILDSYS_SDK_IMAGE} \
--go-mod-cache ${GO_MOD_CACHE} \
--command "gofmt -l cmd/host-ctr")
if [ -n "${unformatted_files}" ]; then
echo "${unformatted_files}"
rc=1
fi
# For rust first-party source code
if ! cargo fmt \
--manifest-path ${BUILDSYS_SOURCES_DIR}/Cargo.toml \
--message-format short \
--all \
-- --check; then
rc=1
fi
if ! cargo fmt \
--manifest-path ${BUILDSYS_TOOLS_DIR}/Cargo.toml \
--message-format short \
--all \
-- --check; then
rc=1
fi
if [ "${rc}" -ne 0 ]; then
echo "Found unformatted source files listed above. First-party source code is checked with gofmt and rustfmt." >&2
exit $rc
fi
'''
]

[tasks.build-tools]
dependencies = ["fetch"]
script = [
Expand Down
2 changes: 1 addition & 1 deletion sources/api/apiclient/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// to the API, which is intended to be reusable by other crates.

use apiclient::{apply, reboot, set, update};
use constants;
use datastore::{serialize_scalar, Key, KeyType};
use log::{info, log_enabled, trace, warn};
use simplelog::{
Expand All @@ -18,7 +19,6 @@ use std::env;
use std::process;
use std::str::FromStr;
use unindent::unindent;
use constants;

const DEFAULT_METHOD: &str = "GET";

Expand Down
4 changes: 2 additions & 2 deletions sources/api/apiserver/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ mod error;
pub use error::Error;

use actix_web::{
body::Body, error::ResponseError, web, App, FromRequest, HttpRequest,
HttpResponse, HttpServer, Responder,
body::Body, error::ResponseError, web, App, FromRequest, HttpRequest, HttpResponse, HttpServer,
Responder,
};
use bottlerocket_release::BottlerocketRelease;
use datastore::{Committed, FilesystemDataStore, Key, Value};
Expand Down
5 changes: 3 additions & 2 deletions sources/api/bootstrap-containers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ journalctl -u [email protected]
#[macro_use]
extern crate log;

use constants;
use datastore::{serialize_scalar, Key, KeyType};
use simplelog::{Config as LogConfig, LevelFilter, SimpleLogger};
use snafu::{ensure, OptionExt, ResultExt};
Expand All @@ -87,7 +88,6 @@ use std::fs;
use std::path::Path;
use std::process::{self, Command};
use std::str::FromStr;
use constants;

use model::modeled_types::{BootstrapContainerMode, Identifier};

Expand Down Expand Up @@ -144,7 +144,8 @@ fn usage() {
--mode MODE
Socket path defaults to {}",
program_name, constants::API_SOCKET,
program_name,
constants::API_SOCKET,
);
}

Expand Down
4 changes: 2 additions & 2 deletions sources/api/certdog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
extern crate log;

use argh::FromArgs;
use constants;
use simplelog::{Config as LogConfig, LevelFilter, SimpleLogger};
use snafu::ResultExt;
use std::collections::HashMap;
Expand All @@ -20,7 +21,6 @@ use std::io::{BufRead, Seek};
use std::path::Path;
use std::process;
use x509_parser;
use constants;

use model::modeled_types::Identifier;

Expand Down Expand Up @@ -295,12 +295,12 @@ type Result<T> = std::result::Result<T, error::Error>;

#[cfg(test)]
mod test_certdog {
use super::*;
use model;
use model::modeled_types::{Identifier, PemCertificateString};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fs::File;
use super::*;

static TEST_PEM: &str = include_str!("../../../models/tests/data/test-pem");

Expand Down
8 changes: 4 additions & 4 deletions sources/api/corndog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ It sets kernel-related settings, for example:

#![deny(rust_2018_idioms)]

use constants;
use log::{debug, error, info, trace, warn};
use simplelog::{Config as LogConfig, LevelFilter, SimpleLogger};
use snafu::ResultExt;
Expand All @@ -16,7 +17,6 @@ use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::string::String;
use std::{env, process};
use constants;

const SYSCTL_PATH_PREFIX: &str = "/proc/sys";
const LOCKDOWN_PATH: &str = "/sys/kernel/security/lockdown";
Expand All @@ -33,8 +33,7 @@ async fn run() -> Result<()> {
let args = parse_args(env::args());

// SimpleLogger will send errors to stderr and anything less to stdout.
SimpleLogger::init(args.log_level, LogConfig::default())
.context(error::Logger)?;
SimpleLogger::init(args.log_level, LogConfig::default()).context(error::Logger)?;

// If the user has kernel settings, apply them.
let model = get_model(args.socket_path).await?;
Expand Down Expand Up @@ -183,7 +182,8 @@ fn usage() -> ! {
--log-level trace|debug|info|warn|error
Socket path defaults to {}",
program_name, constants::API_SOCKET,
program_name,
constants::API_SOCKET,
);
process::exit(2);
}
Expand Down
7 changes: 5 additions & 2 deletions sources/api/datastore/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl MemoryDataStore {
fn dataset(&self, committed: &Committed) -> Option<&HashMap<Key, String>> {
match committed {
Committed::Live => Some(&self.live),
Committed::Pending { tx } => self.pending.get(tx)
Committed::Pending { tx } => self.pending.get(tx),
}
}

Expand Down Expand Up @@ -195,7 +195,10 @@ mod test {
let k = Key::new(KeyType::Data, "memtest").unwrap();
let v = "memvalue";
m.set_key(&k, v, &Committed::Live).unwrap();
assert_eq!(m.get_key(&k, &Committed::Live).unwrap(), Some(v.to_string()));
assert_eq!(
m.get_key(&k, &Committed::Live).unwrap(),
Some(v.to_string())
);

let mdkey = Key::new(KeyType::Meta, "testmd").unwrap();
let md = "mdval";
Expand Down
11 changes: 8 additions & 3 deletions sources/api/early-boot-config/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Currently, Amazon EC2 is supported through the IMDSv1 HTTP API. Data will be ta
#[macro_use]
extern crate log;

use constants;
use simplelog::{Config as LogConfig, LevelFilter, SimpleLogger};
use snafu::{ensure, ResultExt};
use std::fs;
use std::str::FromStr;
use std::{env, process};
use constants;

mod compression;
mod provider;
Expand Down Expand Up @@ -51,7 +51,8 @@ fn usage() -> ! {
[ --log-level trace|debug|info|warn|error ]
Socket path defaults to {}",
program_name, constants::API_SOCKET,
program_name,
constants::API_SOCKET,
);
process::exit(2);
}
Expand Down Expand Up @@ -106,7 +107,11 @@ async fn run() -> Result<()> {
info!("early-boot-config started");

info!("Retrieving platform-specific data");
let uri = &format!("{}?tx={}", constants::API_SETTINGS_URI, constants::LAUNCH_TRANSACTION);
let uri = &format!(
"{}?tx={}",
constants::API_SETTINGS_URI,
constants::LAUNCH_TRANSACTION
);
let method = "PATCH";
for settings_json in Platform
.platform_data()
Expand Down
4 changes: 3 additions & 1 deletion sources/api/early-boot-config/src/provider/vmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ impl VmwareDataProvider {

#[async_trait]
impl PlatformDataProvider for VmwareDataProvider {
async fn platform_data(&self) -> std::result::Result<Vec<SettingsJson>, Box<dyn std::error::Error>> {
async fn platform_data(
&self,
) -> std::result::Result<Vec<SettingsJson>, Box<dyn std::error::Error>> {
let mut output = Vec::new();

// Look at the CD-ROM for user data first, and then...
Expand Down
5 changes: 3 additions & 2 deletions sources/api/ecs-settings-applier/src/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ The configuration file for ECS is a JSON-formatted document with conditionally-d
embedded lists. The structure and names of fields in the document can be found
[here](https://github.com/aws/amazon-ecs-agent/blob/a250409cf5eb4ad84a7b889023f1e4d2e274b7ab/agent/config/types.go).
*/
use constants;
use log::debug;
use serde::Serialize;
use snafu::{OptionExt, ResultExt};
use std::fs;
use std::path::Path;
use std::{env, process};
use constants;

const DEFAULT_ECS_CONFIG_PATH: &str = "/etc/ecs/ecs.config.json";
const VARIANT_ATTRIBUTE_NAME: &str = "bottlerocket.variant";
Expand Down Expand Up @@ -171,7 +171,8 @@ fn usage() -> ! {
[ (-s | --socket-path) PATH ]
Socket path defaults to {}",
program_name, constants::API_SOCKET
program_name,
constants::API_SOCKET
);
process::exit(2);
}
Expand Down
39 changes: 28 additions & 11 deletions sources/api/host-containers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ It queries the API for their settings, then configures the system by:
#[macro_use]
extern crate log;

use constants;
use simplelog::{Config as LogConfig, LevelFilter, SimpleLogger};
use snafu::{ensure, OptionExt, ResultExt};
use std::collections::HashMap;
Expand All @@ -27,7 +28,6 @@ use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::{self, Command};
use std::str::FromStr;
use constants;

use model::modeled_types::Identifier;

Expand Down Expand Up @@ -311,7 +311,8 @@ fn usage() -> ! {
[ --log-level trace|debug|info|warn|error ]
Socket path defaults to {}",
program_name, constants::API_SOCKET,
program_name,
constants::API_SOCKET,
);
process::exit(2);
}
Expand Down Expand Up @@ -370,8 +371,10 @@ where
let enabled = image_details.enabled.unwrap_or(false);
let superpowered = image_details.superpowered.unwrap_or(false);

info!("Host container '{}' is enabled: {}, superpowered: {}, with source: {}",
name, enabled, superpowered, source);
info!(
"Host container '{}' is enabled: {}, superpowered: {}, with source: {}",
name, enabled, superpowered, source
);

// Create the directory regardless if user data was provided for the container
let dir = Path::new(PERSISTENT_STORAGE_BASE_DIR).join(name);
Expand Down Expand Up @@ -404,18 +407,23 @@ where
//
// We only attempt to do this only if host-containerd is active and running
if host_containerd_unit.is_active()? && !systemd_unit.is_enabled()? {
command(constants::HOST_CTR_BIN, &["clean-up", "--container-id", name])?;
command(
constants::HOST_CTR_BIN,
&["clean-up", "--container-id", name],
)?;
}

// Only start the host container if the systemd target is 'multi-user', otherwise
// it will start before the system is fully configured
match command(constants::SYSTEMCTL_BIN, &["get-default"])?.trim().as_ref() {
match command(constants::SYSTEMCTL_BIN, &["get-default"])?
.trim()
.as_ref()
{
"multi-user.target" => {
if systemd_unit.is_active()? {
debug!("Stopping and starting host container: '{}'", unit_name);
systemd_unit.try_reload_or_restart()?
}
else {
} else {
debug!("Enabling and starting container: '{}'", unit_name);
systemd_unit.enable_and_start()?
}
Expand All @@ -432,7 +440,10 @@ where
//
// We only attempt to do this only if host-containerd is active and running
if host_containerd_unit.is_active()? {
command(constants::HOST_CTR_BIN, &["clean-up", "--container-id", name])?;
command(
constants::HOST_CTR_BIN,
&["clean-up", "--container-id", name],
)?;
}
}

Expand All @@ -442,7 +453,10 @@ where
fn is_container_affected(settings: &[&str], container_name: &str) -> bool {
if settings.is_empty() {
// it means that Bottlerocket is booting - all containers need to be started
info!("Handling host container '{}' during full configuration process", container_name);
info!(
"Handling host container '{}' during full configuration process",
container_name
);
return true;
}

Expand All @@ -460,7 +474,10 @@ fn is_container_affected(settings: &[&str], container_name: &str) -> bool {
return true;
}
}
info!("Not handling host container '{}', no changed settings affect it", container_name);
info!(
"Not handling host container '{}', no changed settings affect it",
container_name
);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion sources/api/schnauzer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ If the returned value is "baz", our generated value will be "foo-baz".

#![deny(rust_2018_idioms)]

use constants;
use snafu::{ensure, OptionExt, ResultExt};
use std::collections::HashMap;
use std::string::String;
use std::{env, process};
use constants;

// Setting generators do not require dynamic socket paths at this moment.
const API_METADATA_URI_BASE: &str = "/metadata/";
Expand Down
Loading

0 comments on commit e8b9038

Please sign in to comment.