Skip to content

Commit

Permalink
Refactoring + fmt + lint (#4)
Browse files Browse the repository at this point in the history
* fmt + lint

* update install script
  • Loading branch information
awnion authored Sep 20, 2023
1 parent 87e68d8 commit f1b0e94
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
#
"anytree-cli",
Expand Down
2 changes: 1 addition & 1 deletion anytree-cli/src/bin/anytree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions anytree-cli/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ pub fn build(sbom_path: impl AsRef<Path>, cache: Option<impl AsRef<str>>) -> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -104,6 +106,7 @@ pub enum SourceKind {
// dir suffix
pub fn get_suffix_hash(url: &str, kind: Option<SourceKind>) -> 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);
Expand Down
12 changes: 6 additions & 6 deletions anytree-utils/src/tracing.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -72,4 +72,4 @@ pub fn start_progress(length: u64) -> Span {

pub fn increase_progress() {
Span::current().pb_inc(1);
}
}
3 changes: 2 additions & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
]

Expand Down
58 changes: 33 additions & 25 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:]]=')
Expand All @@ -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"
Expand All @@ -89,4 +97,4 @@ if [ $ALREADY_ADDED -lt 1 ]; then
fi

# Remove the temporary directory
rm -r $TEMP_DIR
rm -r $TEMP_DIR

0 comments on commit f1b0e94

Please sign in to comment.