mirror of
https://github.com/tonydamage/nux-env.git
synced 2025-12-11 13:24:28 +01:00
taskie: Updated backends to use common code.
Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
This commit is contained in:
parent
70dbc4dc1d
commit
cfad8b1576
6 changed files with 174 additions and 98 deletions
47
bin/taskie
47
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() {
|
||||
:
|
||||
}
|
||||
|
|
|
|||
20
inc/taskie/backend.dir.inc.sh
Normal file
20
inc/taskie/backend.dir.inc.sh
Normal file
|
|
@ -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"
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
97
inc/taskie/githublike.inc.sh
Normal file
97
inc/taskie/githublike.inc.sh
Normal file
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue