Skip to content

Instantly share code, notes, and snippets.

@nikoheikkila
Last active March 22, 2024 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikoheikkila/d84831960255674e0883de631b09229b to your computer and use it in GitHub Desktop.
Save nikoheikkila/d84831960255674e0883de631b09229b to your computer and use it in GitHub Desktop.
A quick way to record a new time entry using the official Harvest CLI package.
# A quick way to record a new time entry using the official Harvest CLI package.
# Usage: `hours duration [alias]`
# Create a new alias for a project/task mapping with `hrvst alias create` if needed.
function hours -d "Record new entry to Harvest" -a hours -a alias
__hrvst_auth
if test (count $argv) -eq 0
__total_hours
return 0
end
if test -z $hours || test $hours -le 0
echo "ERROR: Amount of hours must be greater than 0"
return 1
end
set -l flags --editor
if string length -q $alias && npx hrvst alias list | grep -q "$alias"
echo "INFO: Submitting $hours hour(s) under $alias."
npx hrvst log $hours $alias $flags
return 0
end
echo "INFO: Submitting $hours hour(s). Please, choose a project and a task."
npx hrvst log $hours $flags
end
function __total_hours
set -l today (date +%Y-%m-%d)
set -l total (npx hrvst time-entries list --from $today --output json --fields hours | jq '[.[] | .hours] | add')
set -l ratio (math $total / 7.5 x 100)
set -l fields hours,task.name,notes
npx hrvst time-entries list --from $today --output table --fields $fields
echo "Total: $total / 7.5 h ($ratio %)"
end
function __hrvst_auth
if not npx hrvst users me >/dev/null
echo "INFO: Authenticating with Harvest. A browser window will open..."
npx hrvst login
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment