Skip to content

Commit 12e6838

Browse files
committedJun 4, 2017
Add bash completion for job names
This commit adds suggestion of job names to the bash completion script. The list of available jobs is stored in a cache file (by default ~/.cache/python-jenkins-cli/job_cache) and is refreshed with every 'jenkins jobs' call. Fixes #4
1 parent c7a79f9 commit 12e6838

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed
 

‎contrib/bash-completion/jenkins

+10-5
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

+10
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
STATUSES_COLOR = {'blue': {'symbol': 'S',
1112
'color': '\033[94m',
@@ -77,6 +78,7 @@ class CliException(Exception):
7778

7879
class JenkinsCli(object):
7980
SETTINGS_FILE_NAME = '.jenkins-cli'
81+
JOB_CACHE_FILE_NAME = 'job_cache'
8082

8183
QUEUE_EMPTY_TEXT = "Building queue is empty"
8284

@@ -129,10 +131,18 @@ def run_command(self, args):
129131

130132
def jobs(self, args):
131133
jobs = self._get_jobs(args)
134+
135+
# print jobs
132136
for job in jobs:
133137
formated_status = get_formated_status(job['color'])
134138
print(formated_status + " " + job['name'])
135139

140+
# save job names to cache file
141+
our_cache_dir = save_cache_path('python-jenkins-cli')
142+
job_cache_file = os.path.join(our_cache_dir, self.JOB_CACHE_FILE_NAME)
143+
with open(job_cache_file, 'w') as f:
144+
f.write(' '.join(job['name'] for job in jobs))
145+
136146
def _get_jobs(self, args):
137147
jobs = self.jenkins.get_jobs()
138148
if args.a:

‎requirements.txt

+1
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

+2-1
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)
Please sign in to comment.