Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

6 changed files with 181 additions and 133 deletions

View File

@ -1,9 +0,0 @@
---
kind: pipeline
name: shellcheck
steps:
- name: shellcheck
image: koalaman/shellcheck-alpine:stable
commands:
- shellcheck pb

View File

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

View File

@ -1,4 +1,4 @@
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 ![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** is a helper utility for using 0x0 pastebin services **pb** is a helper utility for using 0x0 pastebin services
@ -33,50 +33,33 @@ Upload a file to a different pastebin endpoint
pb -s http://0x0.st scores.txt pb -s http://0x0.st scores.txt
``` ```
Re-upload an image from the web Shorten a URL
```bash ```bash
curl -s https://tildegit.org/_/static/img/gitea-lg.png | pb -e "png" pb -u https://google.com
``` ```
### Options ### Options
```bash ```bash
-h | --help) Show this help -h Show this help
-v | --version) Show current version number -v Show current version number
-f | --file) Explicitly interpret stdin as filename -f Explicitly interpret stdin as filename
-c | --color) Pretty color output -c Pretty color output
-s | --server server_address) Use alternative pastebin server address -u Shorten URL
-e | --extension bin_extension) Specify file extension used in the upload -s server_address Use alternative pastebin server address
``` ```
### Install ### Install
On GNU systems: `sudo make install`
```sh _Note: On systems without admin access the binary can be run directly from the
sudo make install git repo, but will lack `man` support and command completion._
```
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 ### Uninstall
```sh `sudo make uninstall`
sudo make uninstall
```
On BSD systems:
```sh
doas make PREFIX=/usr uninstall
```
## Contributing ## Contributing

144
pb
View File

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

27
pb.1
View File

@ -1,11 +1,11 @@
.TH PB 1 "04 December 2023" "v2023.12.04" .TH PB 1 "20 January 2020" "v2020.01.20"
.SH NAME .SH NAME
pb \- a helper utility for using 0x0 pastebin services pb \- a helper utility for using 0x0 pastebin services
.SH SYNOPSIS .SH SYNOPSIS
.B pb .B pb
[-fucvh] [-s server_endpoint] [-e bin_extension] [-fucvh] [-s server_endpoint]
.P .P
.SH DESRIPTION .SH DESRIPTION
@ -16,8 +16,8 @@ comes pre-configured with a specific pastebin, the
service endpoint can be overridden. service endpoint can be overridden.
Data input can be provided as an argument or via stdin. Data input can be provided as an argument or via stdin.
The data will be processed as binary or text The data will be processed as an image, text or URL to
based on the context. be shortened based on the context.
The options are as follows: The options are as follows:
@ -28,10 +28,8 @@ Explicitly interpret stdin as filename or names.
.BI -s " server_address" .BI -s " server_address"
Use alternative pastebin server address. Use alternative pastebin server address.
.TP .TP
.BI -e " bin_extension" .B -u
Specifes the file extension used in the upload of binary content passed to Shorten a URL.
.B pb
via standard input.
.TP .TP
.B -c .B -c
Pretty color output. Pretty color output.
@ -64,13 +62,18 @@ Upload a list of files to the pastebin individually
.B pb -s http://0x0.st scores.txt .B pb -s http://0x0.st scores.txt
Upload a file to a different pastebin endpoint Upload a file to a different pastebin endpoint
.TP .TP
.B curl -s https://some/image/file.png | pb -e "png" .B pb -u 'https://tilde.team'
Download a binary file and re-upload it to the pastebin with an explicit binary Shorten the URL to tilde.team
type and extension.
.SH BUGS .SH BUGS
Report issues at the git repository at Report issues at the git repository at
.B https://tildegit.org/tomasino/pb .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 .SH AUTHOR
James Tomasino <james (at) tomasino (dot) org> James Tomasino <tomasino (at) lavabit (dot) com>

28
pb.d Normal file
View File

@ -0,0 +1,28 @@
#!/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