merge upstream

Signed-off-by: creme <creme@envs.net>
This commit is contained in:
creme 2022-11-26 21:44:45 +01:00
commit d29380b086
Signed by: creme
GPG Key ID: C147C3B7FBDF08D0
5 changed files with 122 additions and 175 deletions

View File

@ -1,42 +1,29 @@
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man
# Install to /usr/local unless otherwise specified, such as `make PREFIX=/app`
PREFIX?=/usr/local
# Attempt to find bash completion dir in order of preference
ifneq ($(wildcard /etc/bash_completion.d/.),)
CPLDIR ?= /etc/bash_completion.d
endif
# What to run to install various files
INSTALL?=install
# Run to install the actual binary
INSTALL_PROGRAM=$(INSTALL) -Dm 755
# Run to install application data, with differing permissions
INSTALL_DATA=$(INSTALL) -Dm 644
HAS_BREW := $(shell command -v brew 2> /dev/null)
ifdef HAS_BREW
CPLDIR ?= $$(brew --prefix)/etc/bash_completion.d
endif
# Directories into which to install the various files
bindir=$(DESTDIR)$(PREFIX)/bin
sharedir=$(DESTDIR)$(PREFIX)/share
HAS_PKGCONFIG := $(shell command -v pkg-config 2> /dev/null)
ifdef HAS_PKGCONFIG
CPLDIR ?= $$(pkg-config --variable=completionsdir bash-completion 2> /dev/null)
endif
help:
@echo "targets:"
@awk -F '#' '/^[a-zA-Z0-9_-]+:.*?#/ { print $0 }' $(MAKEFILE_LIST) \
| sed -n 's/^\(.*\): \(.*\)#\(.*\)/ \1|-\3/p' \
| column -t -s '|'
install:
@echo Installing the executable to $(BINDIR)
@install -D -m 0755 pb $(BINDIR)/pb
@echo Installing the manual page to $(MANDIR)/man1
@install -D -m 0644 pb.1 $(MANDIR)/man1/pb.1
ifeq ($(CPLDIR),)
@echo Installing the command completion to $(CPLDIR)
@mkdir -p $(CPLDIR)
@cp -f pb.d $(CPLDIR)/pb
@chmod 644 $(CPLDIR)/pb
endif
install: pb pb.1 # system install
$(INSTALL_PROGRAM) pb $(bindir)/pb
$(INSTALL_DATA) pb.1 $(sharedir)/man/man1/pb.1
uninstall:
@echo Removing the executable from $(BINDIR)
@rm -f $(BINDIR)/pb
@echo Removing the manual page from $(MANDIR)/man1
@rm -f $(BINDIR)/man1/pb.1
ifeq ($(CPLDIR),)
@echo Removing the command completion from $(CPLDIR)
@rm -f $(CPLDIR)/pb
endif
uninstall: # system uninstall
rm -f $(bindir)/pb
rm -f $(sharedir)/man/man1/pb.1
.PHONY: install uninstall
.PHONY: install uninstall help

View File

@ -1,4 +1,4 @@
pb ![calver](https://img.shields.io/badge/calver-2020.01.20-22bfda.svg?style=flat-square) ![status](https://img.shields.io/badge/status-working-green.svg?style=flat-square) ![license](https://img.shields.io/badge/license-GPL3-blue.svg?style=flat-square)
pb ![calver](https://img.shields.io/badge/calver-2022.11.03-22bfda.svg?style=flat-square) [![Build Status](https://drone.tildegit.org/api/badges/tomasino/pb/status.svg)](https://drone.tildegit.org/tomasino/pb) ![license](https://img.shields.io/badge/license-GPL3-blue.svg?style=flat-square)
------
**pb** is a helper utility for using 0x0 pastebin services
@ -33,33 +33,50 @@ Upload a file to a different pastebin endpoint
pb -s http://0x0.st scores.txt
```
Shorten a URL
Re-upload an image from the web
```bash
pb -u https://google.com
curl -s https://tildegit.org/_/static/img/gitea-lg.png | pb -e "png"
```
### Options
```bash
-h Show this help
-v Show current version number
-f Explicitly interpret stdin as filename
-c Pretty color output
-u Shorten URL
-s server_address Use alternative pastebin server address
-h | --help) Show this help
-v | --version) Show current version number
-f | --file) Explicitly interpret stdin as filename
-c | --color) Pretty color output
-s | --server server_address) Use alternative pastebin server address
-e | --extension bin_extension) Specify file extension used in the upload
```
### Install
`sudo make install`
On GNU systems:
_Note: On systems without admin access the binary can be run directly from the
git repo, but will lack `man` support and command completion._
```sh
sudo make install
```
On BSD systems:
The man-path `/usr/local/share/man` is not indexed by default on openbsd. Using the `/usr` prefix works around this issue.
```sh
doas make PREFIX=/usr install
```
### Uninstall
`sudo make uninstall`
```sh
sudo make uninstall
```
On BSD systems:
```sh
doas make PREFIX=/usr uninstall
```
## Contributing

140
pb
View File

@ -1,16 +1,17 @@
#!/bin/sh
# init variables
version="v2020.01.20"
version="v2022.11.03"
ENDPOINT="https://envs.sh"
flag_options="hvcufs::x"
flag_options=":hvcfe:s:"
long_flag_options="help,version,color,file,extension:,server:"
flag_version=0
flag_help=0
flag_file=0
flag_url=0
flag_shortlist=0
flag_colors=0
flag_ext=0
data=""
EXT=""
# help message available via func
show_help() {
@ -22,12 +23,18 @@ or
Uploads a file or data to the envs.sh 0x0 paste bin
OPTIONAL FLAGS:
-h Show this help
-v Show current version number
-f Explicitly interpret stdin as filename
-c Pretty color output
-u Shorten URL
-s server_address Use alternative pastebin server address
-h | --help) Show this help
-v | --version) Show current version number
-f | --file) Explicitly interpret stdin as filename
-c | --color) Pretty color output
-s | --server server_address) Use alternative pastebin server address
-e | --extension bin_extension) Specify file extension used in the upload
END
}
show_usage() {
cat > /dev/stdout << END
usage: pb [-hfvcux] [-s server_address] filename
END
}
@ -50,60 +57,30 @@ die () {
exit "${code}"
}
# is not interactive shell, use stdin
if [ -t 0 ]; then
flag_file=1
else
data="$(cat < /dev/stdin )"
fi
# attempt to parse options or die
if ! parsed=$(getopt ${flag_options} "$@"); then
die "Invalid input" 2
if ! PARSED_ARGUMENTS=$(getopt -a -n pb -o ${flag_options} --long ${long_flag_options} -- "$@"); then
printf "pb: unknown option\\n"
show_usage
exit 2
fi
# handle options
eval set -- "${parsed}"
while true; do
# For debugging: echo "PARSED_ARGUMENTS is $PARSED_ARGUMENTS"
eval set -- "$PARSED_ARGUMENTS"
while :
do
case "$1" in
-h)
flag_help=1
;;
-v)
flag_version=1
;;
-c)
flag_colors=1
;;
-f)
flag_file=1
;;
-s)
shift
ENDPOINT="$1"
;;
-u)
flag_url=1
;;
-x)
flag_shortlist=1
;;
--)
shift
break
;;
*)
die "Internal error: $1" 3
;;
-h | --help) flag_help=1 ; shift ;;
-v | --version) flag_version=1 ; shift ;;
-c | --color) flag_color=1 ; shift ;;
-f | --file) flag_file=1 ; shift ;;
-e | --extension) flag_ext=1; EXT="$2" ; shift 2 ;;
-s | --server) ENDPOINT="$2" ; shift 2 ;;
--) shift; break ;;
*) echo "Unexpected option: $1 - this should not happen."
show_usage ; die 3 ;;
esac
shift
done
# if data variable is empty (not a pipe) use params as fallback
if [ -z "$data" ]; then
data="$*"
fi
# display current version
if [ ${flag_version} -gt 0 ]; then
printf "%s\\n" "${version}"
@ -116,13 +93,23 @@ if [ ${flag_help} -gt 0 ]; then
die "" 0
fi
# shortlist used for bash command completion
if [ ${flag_shortlist} -gt 0 ]; then
out="-f -v -h -s -c -u"
lsresults="$(ls)"
die "${out} ${lsresults}" 0
# is not interactive shell, use stdin
if [ -t 0 ]; then
flag_file=1
else
if [ ${flag_ext} -gt 0 ]; then
# short-circuit stdin access to ensure binary data is transferred to curl
curl -sF"file=@-;filename=null.${EXT}" "${ENDPOINT}" < /dev/stdin
exit 0
else
data="$(cat < /dev/stdin )"
fi
fi
# if data variable is empty (not a pipe) use params as fallback
if [ -z "$data" ]; then
data="$*"
fi
# Colors
if [ ${flag_colors} -gt 0 ]; then
@ -135,24 +122,6 @@ else
RESET=""
fi
# URL shortening reference
# If URL mode detected, process URL shortener and end processing without
# checking for a file to upload to the pastebin
if [ ${flag_url} -gt 0 ]; then
if [ -z "${data}" ]; then
# if no data
# print error message
printf "%sProvide URL to shorten%s\\n" "$ERROR" "$RESET"
else
# shorten URL and print results
result=$(curl -sF"shorten=${data}" "${ENDPOINT}")
printf "%s%s%s\\n" "$SUCCESS" "$result" "$RESET"
fi
die "" 0
fi
if [ ${flag_file} -gt 0 ]; then
# file mode
if [ -z "${data}" ]; then
@ -171,8 +140,13 @@ if [ ${flag_file} -gt 0 ]; then
fi
# check if file exists
if [ -f "${f}" ]; then
# send file to endpoint
result=$(curl -sF"file=@${f}" "${ENDPOINT}")
if [ ${flag_ext} -gt 0 ]; then
# send file to endpoint masked with new extension
result=$(curl -sF"file=@${f};filename=null.${EXT}" "${ENDPOINT}")
else
# send file to endpoint
result=$(curl -sF"file=@${f}" "${ENDPOINT}")
fi
printf "%s%s%s\\n" "$SUCCESS" "$result" "$RESET"
else
# print error message
@ -193,7 +167,7 @@ else
printf "%sNo data found for upload. Please try again.%s\\n" "$ERROR" "$RESET"
else
# data available
# send data to endpoint, print short url
# send data to endpoint
result=$(printf "%s" "${data}" | curl -sF"file=@-;filename=null.txt" "${ENDPOINT}")
printf "%s%s%s\\n" "$SUCCESS" "$result" "$RESET"
fi

27
pb.1
View File

@ -1,11 +1,11 @@
.TH PB 1 "20 January 2020" "v2020.01.20"
.TH PB 1 "03 November 2022" "v2022.11.03"
.SH NAME
pb \- a helper utility for using 0x0 pastebin services
.SH SYNOPSIS
.B pb
[-fucvh] [-s server_endpoint]
[-fucvh] [-s server_endpoint] [-e bin_extension]
.P
.SH DESRIPTION
@ -16,8 +16,8 @@ comes pre-configured with a specific pastebin, the
service endpoint can be overridden.
Data input can be provided as an argument or via stdin.
The data will be processed as an image, text or URL to
be shortened based on the context.
The data will be processed as binary or text
based on the context.
Default Server:
.B https://envs.sh
@ -31,8 +31,10 @@ Explicitly interpret stdin as filename or names.
.BI -s " server_address"
Use alternative pastebin server address.
.TP
.B -u
Shorten a URL.
.BI -e " bin_extension"
Specifes the file extension used in the upload of binary content passed to
.B pb
via standard input.
.TP
.B -c
Pretty color output.
@ -65,18 +67,13 @@ Upload a list of files to the pastebin individually
.B pb -s http://0x0.st scores.txt
Upload a file to a different pastebin endpoint
.TP
.B pb -u 'https://duckduckgo.com'
Shorten the URL to envs.sh
.B curl -s https://some/image/file.png | pb -e "png"
Download a binary file and re-upload it to the pastebin with an explicit binary
type and extension.
.SH BUGS
Report issues at the git repository at
.B https://tildegit.org/tomasino/pb
.SH STANDARDS
The
.B pb
utility is compliant with the IEEE Std 1003.1-2008 ("POSIX.1")
specification.
.SH AUTHOR
James Tomasino <tomasino (at) lavabit (dot) com>
James Tomasino <james (at) tomasino (dot) org>

28
pb.d
View File

@ -1,28 +0,0 @@
#!/usr/bin/env bash
_pb() {
# Get basic autocomplete commands from the function itself
local helplist
helplist=$(pb -x)
# Combine all the lists for autocomplete
local cur
cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $( compgen -W "$helplist" -- "$cur" ) )
}
# Detect if current shell is ZSH, and if so, load this file in bash
# compatibility mode.
if [ -n "$ZSH_VERSION" ]; then
autoload bashcompinit
bashcompinit
fi
complete -o default -o nospace -F _pb pb
# The following are necessary only for Cygwin, and only are needed
# when the user has tab-completed the executable name and consequently
# included the '.exe' suffix.
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
complete -o default -o nospace -F _pb pb.exe
fi