-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfacct
152 lines (138 loc) · 5.74 KB
/
nfacct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env bash
# -----------------------------------------------------------------
# Programmable completion specification for nfacct (netfilter)
# http://www.netfilter.org/projects/nfacct/index.html
#
# Project home:
# https://github.com/AllKind/nfacct-bash_completion
# https://sourceforge.net/projects/nfacct-bashcompletion
# -----------------------------------------------------------------
# Copyright (C) 2016 Mart Frauenlob aka AllKind ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------
# Requirements:
#
# The bash completion package version 2.0 or greater is recommended.
# https://github.com/scop/bash-completion
# If bash completion is present, and the version includes the
# `_init_completion()' function, completion on variables and redirection
# is available in addition. Otherwise option completion is fully supported,
# with the only exception, that object names with spaces are not working.
# -----------------------------------------------------------------
# -----------------------------------------------------------------
# Installation (quote from bash-completion README):
#
# Install it in one of the directories pointed to by
# bash-completion's pkgconfig file variables. There are two
# alternatives: the recommended one is 'completionsdir' (get it with
# "pkg-config --variable=completionsdir bash-completion") from which
# completions are loaded on demand based on invoked commands' names,
# so be sure to name your completion file accordingly, and to include
# for example symbolic links in case the file provides completions
# for more than one command. The other one which is present for
# backwards compatibility reasons is 'compatdir' (get it with
# "pkg-config --variable=compatdir bash-completion") from which files
# are loaded when bash_completion is loaded.
#
# For packages using GNU autotools the installation can be handled
# for example like this in configure.ac:
#
# PKG_CHECK_VAR(bashcompdir, [bash-completion], [completionsdir], ,
# bashcompdir="${sysconfdir}/bash_completion.d")
# AC_SUBST(bashcompdir)
#
# ...accompanied by this in Makefile.am:
#
# bashcompdir = @bashcompdir@
# dist_bashcomp_DATA = # completion files go here
#
# For cmake we ship the bash-completion-config.cmake and
# bash-completion-config-version.cmake files. Example usage:
#
# find_package(bash-completion)
# if(BASH_COMPLETION_FOUND)
# message(STATUS
# "Using bash completion dir ${BASH_COMPLETION_COMPLETIONSDIR}")
# else()
# set (BASH_COMPLETION_COMPLETIONSDIR "/etc/bash_completion.d")
# message (STATUS
# "Using fallback bash completion dir ${BASH_COMPLETION_COMPLETIONSDIR}")
# endif()
#
# install(FILES your-completion-file DESTINATION
# ${BASH_COMPLETION_COMPLETIONSDIR})
#
# For backwards compatibility it is still possible to put it into
# ~/.bash_completion or /etc/bash_completion.d/.
# -----------------------------------------------------------------
#
# Version 1.1
#
# -----------------------------------------------------------------
shopt -s extglob
_nfacct_complete() {
local cur prev cword words
local -i i
# main command
local str_act=""
# main commands
local -a arr_par=( list add delete get flush monitor restore version help )
COMPREPLY=()
# if _init_completion() from bash_completion is available, use it to deal with
# variables and redirections. otherwise use _get_comp_words_by_ref().
# as fallback we commpose our own variables.
if declare -f _init_completion &>/dev/null; then
_init_completion || return # ret val -gt 0 = nothing more to do
elif declare -f _get_comp_words_by_ref &>/dev/null; then
_get_comp_words_by_ref cur prev cword words || return
else # not so neat, but a workaround - will fail w/ spaces in obj names
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cword=$COMP_CWORD
for i in ${!COMP_WORDS[@]}; do words[i]="${COMP_WORDS[i]}"; done
fi
# gather information about the command line
if [[ ${words[1]} = @($(IFS='|' ;printf "%s\n" "${arr_par[*]}") ) ]]; then
str_act="${words[1]}" # this is the main command
fi
# here we go
if ((cword == 1)); then # main command
COMPREPLY=( $( compgen -W '${arr_par[@]}' -- "$cur" ) )
elif ((cword == 2)); then
if [[ $prev = list ]]; then # previous = list
COMPREPLY=( $( compgen -W \
'reset json xml counters overquota bytes-quota pkts-quota' \
-- "$cur" ) )
elif [[ $prev = @(delete|get) ]]; then # previous = delete/get
while read -r; do # retrieve object table
REPLY="${REPLY##*=+([[:blank:]])}"
REPLY="${REPLY%;}"
COMPREPLY[${#COMPREPLY[@]}]="$REPLY"
done < <(command nfacct list 2>/dev/null)
local IFS=$'\n' # to support object names with spaces
COMPREPLY=( $( compgen -P '"' -S '"' -W '${COMPREPLY[@]}' -- "$cur" ) )
fi
elif ((cword == 3)); then
if [[ $str_act = add ]]; then # quota args for add command
COMPREPLY=( $( compgen -W 'byte packet' -- "$cur" ) )
elif [[ $str_act = get ]]; then # get has json/xml param
COMPREPLY=( $( compgen -W 'json xml' -- "$cur" ) )
elif [[ $str_act = list && $prev = reset ]]; then # filter
COMPREPLY=( $( compgen -W 'counters overquota bytes-quota pkts-quota' \
-- "$cur" ) )
fi
fi
}
complete -F _nfacct_complete nfacct