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
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
# Attempt to find bash completion dir in order of preference
ifneq ($(wildcard /etc/bash_completion.d/.),)
CPLDIR ?= /etc/bash_completion.d
endif
# Directories into which to install the various files
bindir=$(DESTDIR)$(PREFIX)/bin
sharedir=$(DESTDIR)$(PREFIX)/share
HAS_BREW := $(shell command -v brew 2> /dev/null)
ifdef HAS_BREW
CPLDIR ?= $$(brew --prefix)/etc/bash_completion.d
endif
help:
@echo "targets:"
@awk -F '#' '/^[a-zA-Z0-9_-]+:.*?#/ { print $0 }' $(MAKEFILE_LIST) \
| sed -n 's/^\(.*\): \(.*\)#\(.*\)/ \1|-\3/p' \
| column -t -s '|'
HAS_PKGCONFIG := $(shell command -v pkg-config 2> /dev/null)
ifdef HAS_PKGCONFIG
CPLDIR ?= $$(pkg-config --variable=completionsdir bash-completion 2> /dev/null)
endif
install: pb pb.1 # system install
$(INSTALL_PROGRAM) pb $(bindir)/pb
$(INSTALL_DATA) pb.1 $(sharedir)/man/man1/pb.1
install:
@echo Installing the executable to $(BINDIR)
@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
rm -f $(bindir)/pb
rm -f $(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
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
@ -33,50 +33,33 @@ Upload a file to a different pastebin endpoint
pb -s http://0x0.st scores.txt
```
Re-upload an image from the web
Shorten a URL
```bash
curl -s https://tildegit.org/_/static/img/gitea-lg.png | pb -e "png"
pb -u https://google.com
```
### Options
```bash
-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
-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
```
### Install
On GNU systems:
`sudo make install`
```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
```
_Note: On systems without admin access the binary can be run directly from the
git repo, but will lack `man` support and command completion._
### Uninstall
```sh
sudo make uninstall
```
On BSD systems:
```sh
doas make PREFIX=/usr uninstall
```
`sudo make uninstall`
## Contributing

144
pb
View File

@ -1,17 +1,16 @@
#!/bin/sh
# init variables
version="v2023.12.04"
ENDPOINT="https://envs.sh"
flag_options=":hvcfe:s:"
long_flag_options="help,version,color,file,extension:,server:"
version="v2020.01.20"
ENDPOINT="https://ttm.sh"
flag_options="hvcufs::x"
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() {
@ -23,18 +22,12 @@ or
Uploads a file or data to the tilde 0x0 paste bin
OPTIONAL FLAGS:
-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
-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
END
}
@ -57,30 +50,60 @@ die () {
exit "${code}"
}
# attempt to parse options or die
if ! PARSED_ARGUMENTS=$(getopt -a -n pb -o ${flag_options} --long ${long_flag_options} -- "$@"); then
printf "pb: unknown option\\n"
show_usage
exit 2
# is not interactive shell, use stdin
if [ -t 0 ]; then
flag_file=1
else
data="$(cat < /dev/stdin )"
fi
# For debugging: echo "PARSED_ARGUMENTS is $PARSED_ARGUMENTS"
eval set -- "$PARSED_ARGUMENTS"
while :
do
# attempt to parse options or die
if ! parsed=$(getopt ${flag_options} "$@"); then
die "Invalid input" 2
fi
# handle options
eval set -- "${parsed}"
while true; do
case "$1" in
-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 ;;
-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
;;
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}"
@ -93,23 +116,13 @@ if [ ${flag_help} -gt 0 ]; then
die "" 0
fi
# 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
# 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
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
@ -122,6 +135,24 @@ 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
@ -140,13 +171,8 @@ if [ ${flag_file} -gt 0 ]; then
fi
# check if file exists
if [ -f "${f}" ]; then
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
# send file to endpoint
result=$(curl -sF"file=@${f}" "${ENDPOINT}")
printf "%s%s%s\\n" "$SUCCESS" "$result" "$RESET"
else
# print error message
@ -167,7 +193,7 @@ else
printf "%sNo data found for upload. Please try again.%s\\n" "$ERROR" "$RESET"
else
# data available
# send data to endpoint
# send data to endpoint, print short url
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 "04 December 2023" "v2023.12.04"
.TH PB 1 "20 January 2020" "v2020.01.20"
.SH NAME
pb \- a helper utility for using 0x0 pastebin services
.SH SYNOPSIS
.B pb
[-fucvh] [-s server_endpoint] [-e bin_extension]
[-fucvh] [-s server_endpoint]
.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 binary or text
based on the context.
The data will be processed as an image, text or URL to
be shortened based on the context.
The options are as follows:
@ -28,10 +28,8 @@ Explicitly interpret stdin as filename or names.
.BI -s " server_address"
Use alternative pastebin server address.
.TP
.BI -e " bin_extension"
Specifes the file extension used in the upload of binary content passed to
.B pb
via standard input.
.B -u
Shorten a URL.
.TP
.B -c
Pretty color output.
@ -64,13 +62,18 @@ 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 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.
.B pb -u 'https://tilde.team'
Shorten the URL to tilde.team
.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 <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