Skip to content

Commit

Permalink
setup-util-sd: sd v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Nov 10, 2023
1 parent de23d08 commit cc23edb
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 20 deletions.
1 change: 1 addition & 0 deletions commands/config-helper
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ function config_helper() (
# this will then leave the first occurance in the whole
# which we can use sd to perform our final intended replacement
# of the primary occurance
# printf '%s' "$content" | sd -n 1 "$search_pattern" 'REPLACE_THIS_LINE' | sd "$search_pattern" '' | sd $'(REPLACE_THIS_LINE\n)+' "$replace_pattern" >"$option_file" <-- this fails to handle replacement groups
printf '%s' "$content" |
teip -g "$search_pattern" -- teip -l 2- -- sed 's/.*/REMOVE_THIS_LINE/' |
sd $'(REMOVE_THIS_LINE\n)+' '' |
Expand Down
50 changes: 50 additions & 0 deletions commands/echo-nothing-or-fail
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

function echo_nothing_or_fail() (
source "$DOROTHY/sources/stdinargs.bash"

function help {
cat <<-EOF >/dev/stderr
ABOUT:
If input is provided, fail.
If no input was provided, success.
USAGE:
echo-nothing-or-fail [...options] [--] ...<input>
echo-lines ...<input> | echo-nothing-or-fail [...options]
OPTIONS:
$(stdinargs_options_help --)
EXAMPLE:
# success cases
echo-nothing-or-fail -- 'sup'
echo 'sup' | echo-nothing-or-fail --stdin
# exit status: 1
# failure cases, no stdin, no arguments
echo-nothing-or-fail --
true | echo-nothing-or-fail --stdin
# exit status: 0
EOF
return 22 # EINVAL 22 Invalid argument
}

function on_input {
return 1 # failure
}

function on_no_input {
return 0 # success
}

stdinargs "$@"
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
echo_nothing_or_fail "$@"
fi
83 changes: 83 additions & 0 deletions commands/fs-dequarantine
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash

function fs_dequaratine() (
source "$DOROTHY/sources/bash.bash"

# =====================================
# Arguments

function help {
cat <<-EOF >/dev/stderr
ABOUT:
Remove the quaratine flag from a path.
USAGE:
fs-dequarantine [--] ...<path>
EOF
if test "$#" -ne 0; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}

# process
local item option_paths=()
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--')
option_paths+=("$@")
shift $#
break
;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*) option_paths+=("$item") ;;
esac
done

# check
if test "${#option_paths[@]}" -eq 0; then
help 'No <path>s were provided.'
fi

# =====================================
# Action

if ! is-mac || command-missing xattr; then
return 0 # not needed
fi

function disable_quarantine_on_path {
local path="$1" output status cmd=('xattr')
# https://apple.stackexchange.com/a/436677/15131
# option -r not recognized
if test -d "$path"; then
cmd+=('-dr')
else
cmd+=('-d')
fi
cmd+=('com.apple.quarantine' "$path")
eval_capture --outputvar=output --statusvar=status -- "${cmd[@]}"
if test "$status" -eq 0 -o "$output" = 'No such xattr: com.apple.quarantine'; then
return 0
fi
eval_capture --outputvar=output --statusvar=status -- sudo-helper -- "${cmd[@]}"
if test "$status" -eq 0 -o "$output" = 'No such xattr: com.apple.quarantine'; then
return 0
fi
print_line "$output" >/dev/stderr
return "$status"
}

local path
for path in "${option_paths[@]}"; do
disable_quarantine_on_path "$path"
done
)

# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
fs_dequaratine "$@"
fi
2 changes: 1 addition & 1 deletion commands/fs-structure
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function fs_structure() (
else
cd "$(dirname "$path")" || return 1
basename="$(basename "$path")"
"${cmd[@]}" "$basename" | sd -s "$basename" ''
"${cmd[@]}" "$basename" | sd --fixed-strings "$basename" ''
fi
done
)
Expand Down
1 change: 1 addition & 0 deletions commands/setup-util
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ function setup_util() (

# adjust
fs-own --permissions='+x' -- "$path"
fs-dequarantine -- "$path"

# verify
if [[ $path == *'.app' ]] && test -d "$path"; then
Expand Down
42 changes: 23 additions & 19 deletions commands/setup-util-sd
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
# sed/awk alternative

# https://github.com/chmln/sd/releases
# sd-v0.7.6-x86_64-apple-darwin
# sd-v0.7.6-x86_64-unknown-linux-gnu
# sd-v0.7.6-x86_64-unknown-linux-musl

# arm64
# https://github.com/chmln/sd/issues/152
# https://github.com/yhakbar/sd/releases/tag/v0.7.7-yhakbar
# sd-v0.7.7-yhakbar-aarch64-apple-darwin
# sd-v0.7.7-yhakbar-aarch64-unknown-linux-musl
# sd-v0.7.7-yhakbar-arm-unknown-linux-gnueabihf
# sd-v0.7.7-yhakbar-x86_64-apple-darwin
# sd-v0.7.7-yhakbar-x86_64-unknown-linux-gnu
# sd-v0.7.7-yhakbar-x86_64-unknown-linux-musl
# sd-v1.0.0-aarch64-apple-darwin.tar.gz
# sd-v1.0.0-aarch64-unknown-linux-musl.tar.gz
# sd-v1.0.0-arm-unknown-linux-gnueabihf.tar.gz
# sd-v1.0.0-armv7-unknown-linux-gnueabihf.tar.gz
# sd-v1.0.0-x86_64-apple-darwin.tar.gz
# sd-v1.0.0-x86_64-pc-windows-gnu.zip
# sd-v1.0.0-x86_64-pc-windows-msvc.zip
# sd-v1.0.0-x86_64-unknown-linux-gnu.tar.gz
# sd-v1.0.0-x86_64-unknown-linux-musl.tar.gz

# https://repology.org/project/sd-find-replace/versions

Expand Down Expand Up @@ -48,27 +44,35 @@ function setup_util_sd() (
function get_github_asset_url {
github-download \
--dry \
--slug='yhakbar/sd' \
--slug='chmln/sd' \
--release='latest' \
--asset-filter="$(echo-escape-regex -- "$1")$" | echo-first-line || :
}
function add_download_option {
options+=(
DOWNLOAD="$(get_github_asset_url "$1")"
DOWNLOAD_UNZIP_FILTER="*/$2"
)
}
if is-mac; then
if test "$arch" = 'a64'; then
add_download_option '-aarch64-apple-darwin'
add_download_option '-aarch64-apple-darwin.tar.gz' 'sd'
else
add_download_option '-x86_64-apple-darwin'
add_download_option '-x86_64-apple-darwin.tar.gz' 'sd'
fi
else
elif is-linux; then
arch="$(get-arch)"
if test "$arch" = 'a64'; then
add_download_option '-aarch64-unknown-linux-musl'
add_download_option '-aarch64-unknown-linux-musl.tar.gz' 'sd'
elif test "$arch" = 'x64'; then
add_download_option '-x86_64-unknown-linux-musl'
add_download_option '-x86_64-unknown-linux-musl.tar.gz' 'sd'
elif test "$arch" = 'a32'; then
add_download_option '-armv7-unknown-linux-gnueabihf.tar.gz' 'sd'
fi
elif is-wsl; then
arch="$(get-arch)"
if test "$arch" = 'x64'; then
add_download_option '-x86_64-pc-windows-msvc.zip' 'sd.exe'
fi
fi
setup-util "${options[@]}"
Expand Down

0 comments on commit cc23edb

Please sign in to comment.