Skip to content

Commit b1f03e2

Browse files
committed
bin: add bash completion for copy command
Add bash completion for the common datastores, like we already do in the CLI, and update the usage text accordingly. Also, make sure to install to /usr/bin, not /bin since we've now merged the hierarchies since a while back. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
1 parent fa1b38e commit b1f03e2

File tree

4 files changed

+115
-8
lines changed

4 files changed

+115
-8
lines changed

package/bin/bin.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ BIN_LICENSE = BSD-3-Clause
1111
BIN_LICENSE_FILES = LICENSE
1212
BIN_REDISTRIBUTE = NO
1313
BIN_DEPENDENCIES = sysrepo libite
14-
BIN_CONF_OPTS = --prefix= --disable-silent-rules
14+
BIN_CONF_OPTS = --disable-silent-rules
1515
BIN_AUTORECONF = YES
1616

1717
define BIN_CONF_ENV

src/bin/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ ACLOCAL_AMFLAGS = -I m4
33

44
bin_PROGRAMS = copy erase files
55

6+
# Bash completion
7+
bashcompdir = $(datadir)/bash-completion/completions
8+
dist_bashcomp_DATA = copy.bash
9+
610
copy_SOURCES = copy.c util.c util.h
711
copy_CPPFLAGS = -D_DEFAULT_SOURCE -D_GNU_SOURCE
812
copy_CFLAGS = -W -Wall -Wextra

src/bin/copy.bash

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# bash completion for copy command
2+
# SPDX-License-Identifier: ISC
3+
4+
_copy_completion()
5+
{
6+
local cur prev opts
7+
COMPREPLY=()
8+
cur="${COMP_WORDS[COMP_CWORD]}"
9+
prev="${COMP_WORDS[COMP_CWORD-1]}"
10+
11+
# Options for the copy command
12+
opts="-h -n -q -s -t -u -v"
13+
14+
local datastores_dst="running-config startup-config"
15+
local datastores_src="running-config startup-config factory-config"
16+
17+
case "${prev}" in
18+
-t)
19+
# Timeout expects a number, no completion
20+
return 0
21+
;;
22+
-u)
23+
# Username, could complete with getent passwd but keep it simple
24+
return 0
25+
;;
26+
esac
27+
28+
# If current word starts with -, complete options
29+
if [[ ${cur} == -* ]]; then
30+
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
31+
return 0
32+
fi
33+
34+
# Determine position (source or destination)
35+
# Count non-option arguments before current position
36+
local arg_count=0
37+
local i
38+
for ((i=1; i < COMP_CWORD; i++)); do
39+
case "${COMP_WORDS[i]}" in
40+
-h|-n|-q|-s|-v)
41+
# Flag without argument
42+
;;
43+
-t|-u)
44+
# Flag with argument, skip next word
45+
((i++))
46+
;;
47+
-*)
48+
# Unknown flag, might have argument
49+
;;
50+
*)
51+
# Non-option argument
52+
((arg_count++))
53+
;;
54+
esac
55+
done
56+
57+
# Helper function to add non-hidden files/dirs, filtering out dotfiles unless explicitly requested
58+
_add_files() {
59+
local IFS=$'\n'
60+
local files
61+
62+
# If user is explicitly typing a dotfile path, include dotfiles
63+
if [[ ${cur} == .* || ${cur} == */.* ]]; then
64+
files=( $(compgen -f -- "${cur}") )
65+
else
66+
# Only show non-hidden files and directories
67+
files=( $(compgen -f -- "${cur}" | grep -v '/\.[^/]*$' | grep -v '^\.[^/]') )
68+
fi
69+
70+
COMPREPLY+=( "${files[@]}" )
71+
}
72+
73+
case ${arg_count} in
74+
0)
75+
# First argument (source): complete with files and all datastores including factory-config
76+
COMPREPLY=( $(compgen -W "${datastores_src}" -- ${cur}) )
77+
_add_files
78+
;;
79+
1)
80+
# Second argument (destination): complete with files and limited datastores (no factory-config)
81+
COMPREPLY=( $(compgen -W "${datastores_dst}" -- ${cur}) )
82+
_add_files
83+
;;
84+
*)
85+
# No more arguments expected
86+
return 0
87+
;;
88+
esac
89+
90+
return 0
91+
}
92+
93+
complete -F _copy_completion copy

src/bin/copy.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,23 @@ static int usage(int rc)
470470
printf("Usage: %s [OPTIONS] SRC DST\n"
471471
"\n"
472472
"Options:\n"
473-
" -h This help text\n"
474-
" -n Dry-run, validate configuration without applying\n"
475-
" -q Quiet mode, suppress informational messages\n"
476-
" -s Sanitize paths for CLI use (restrict path traversal)\n"
477-
" -u USER Username for remote commands, like scp\n"
478-
" -t SEEC Timeout for the operation, or default %d sec\n"
479-
" -v Show version\n", prognm, timeout);
473+
" -h This help text\n"
474+
" -n Dry-run, validate configuration without applying\n"
475+
" -q Quiet mode, suppress informational messages\n"
476+
" -s Sanitize paths for CLI use (restrict path traversal)\n"
477+
" -t SEC Timeout for the operation, or default %d sec\n"
478+
" -u USER Username for remote commands, like scp\n"
479+
" -v Show version\n"
480+
"\n"
481+
"Files:\n"
482+
" SRC JSON configuration file, or a datastore\n"
483+
" DST A file or datastore, except factory-config\n"
484+
"\n"
485+
"Datastores:\n"
486+
" running-config The running datastore, current active config\n"
487+
" startup-config The non-volatile config used at startup\n"
488+
" factory-config The device's factory default configuration\n"
489+
"\n", prognm, timeout);
480490

481491
return rc;
482492
}

0 commit comments

Comments
 (0)