Skip to content

Commit 87cf85e

Browse files
authored
Merge pull request #46 from radomirbosak/jobname-autocomplete
Add bash completion for job names
2 parents 54d6477 + 12e6838 commit 87cf85e

4 files changed

Lines changed: 23 additions & 6 deletions

File tree

contrib/bash-completion/jenkins

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ _jenkins()
2626
case $prev in
2727
jobs)
2828
opts="-h --help -a -p"
29-
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
30-
return 0
3129
;;
32-
queue|building|builds|start|info|configxml|setbranch|stop|console|changes)
30+
builds|start|info|configxml|setbranch|stop|console|changes)
31+
opts="-h --help"
32+
33+
# if the cached-jobs file exists suggest also job names
34+
CACHE_DIR=${XDG_CACHE_HOME:-~/.cache}"/python-jenkins-cli"
35+
if [ -r $CACHE_DIR/job_cache ]; then
36+
opts="$opts $(cat $CACHE_DIR/job_cache)"
37+
fi
38+
;;
39+
queue|building)
3340
opts="-h --help"
34-
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
35-
return 0
3641
;;
3742
esac
3843

jenkins_cli/cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import jenkins
77
import socket
88
from xml.etree import ElementTree
9+
from xdg.BaseDirectory import save_cache_path
910

1011
try:
1112
from ConfigParser import ConfigParser, NoSectionError
@@ -83,6 +84,7 @@ class CliException(Exception):
8384

8485
class JenkinsCli(object):
8586
SETTINGS_FILE_NAME = '.jenkins-cli'
87+
JOB_CACHE_FILE_NAME = 'job_cache'
8688

8789
QUEUE_EMPTY_TEXT = "Building queue is empty"
8890

@@ -144,10 +146,18 @@ def run_command(self, args):
144146

145147
def jobs(self, args):
146148
jobs = self._get_jobs(args)
149+
150+
# print jobs
147151
for job in jobs:
148152
formated_status = get_formated_status(job['color'])
149153
print(formated_status + " " + job['name'])
150154

155+
# save job names to cache file
156+
our_cache_dir = save_cache_path('python-jenkins-cli')
157+
job_cache_file = os.path.join(our_cache_dir, self.JOB_CACHE_FILE_NAME)
158+
with open(job_cache_file, 'w') as f:
159+
f.write(' '.join(job['name'] for job in jobs))
160+
151161
def _get_jobs(self, args):
152162
jobs = self.jenkins.get_jobs()
153163
if args.a:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pyfakefs==2.7.0
44
mock==2.0.0
55
unittest2==1.1.0
66
flake8==2.5.4
7+
pyxdg==0.25

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
raise RuntimeError('Unable to find version string.')
1616

1717
requires = ['python-jenkins==0.4.14',
18-
'six>=1.9.0']
18+
'six>=1.9.0',
19+
'pyxdg>=0.25']
1920

2021
tests_require = ['unittest2==1.1.0',
2122
'mock==2.0.0',

0 commit comments

Comments
 (0)