From f1b0e94865e09705ae849bc2d9e620dc1da3c7ac Mon Sep 17 00:00:00 2001 From: Sergei Blinov Date: Wed, 20 Sep 2023 19:21:16 +0200 Subject: [PATCH] Refactoring + fmt + lint (#4) * fmt + lint * update install script --- Cargo.toml | 1 + anytree-cli/src/bin/anytree.rs | 2 +- anytree-cli/src/commands/build.rs | 5 +- .../src/cargo_components/helper.rs | 5 +- anytree-utils/src/tracing.rs | 12 ++-- clippy.toml | 3 +- install.sh | 58 +++++++++++-------- 7 files changed, 50 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7233aa3..69ef58a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] +resolver = "2" members = [ # "anytree-cli", diff --git a/anytree-cli/src/bin/anytree.rs b/anytree-cli/src/bin/anytree.rs index 2808251..6c7c974 100644 --- a/anytree-cli/src/bin/anytree.rs +++ b/anytree-cli/src/bin/anytree.rs @@ -22,7 +22,7 @@ fn run() -> anyhow::Result<()> { Err(e) => { if e.to_string().starts_with("Usage") { eprintln!("{e}"); - return Ok(()) + return Ok(()); } anyhow::bail!(e); } diff --git a/anytree-cli/src/commands/build.rs b/anytree-cli/src/commands/build.rs index 3b4da0b..9aebbc6 100644 --- a/anytree-cli/src/commands/build.rs +++ b/anytree-cli/src/commands/build.rs @@ -8,8 +8,9 @@ pub fn build(sbom_path: impl AsRef, cache: Option>) -> any serde_json::from_reader(File::open(sbom_path.as_ref())?)?; let container_name = if let Some(run_dir) = cache { - // TODO: remove container with such name if it is not running otherwise throw error - //docker ps -qf "name=anytree-builder-d5badd63-06bc-4db5-bfa4-165be65d4cde" + // TODO: remove container with such name if it is not running otherwise + // throw error docker ps -qf + // "name=anytree-builder-d5badd63-06bc-4db5-bfa4-165be65d4cde" run_dir.as_ref().to_string() } else { let container_uuid = Uuid::new_v4().to_string(); diff --git a/anytree-plugin-cargo-dependencies/src/cargo_components/helper.rs b/anytree-plugin-cargo-dependencies/src/cargo_components/helper.rs index b31b563..461f3e8 100644 --- a/anytree-plugin-cargo-dependencies/src/cargo_components/helper.rs +++ b/anytree-plugin-cargo-dependencies/src/cargo_components/helper.rs @@ -1,6 +1,8 @@ use std::collections::HashMap; use std::fs::File; -use std::hash::{Hash, Hasher, SipHasher}; +#[allow(deprecated)] +use std::hash::SipHasher; +use std::hash::{Hash, Hasher}; use std::io::Write; use std::path::Path; @@ -104,6 +106,7 @@ pub enum SourceKind { // dir suffix pub fn get_suffix_hash(url: &str, kind: Option) -> String { // NOTE: SipHasher is deprecated, but cargo seems to check this hash !!!! + #[allow(deprecated)] let mut hasher = SipHasher::new(); if let Some(kind) = kind { kind.hash(&mut hasher); diff --git a/anytree-utils/src/tracing.rs b/anytree-utils/src/tracing.rs index 2244d1d..c61ce99 100644 --- a/anytree-utils/src/tracing.rs +++ b/anytree-utils/src/tracing.rs @@ -1,12 +1,12 @@ use std::io::{BufRead, BufReader}; + use indicatif::ProgressStyle; use tracing::Span; -use tracing_indicatif::IndicatifLayer; use tracing_indicatif::span_ext::IndicatifSpanExt; +use tracing_indicatif::IndicatifLayer; use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::util::SubscriberInitExt; - -use tracing_subscriber::{EnvFilter, Layer}; +use tracing_subscriber::EnvFilter; pub fn default_init() { let indicatif_layer = IndicatifLayer::new(); @@ -21,7 +21,7 @@ pub fn default_init() { .with_ansi(true) .with_thread_ids(true) .with_source_location(false), - ) + ), ) .with(EnvFilter::new(directives)) .with(indicatif_layer) @@ -36,7 +36,7 @@ pub fn default_init() { .with_ansi(true) .with_thread_ids(true) .with_source_location(false), - ) + ), ) .with(EnvFilter::new("info")) .with(indicatif_layer) @@ -72,4 +72,4 @@ pub fn start_progress(length: u64) -> Span { pub fn increase_progress() { Span::current().pb_inc(1); -} \ No newline at end of file +} diff --git a/clippy.toml b/clippy.toml index 482817a..75120de 100644 --- a/clippy.toml +++ b/clippy.toml @@ -19,7 +19,8 @@ disallowed-methods = [ # but we cannot disable them all here (because of e.g. https://github.com/rust-lang/rust-clippy/issues/10406) # so we do that in `clipppy_wasm.toml` instead. "std::thread::spawn", # Use `std::thread::Builder` and name the thread - "sha1::Digest::new", # SHA1 is cryptographically broken + ## INFO: in anytree project we have to use this algo anyway + # "sha1::Digest::new", # SHA1 is cryptographically broken "std::panic::catch_unwind", # We compile with `panic = "abort"` ] diff --git a/install.sh b/install.sh index 533d5d5..4d37cfb 100755 --- a/install.sh +++ b/install.sh @@ -2,9 +2,10 @@ set -e -REPO_OWNER=gosh-sh -REPO=anytree -BIN_NAME=anytree +: "${REPO_OWNER:=gosh-sh}" +: "${REPO:=anytree}" +: "${BIN_NAME:=anytree}" + if [[ -z "${TAG}" ]]; then echo "" echo "Downloading latest release of GOSH Anytree" @@ -18,29 +19,38 @@ else fi # Check OS and architecture -if [[ "$OSTYPE" == "linux-gnu" ]]; then - if [[ $(uname -m) == "x86_64" ]]; then - TAR="$BIN_NAME-linux-amd64.tar.gz" - else - TAR="$BIN_NAME-linux-arm64.tar.gz" - fi -else - echo "Only \"Linux\" is supported - not \"$OSTYPE\"" +case "$OSTYPE" in + "linux"*) + case "$(uname -m)" in + "x86_64") TAR="$BIN_NAME"-linux-amd64.tar.gz ;; + *) TAR="$BIN_NAME"-linux-arm64.tar.gz ;; + esac + ;; + "darwin"*) + case "$(uname -m)" in + "x86_64") TAR="$BIN_NAME"-darwin-amd64.tar.gz ;; + *) TAR="$BIN_NAME"-darwin-arm64.tar.gz ;; + esac + ;; + *) + echo "Only \"Linux\" and \"Darwin\" are supported" exit 1 -fi + ;; +esac + OLD_TAR="${TAR%.*}" TEMP_DIR="gosh_tmp" -[ -f $TAR ] && rm $TAR -[ -d $OLD_TAR ] && rm -r $OLD_TAR -[ -d $TEMP_DIR ] && rm -r $TEMP_DIR +[[ -f $TAR ]] && rm "$TAR" +[[ -d $OLD_TAR ]] && rm -r "$OLD_TAR" +[[ -d $TEMP_DIR ]] && rm -r "$TEMP_DIR" GH_API="https://api.github.com" GH_REPO="$GH_API/repos/${REPO_OWNER}/${REPO}" GH_TAGS="$GH_REPO/releases/$TAG" # Read asset tags. -response=$(curl -s "$GH_TAGS") +response=$(wget -qO- "$GH_TAGS") # Get ID of the asset based on the given name. eval $(echo "$response" | grep -C3 "name.:.\+$TAR" | grep -w id | tr : = | tr -cd '[[:alnum:]]=') @@ -60,22 +70,20 @@ mkdir $TEMP_DIR # Unpack the downloaded tar archive to the temporary directory if [[ -z "${OLD}" ]]; then - tar -xvzf $TAR -C $TEMP_DIR - rm -f $TAR + tar -xvzf "$TAR" -C $TEMP_DIR + rm -f "$TAR" else - tar -xf $OLD_TAR -C $TEMP_DIR - rm -f $OLD_TAR + tar -xf "$OLD_TAR" -C $TEMP_DIR + rm -f "$OLD_TAR" fi DEFAULT_PATH=$HOME/.gosh/ BINARY_PATH="${BINARY_PATH:-$DEFAULT_PATH}" -if [ ! -d $BINARY_PATH ]; then - mkdir -p $BINARY_PATH -fi +mkdir -p "$BINARY_PATH" # Move the contents from the temporary directory to the desired installation path -mv $TEMP_DIR/$BIN_NAME $BINARY_PATH +mv $TEMP_DIR/"$BIN_NAME" "$BINARY_PATH" echo "" echo "Binary was installed to $BINARY_PATH" @@ -89,4 +97,4 @@ if [ $ALREADY_ADDED -lt 1 ]; then fi # Remove the temporary directory -rm -r $TEMP_DIR \ No newline at end of file +rm -r $TEMP_DIR