From patchwork Thu Jul 19 00:38:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Verma, Vishal L" X-Patchwork-Id: 10533497 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6F7356053F for ; Thu, 19 Jul 2018 00:39:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3FC0C2980A for ; Thu, 19 Jul 2018 00:39:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3418929827; Thu, 19 Jul 2018 00:39:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D08312980A for ; Thu, 19 Jul 2018 00:39:16 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 7802F20988749; Wed, 18 Jul 2018 17:39:16 -0700 (PDT) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.93; helo=mga11.intel.com; envelope-from=vishal.l.verma@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 43AAA21BADAB2 for ; Wed, 18 Jul 2018 17:39:14 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Jul 2018 17:39:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,371,1526367600"; d="scan'208";a="72534473" Received: from vverma7-desk1.lm.intel.com ([10.232.112.133]) by fmsmga004.fm.intel.com with ESMTP; 18 Jul 2018 17:38:51 -0700 From: Vishal Verma To: Subject: [ndctl PATCH] ndctl, bash-completion: add completion for ndctl-monitor Date: Wed, 18 Jul 2018 18:38:46 -0600 Message-Id: <20180719003846.12068-1-vishal.l.verma@intel.com> X-Mailer: git-send-email 2.14.4 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP Add bash completions for the 'ndctl monitor' command. This introduces some new semantics where the completion for --log= can be either a file path, or one of the canonical options (currently 'standard' or 'syslog'. We handle this by (ab)using compgen to test whether the entered string is a substring of one of the canonical strings. If it is, then we include the array of the canonical strngs as well as regular _filedir output in our reply. If not, then we rely on _filedir doing its thing. Cc: QI Fuli Signed-off-by: Vishal Verma --- contrib/ndctl | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/contrib/ndctl b/contrib/ndctl index 7a99baf..515494d 100755 --- a/contrib/ndctl +++ b/contrib/ndctl @@ -91,7 +91,7 @@ __ndctlcomp() COMPREPLY=( $( compgen -W "$1" -- "$2" ) ) for cword in "${COMPREPLY[@]}"; do - if [[ "$cword" == @(--bus|--region|--type|--mode|--size|--dimm|--reconfig|--uuid|--name|--sector-size|--map|--namespace|--input|--output|--label-version|--align|--block|--count|--firmware|--media-temperature|--ctrl-temperature|--spares|--media-temperature-threshold|--ctrl-temperature-threshold|--spares-threshold|--media-temperature-alarm|--ctrl-temperature-alarm|--spares-alarm|--numa-node) ]]; then + if [[ "$cword" == @(--bus|--region|--type|--mode|--size|--dimm|--reconfig|--uuid|--name|--sector-size|--map|--namespace|--input|--output|--label-version|--align|--block|--count|--firmware|--media-temperature|--ctrl-temperature|--spares|--media-temperature-threshold|--ctrl-temperature-threshold|--spares-threshold|--media-temperature-alarm|--ctrl-temperature-alarm|--spares-alarm|--numa-node|--log|--dimm-event|--config-file) ]]; then COMPREPLY[$i]="${cword}=" else COMPREPLY[$i]="${cword} " @@ -167,10 +167,13 @@ __ndctl_get_nodes() echo "$nlist" } +saveopts="" __ndctl_file_comp() { local cur="$1" + _filedir + saveopts="${COMPREPLY[*]}" } __ndctl_comp_options() { @@ -179,6 +182,7 @@ __ndctl_comp_options() if [[ "$cur" == *=* ]]; then local cur_subopt=${cur%%=*} + local cur_arg=${cur##*=} case $cur_subopt in --bus) opts=$(__ndctl_get_buses) @@ -219,7 +223,7 @@ __ndctl_comp_options() --input) ;& --firmware) - __ndctl_file_comp "${cur##*=}" + __ndctl_file_comp "$cur_arg" return ;; --label-version) @@ -235,11 +239,45 @@ __ndctl_comp_options() --numa-node) opts=$(__ndctl_get_nodes) ;; + --log) + local my_args=( "standard" "syslog" ) + + __ndctl_file_comp "$cur_arg" + if [ -n "$cur_arg" ]; then + # if $cur_arg is a substring of $my_args (as + # determined by compgen), then we continue to + # manually construct opts using $my_args, but + # if it is not a substring (compgen returns + # empty), then we defer to _ndctl_file_comp + # and return immediately. (NOTE: subscripting + # of the my_args array here is fragile). + # If this pattern repeats, where we need to + # complete with filenames and some custom + # options, find a better way to generalize this + if [ -z "$(compgen -W "${my_args[*]}" -- "$cur_arg")" ]; then + return + fi + fi + opts="${my_args[*]} $saveopts" + ;; + --dimm-event) + opts="all \ + dimm-spares-remaining \ + dimm-media-temperature \ + dimm-controller-temperature \ + dimm-health-state \ + dimm-unclean-shutdown \ + " + ;; + --config-file) + __ndctl_file_comp "$cur_arg" + return + ;; *) return ;; esac - __ndctlcomp "$opts" "${cur##*=}" + __ndctlcomp "$opts" "$cur_arg" fi }