diff --git a/bin/taskie b/bin/taskie index d92cf6c..0afc1ef 100755 --- a/bin/taskie +++ b/bin/taskie @@ -10,6 +10,7 @@ nux.use nux.repl nux.use taskie/common nux.use taskie/backend.github nux.use taskie/backend.gogs +nux.use taskie/backend.dir with.backend() { backendId="$1"; @@ -24,7 +25,9 @@ endwith.backend() { task.labels() { - : + with.backend $(backend.detect); + backend.$backend.labels "$@"; + endwith.backend; } ## @@ -35,19 +38,36 @@ task.labels() { task.add() { with.backend $(backend.detect); if ! backend.$backend.issue.exists "$@" ; then - backend.$backend.add "$@"; + local labels=$(backend.$backend.labels.id) + nux.log debug "Labels: $labels" + label=$(echo "$labels" | grep -G "^$1:") + local labelName="" + local labelId="" + if [ -n "$label" ] ;then + labelId=${label#*:} + labelName=${label%:*} + nux.log debug "First argument is label $labelName ($labelId)" + shift; + fi + + backend.$backend.issue.add "$@"; else nux.echo.error Issue already exists. fi endwith.backend; } -nuxr.repl.expose cd pwd ls + +nuxr.repl.expose cd pwd ls clear +nuxr.repl.prompt() { + echo "${nc_green}$NUX_APPNAME${nc_end}:${nc_blue}$(pwd)${nc_end}> " +} + ## ## ## task.list() { with.backend $(backend.detect); - backend.$backend.list "$@"; + backend.$backend.issue.list "$@"; endwith.backend; } @@ -70,7 +90,7 @@ backend.detect() { shift; fi for backend in $taskie_backends; do - nux.log trace Executing backend $backend detection + nux.log trace "Executing backend '$backend' detection" localId=$(backend.$backend.detect "$@") if [ -n "$localId" ]; then echo $backend:$localId @@ -91,23 +111,6 @@ gogs.config.site() { nux.cfg.read "gogs.\"$1\"$2"; } -backend.github.detect() { - closest_git=$(nuxfs.closest .git "$1") - - git.origins "$closest_git" | grep github.com | while read origin - do - repo=$(nux.url.parse "$origin" "\9") - echo $repo:$closest_git - done -} - - - backend.file.detect() { : - -} - -backend.dir.detect() { - : } diff --git a/inc/taskie/backend.dir.inc.sh b/inc/taskie/backend.dir.inc.sh new file mode 100644 index 0000000..8d1c91e --- /dev/null +++ b/inc/taskie/backend.dir.inc.sh @@ -0,0 +1,20 @@ +nux.log debug "Backend Dir loaded" + +backend.dir.detect() { + local dottaskie=$(nuxfs.closest .taskie "$1") + local dottaskiedir=$(nuxfs.closest .taskie.self "$1") + if [ -d "$dottaskie" ]; then + echo "$dottaskie" + elif [ -f "$dottaskie" -a "$(yaml r "$dottaskie" backend)" = "dir:self" ]; then + dirname "$dottaskie" + fi +} + +backend.dir.with() { + dir_repository=$(echo $backendId | cut -d: -f2) + +} + +backend.dir.labels() { + find "$dir_repository" -type d | grep -v "^\." | grep -v "/\." |xargs -n1 realpath --relative-to="$dir_repository" +} diff --git a/inc/taskie/backend.github.inc.sh b/inc/taskie/backend.github.inc.sh index 9d83392..e8df81e 100644 --- a/inc/taskie/backend.github.inc.sh +++ b/inc/taskie/backend.github.inc.sh @@ -1,3 +1,8 @@ +nux.use taskie/githublike + +githublike github + + backend.github.with() { github_repository=$(echo $backendId | cut -d: -f2) github_api_url=https://api.github.com @@ -6,12 +11,22 @@ backend.github.with() { github_issuemap=~/.config/taskie/github.issuemap.json nux.log debug Github repository is $github_repository; nux.log debug Github API URL: $github_api_url; + + githublike_wrapper=github + githublike_api=$github_api_url; + githublike_repository=$github_repository; + githublike_api_append=""; + githublike_curl_params="-u $github_api_user:$github_api_token"; + githublike_next_append=""; + githublike.with } -backend.github.list() { - local api="$github_api_url/repos/$github_repository/issues" - - CURL_ADDITIONAL_ARGS="-u $github_api_user:$github_api_token" \ - backend.githublike.get "$api" | jq -r ".[] | [.number,.state,.title] | @sh" +backend.github.detect() { + closest_git=$(nuxfs.closest .git "$1") + git.origins "$closest_git" | grep github.com | while read origin + do + repo=$(nux.url.parse "$origin" "\9") + echo $repo:$closest_git + done } diff --git a/inc/taskie/backend.gogs.inc.sh b/inc/taskie/backend.gogs.inc.sh index 1597f28..e68bb3b 100644 --- a/inc/taskie/backend.gogs.inc.sh +++ b/inc/taskie/backend.gogs.inc.sh @@ -1,34 +1,8 @@ -nux.use taskie/backend.utils +nux.use taskie/githublike nux.use nux.json -backend.gogs.list() { - local api="$gogs_api_url/repos/$gogs_repository/issues?token=$gogs_api_token" - local append_next="&token=$gogs_api_token" - backend.githublike.get "$api" "$append_next" \ - | jq -r ".[] | [.number,.state,(\"#\" + .labels[].name) ,.title] | @sh" \ - | while read line - do - eval taskie.issue.display.short $line - done -} -argz() { - int=0; - for arg in "$@" - do - let int=int+1 - echo $int $arg - done -} - -backend.gogs.issue.exists() { - local message="$@" - nux.json.start - nux.json.open "$gogs_issuemap" - id=$(nux.json.read "\"$gogs_api_url\".\"$gogs_repository\".\"$message\"") - nux.log debug "Message Id is $id" - test "$id" != null #-o -n "$id"; -} +githublike gogs backend.gogs.detect() { closest_git=$(nuxfs.closest .git "$1") @@ -50,31 +24,18 @@ backend.gogs.detect() { backend.gogs.with() { gogs_repository=$(echo $backendId | cut -d: -f2) gogs_configId=$(echo $backendId | cut -d: -f4) - - gogs_api_url=$(gogs.config.site "$gogs_configId" .api.url) gogs_api_token=$(gogs.config.site "$gogs_configId" .api.token) - gogs_issuemap=$(nux.cfg.dir.global)/gogs.issuemap.json + nux.log debug Gogs repository is $gogs_repository; nux.log debug Gogs API URL: $gogs_api_url; -} - -backend.gogs.add() { - - echo "Adding issue:" "\"$@\"" - - local message="$@" - local payload="{\"title\": \"$message\",\"body\": \"$message\"}" - local api="$gogs_api_url/repos/$gogs_repository/issues?token=$gogs_api_token" - nux.log debug Repository is $gogs_repository, message is $message - nux.log debug API call: $api Payload: $payload - remId=$(curl -s -H "Content-Type: application/json" -X POST -d "$payload" "$api" | jq -r .number) - - nux.json.start - nux.json.open "$gogs_issuemap" - nux.json.write "\"$gogs_api_url\".\"$gogs_repository\".\"$message\"" $remId - nux.json.flush "$gogs_issuemap" - - echo Issue Number is: $remId - + + githublike_wrapper=gogs + githublike_api=$(gogs.config.site "$gogs_configId" .api.url); + githublike_repository=$gogs_repository; + githublike_api_append="?token=$gogs_api_token"; + githublike_curl_params=""; + githublike_next_append="&token=$gogs_api_token"; + + githublike.with } diff --git a/inc/taskie/backend.utils.inc.sh b/inc/taskie/backend.utils.inc.sh index 218fb60..e69de29 100644 --- a/inc/taskie/backend.utils.inc.sh +++ b/inc/taskie/backend.utils.inc.sh @@ -1,20 +0,0 @@ - -backend.githublike.get() { - local api=$1; - local append_next="$2"; - nux.log debug Repository is $gogs_repository, message is $message - nux.log debug API call: $api Payload: $payload - header_tmp=$(mktemp); - while [ -n "$api" ]; - do - curl $CURL_ADDITIONAL_ARGS -s -D "$header_tmp" -H "Content-Type: application/json" "$api" - next=$(grep "Link: " "$header_tmp" | tr "," "\n" | grep rel=\"next\" | cut -d"<" -f2 | cut -d">" -f1) - nux.log debug Next "$next"; - if [ -n "$next" ]; then - api="${next}${append_next}" - else - api="" - fi - done; - rm -f $header_tmp; -} diff --git a/inc/taskie/githublike.inc.sh b/inc/taskie/githublike.inc.sh new file mode 100644 index 0000000..f4c32fa --- /dev/null +++ b/inc/taskie/githublike.inc.sh @@ -0,0 +1,97 @@ +githublike.common.issue.list() { + githublike.get issues \ + | jq -r ".[] | [.number,.state,(\"#\" + .labels[].name) ,.title] | @sh" \ + | while read line + do + eval taskie.issue.display.short $line + done +} + +githublike.common.issue.exists() { + local message="$@" + nux.json.start + nux.json.open "$githublike_issuemap" + id=$(nux.json.read "\"$githublike_api\".\"$githublike_repository\".\"$message\"") + nux.log debug "Message Id is $id" + test "$id" != null #-o -n "$id"; +} + +githublike.common.labels() { + githublike.get "labels" \ + | jq -r ".[] | .name + \":\" + (.id | @text) | @text" +} + +githublike.common.issue.add() { + echo "Adding issue:" "\"$@\"" + local message="$@" + + nux.json.start + + nux.json.write title "$message" + nux.json.write message "$message" + if [ -n "labelId" ]; then + nux.json.write.raw labels[0] $labelId + fi + local payload=$(nux.json.flush) + + nux.log debug Repository is $githublike_repository, message is $message + nux.log debug API call: $api Payload: $payload + remId=$(githublike.post issues "$payload" | jq -r .number) + + nux.json.start + nux.json.open "$githublike_issuemap" + nux.json.write "\"$githublike_api\".\"$githublike_repository\".\"$message\"" $remId + nux.json.flush "$githublike_issuemap" + + echo Issue Number is: $remId + +} + +githublike.common.labels.id() { + githublike.get "labels" \ + | jq -r ".[] | .name + \":\" + (.id | @text) | @text" +} + +githublike.get() { + local resource=$1; + local api="${githublike_api}/repos/${githublike_repository}/${resource}${githublike_api_append}"; + nux.log debug Repository is $githublike_repository, message is $message + nux.log debug API call: $api Payload: $payload + header_tmp=$(mktemp); + while [ -n "$api" ]; + do + curl $githublike_curl_params -s -D "$header_tmp" -H "Content-Type: application/json" "$api" + next=$(grep "Link: " "$header_tmp" | tr "," "\n" | grep rel=\"next\" | cut -d"<" -f2 | cut -d">" -f1) + nux.log debug Next "$next"; + if [ -n "$next" ]; then + api="${next}${githublike_next_append}" + else + api="" + fi + done; + rm -f $header_tmp; +} + +githublike.post() { + local resource=$1 + local api="${githublike_api}/repos/${githublike_repository}/${resource}${githublike_api_append}" + local payload="$2" + nux.log debug POST API call: $api Payload: $payload + curl $githublike_curl_params -s -H "Content-Type: application/json" -X POST -d "$payload" "$api" +} + +githublike() { + backend=$1; + + functions=$(set | grep -G "^githublike.common.* (" | cut -d"." -f3- | cut -d"(" -f1) + for function in $functions + do + eval """function backend.$backend.$function { + githublike.common.$function \"\$@\" + }" + done +} + +githublike.with() { + githublike_issuemap=$(nux.cfg.dir.global)/${githublike_wrapper}.issuemap.json +}