提交 ec3403be 编写于 作者: M Michal Privoznik

virsh: Introduce virshDomainNameCompleter

Now that we have everything prepared let the fun begin. This
completer is very simple and returns domain names. Moreover,
depending on the command it can return just a subset of domains
(e.g. only running/paused/transient/.. ones).
Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
上级 f0d390bc
......@@ -231,6 +231,15 @@ virsh_SOURCES = \
virsh-volume.c virsh-volume.h \
$(NULL)
VIRSH_COMPLETER = \
virsh-completer.c virsh-completer.h
if WITH_READLINE
virsh_SOURCES += $(VIRSH_COMPLETER)
else ! WITH_READLINE
EXTRA_DIST += $(VIRSH_COMPLETER)
endif ! WITH_READLINE
virsh_LDFLAGS = \
$(AM_LDFLAGS) \
$(PIE_LDFLAGS) \
......
/*
* virsh-completer.c: virsh completer callbacks
*
* Copyright (C) 2017 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Author: Michal Privoznik <mprivozn@redhat.com>
*/
#include <config.h>
#include "virsh-completer.h"
#include "virsh.h"
#include "virsh-util.h"
#include "internal.h"
#include "viralloc.h"
#include "virstring.h"
char **
virshDomainNameCompleter(vshControl *ctl,
const vshCmd *cmd ATTRIBUTE_UNUSED,
unsigned int flags)
{
virshControlPtr priv = ctl->privData;
virDomainPtr *domains = NULL;
int ndomains = 0;
size_t i = 0;
char **ret = NULL;
virCheckFlags(VIR_CONNECT_LIST_DOMAINS_ACTIVE |
VIR_CONNECT_LIST_DOMAINS_INACTIVE |
VIR_CONNECT_LIST_DOMAINS_PERSISTENT |
VIR_CONNECT_LIST_DOMAINS_TRANSIENT |
VIR_CONNECT_LIST_DOMAINS_RUNNING |
VIR_CONNECT_LIST_DOMAINS_PAUSED |
VIR_CONNECT_LIST_DOMAINS_SHUTOFF |
VIR_CONNECT_LIST_DOMAINS_OTHER |
VIR_CONNECT_LIST_DOMAINS_MANAGEDSAVE |
VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE |
VIR_CONNECT_LIST_DOMAINS_AUTOSTART |
VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART |
VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT |
VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHOT,
NULL);
if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
return NULL;
if ((ndomains = virConnectListAllDomains(priv->conn, &domains, flags)) < 0)
return NULL;
if (VIR_ALLOC_N(ret, ndomains + 1) < 0)
goto error;
for (i = 0; i < ndomains; i++) {
const char *name = virDomainGetName(domains[i]);
if (VIR_STRDUP(ret[i], name) < 0)
goto error;
virshDomainFree(domains[i]);
}
VIR_FREE(domains);
return ret;
error:
for (; i < ndomains; i++)
virshDomainFree(domains[i]);
VIR_FREE(domains);
for (i = 0; i < ndomains; i++)
VIR_FREE(ret[i]);
VIR_FREE(ret);
return NULL;
}
/*
* virsh-completer.h: virsh completer callbacks
*
* Copyright (C) 2017 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Michal Privoznik <mprivozn@redhat.com>
*
*/
#ifndef VIRSH_COMPLETER
# define VIRSH_COMPLETER
# include "vsh.h"
char ** virshDomainNameCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);
#endif
......@@ -40,8 +40,8 @@
#include "virxml.h"
#include "virstring.h"
#define VIRSH_COMMON_OPT_DOMAIN_FULL \
VIRSH_COMMON_OPT_DOMAIN(N_("domain name, id or uuid"))
#define VIRSH_COMMON_OPT_DOMAIN_FULL(cflags) \
VIRSH_COMMON_OPT_DOMAIN(N_("domain name, id or uuid"), cflags)
VIR_ENUM_DECL(virshDomainIOError)
VIR_ENUM_IMPL(virshDomainIOError,
......@@ -278,7 +278,7 @@ static const vshCmdInfo info_dommemstat[] = {
};
static const vshCmdOptDef opts_dommemstat[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
{.name = "period",
.type = VSH_OT_INT,
.flags = VSH_OFLAG_REQ_OPT,
......@@ -390,7 +390,7 @@ static const vshCmdInfo info_domblkinfo[] = {
};
static const vshCmdOptDef opts_domblkinfo[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "device",
.type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ,
......@@ -460,7 +460,7 @@ static const vshCmdInfo info_domblklist[] = {
};
static const vshCmdOptDef opts_domblklist[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "inactive",
.type = VSH_OT_BOOL,
.help = N_("get inactive rather than running configuration")
......@@ -566,7 +566,7 @@ static const vshCmdInfo info_domiflist[] = {
};
static const vshCmdOptDef opts_domiflist[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "inactive",
.type = VSH_OT_BOOL,
.help = N_("get inactive rather than running configuration")
......@@ -655,7 +655,7 @@ static const vshCmdInfo info_domif_getlink[] = {
};
static const vshCmdOptDef opts_domif_getlink[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "interface",
.type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ,
......@@ -752,7 +752,7 @@ static const vshCmdInfo info_domcontrol[] = {
};
static const vshCmdOptDef opts_domcontrol[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
{.name = NULL}
};
......@@ -805,7 +805,7 @@ static const vshCmdInfo info_domblkstat[] = {
};
static const vshCmdOptDef opts_domblkstat[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
{.name = "device",
.type = VSH_OT_STRING,
.flags = VSH_OFLAG_EMPTY_OK,
......@@ -991,7 +991,7 @@ static const vshCmdInfo info_domifstat[] = {
};
static const vshCmdOptDef opts_domifstat[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
{.name = "interface",
.type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ,
......@@ -1064,7 +1064,7 @@ static const vshCmdInfo info_domblkerror[] = {
};
static const vshCmdOptDef opts_domblkerror[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
{.name = NULL}
};
......@@ -1125,7 +1125,7 @@ static const vshCmdInfo info_dominfo[] = {
};
static const vshCmdOptDef opts_dominfo[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = NULL}
};
......@@ -1264,7 +1264,7 @@ static const vshCmdInfo info_domstate[] = {
};
static const vshCmdOptDef opts_domstate[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "reason",
.type = VSH_OT_BOOL,
.help = N_("also print reason for the state")
......@@ -1316,7 +1316,7 @@ static const vshCmdInfo info_domtime[] = {
};
static const vshCmdOptDef opts_domtime[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
{.name = "now",
.type = VSH_OT_BOOL,
.help = N_("set to the time of the host running virsh")
......@@ -2145,7 +2145,7 @@ static const vshCmdInfo info_domifaddr[] = {
};
static const vshCmdOptDef opts_domifaddr[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(VIR_CONNECT_LIST_DOMAINS_ACTIVE),
{.name = "interface",
.type = VSH_OT_STRING,
.flags = VSH_OFLAG_NONE,
......
此差异已折叠。
......@@ -42,8 +42,8 @@
#include "virxml.h"
#include "conf/snapshot_conf.h"
#define VIRSH_COMMON_OPT_DOMAIN_FULL \
VIRSH_COMMON_OPT_DOMAIN(N_("domain name, id or uuid"))
#define VIRSH_COMMON_OPT_DOMAIN_FULL(cflags) \
VIRSH_COMMON_OPT_DOMAIN(N_("domain name, id or uuid"), cflags)
/* Helper for snapshot-create and snapshot-create-as */
static bool
......@@ -125,7 +125,7 @@ static const vshCmdInfo info_snapshot_create[] = {
};
static const vshCmdOptDef opts_snapshot_create[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "xmlfile",
.type = VSH_OT_STRING,
.help = N_("domain snapshot XML")
......@@ -319,7 +319,7 @@ static const vshCmdInfo info_snapshot_create_as[] = {
};
static const vshCmdOptDef opts_snapshot_create_as[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "name",
.type = VSH_OT_STRING,
.help = N_("name of snapshot")
......@@ -508,7 +508,7 @@ static const vshCmdInfo info_snapshot_edit[] = {
};
static const vshCmdOptDef opts_snapshot_edit[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("snapshot name")
......@@ -620,7 +620,7 @@ static const vshCmdInfo info_snapshot_current[] = {
};
static const vshCmdOptDef opts_snapshot_current[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "name",
.type = VSH_OT_BOOL,
.help = N_("list the name, rather than the full xml")
......@@ -851,7 +851,7 @@ static const vshCmdInfo info_snapshot_info[] = {
};
static const vshCmdOptDef opts_snapshot_info[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("snapshot name")
......@@ -1401,7 +1401,7 @@ static const vshCmdInfo info_snapshot_list[] = {
};
static const vshCmdOptDef opts_snapshot_list[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "parent",
.type = VSH_OT_BOOL,
.help = N_("add a column showing parent snapshot")
......@@ -1657,7 +1657,7 @@ static const vshCmdInfo info_snapshot_dumpxml[] = {
};
static const vshCmdOptDef opts_snapshot_dumpxml[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ,
......@@ -1720,7 +1720,7 @@ static const vshCmdInfo info_snapshot_parent[] = {
};
static const vshCmdOptDef opts_snapshot_parent[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("find parent of snapshot name")
......@@ -1779,7 +1779,7 @@ static const vshCmdInfo info_snapshot_revert[] = {
};
static const vshCmdOptDef opts_snapshot_revert[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("snapshot name")
......@@ -1863,7 +1863,7 @@ static const vshCmdInfo info_snapshot_delete[] = {
};
static const vshCmdOptDef opts_snapshot_delete[] = {
VIRSH_COMMON_OPT_DOMAIN_FULL,
VIRSH_COMMON_OPT_DOMAIN_FULL(0),
{.name = "snapshotname",
.type = VSH_OT_STRING,
.help = N_("snapshot name")
......
......@@ -38,6 +38,7 @@
# include "virthread.h"
# include "virpolkit.h"
# include "vsh.h"
# include "virsh-completer.h"
# define VIRSH_PROMPT_RW "virsh # "
# define VIRSH_PROMPT_RO "virsh > "
......@@ -70,11 +71,13 @@
.help = _helpstr \
}
# define VIRSH_COMMON_OPT_DOMAIN(_helpstr) \
# define VIRSH_COMMON_OPT_DOMAIN(_helpstr, cflags) \
{.name = "domain", \
.type = VSH_OT_DATA, \
.flags = VSH_OFLAG_REQ, \
.help = _helpstr \
.help = _helpstr, \
.completer = virshDomainNameCompleter, \
.completer_flags = cflags, \
}
# define VIRSH_COMMON_OPT_CONFIG(_helpstr) \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册