~skiqqy/q

2d2950cc34f75310836278d4c48905c3345a0b47 — Stephen Cochrane 2 months ago 9d5eb06 master
Added q server and q sync commands
1 files changed, 85 insertions(+), 1 deletions(-)

M q
M q => q +85 -1
@@ 14,6 14,8 @@ commands[ed]="ed id\nEdit the note at id."
commands[help]="q help\nShows helpful information."
commands[list]="list\nShows all available commands."
commands[tree]="tree\nShows all available stacks."
commands[server]="server [options]\nStart a q server."
commands[sync]="sync [ pull | push ] [options]\nPull/ Push current stack to q server."

usage()
{


@@ 255,11 257,89 @@ qtree()
	tree -I id --noreport "$qdir" | tail -n +2
}

qserver()
{
	local spath
	spath="$HOME/.local/lib"
	mkdir -p "$spath"
	[ ! -d "$spath/kept-folder" ] && git clone "https://git.sr.ht/~skiqqy/kept-folder" "$spath/kept-folder"
	if [ ! -f "$qdir/config" ]
	then
		printf 'Generated config file in %s, please complete and then restart server\n.' "$qdir/config"
		cat << EOF > "$qdir/config"
nic=nickname
privkey=key
domain=http://0.0.0.0:5000/
EOF
	return
	fi

	pushd "$spath/kept-folder" > /dev/null || return 1
		printf 'Server Directory: %s\n\n' "$PWD"
		./main.sh "$@"
	popd > /dev/null || return 1
}

qsync()
{
	if [ "$1" = push ]
	then
		upload "$q"
	elif [ "$1" = pull ]
	then
		download "$q"
	fi
}

# Upload a file
# Usage: upload FILE
upload() {
	[ -z "$domain" ] && . "$qdir/config"
	route="$domain/upload/note"
	content=$(cat "$1")
	fname=$(basename "$1")
	curl -s -X POST -F "nic=$nic" -F "key=$privkey" -F "fname=$fname" -F "content=$content" "$route" > /dev/null
}

# Download a file
# Usage: download FILE
download() {
	[ ! -d "$qdir/syc" ] && mkdir -p "$qdir/sync"
	[ -z "$domain" ] && . "$qdir/config"
	route="$domain/download/note"
	fname=$(basename "$1")
	ret="$(curl -s -X POST -F "nic=$nic" -F "key=$privkey" -F "fname=$fname" "$route")"

	code=$(echo "$ret" | jq '.status')
	if [ -n "$code" ]  && [ "$code" -eq 0 ]
	then
		data=$(echo "$ret" | jq '.data')
		fname=$(echo "$data" | jq '.fname')
		content=$(echo "$data" | jq '.content')

		fname=$(sq "$fname")
		content=$(sq "$content")

		echo -en "$content" > "$qdir/$fname"
		echo "Sync for <$1> completed."
	else
		echo "Sync for <$1> failed."
		return 1
	fi
}

sq () {
	sed -e 's/^"//' -e 's/"$//' <<< "$1"
}

main()
{
	default=qpeek # Default command
	qdir="$HOME/.q" # Default location
	q="q" # Default Stack Name
	nic=
	privkey=
	domain=

	while getopts "hd:q:" opt
	do


@@ 271,7 351,11 @@ main()
				qdir="$OPTARG"
				;;
			q)
				[ "$OPTARG" = id ] && { printf 'q cannot be set to id, aborting.\n'; exit 1; }
				if [ "$OPTARG" = id ] || [ "$OPTARG" = config ]
				then
					printf 'q cannot be set to %s, aborting.\n' "$OPTARG"
					exit 1
				fi
				q="$OPTARG"
				;;
			*)