From: Eric Blake <eblake@redhat.com>
To: "Benoît Canet" <benoit.canet@gmail.com>
Cc: aliguori@us.ibm.com, stefanha@linux.vnet.ibm.com,
qemu-devel@nongnu.org, pbonzini@redhat.com,
xiawenc@linux.vnet.ibm.com, "Benoît Canet" <benoit@irqsave.net>
Subject: Re: [Qemu-devel] [PATCH V4 2/2] qemu-img: Add json output option to the info command.
Date: Wed, 22 Aug 2012 08:26:46 -0600 [thread overview]
Message-ID: <5034EC26.2070604@redhat.com> (raw)
In-Reply-To: <1345639535-8822-3-git-send-email-benoit@irqsave.net>
[-- Attachment #1: Type: text/plain, Size: 3200 bytes --]
On 08/22/2012 06:45 AM, Benoît Canet wrote:
> This option --output=[human|json] make qemu-img info output on
> human or JSON representation at the choice of the user.
>
> @@ -1083,7 +1088,6 @@ out:
> return 0;
> }
>
> -
> static void dump_snapshots(BlockDriverState *bs)
> {
Spurious whitespace change.
> +static int img_info(int argc, char **argv)
> +{
> + int c;
> + bool human = false, json = false;
> + const char *filename, *fmt, *output;
> + BlockDriverState *bs;
> + ImageInfo *info;
>
> fmt = NULL;
> + output = NULL;
> for(;;) {
> - c = getopt(argc, argv, "f:h");
> + int option_index = 0;
> + static struct option long_options[] = {
> + {"help", no_argument, 0, 'h'},
> + {"format", required_argument, 0, 'f'},
> + {"output", required_argument, 0, 'm'},
I would define a constant, such as 'enum {OPTION_FORMAT=256};', so that
you don't risk future confusion...
> + {0, 0, 0, 0}
> + };
> + c = getopt_long(argc, argv, "f:h",
...if we later do add an 'm' short option.
> + long_options, &option_index);
> if (c == -1) {
> break;
> }
> @@ -1128,6 +1299,9 @@ static int img_info(int argc, char **argv)
> case 'f':
> fmt = optarg;
> break;
> + case 'm':
Given the above, this would reuse your new named value.
> @@ -1135,52 +1309,42 @@ static int img_info(int argc, char **argv)
> }
> filename = argv[optind++];
>
> + if (output && !strncmp(output, "json", strlen("json"))) {
Why strncmp? It ignores trailing garbage (as in --output=jsonoops).
Stick to strcmp.
> + json = true;
> + } else if (output && !strncmp(output, "human", strlen("human"))) {
And again.
> + human = true;
> + } else {
> + fprintf(stderr,
> + "Error: --output must be used with human or json as argument.\n");
> + return 1;
> + }
If we get here, and --output=... was not given, then both human and json
are false. That's a problem, since...
> +
> + if (human) {
> + dump_human_image_info(info);
> + dump_snapshots(bs);
> }
> - bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
> - if (backing_filename[0] != '\0') {
> - bdrv_get_full_backing_filename(bs, backing_filename2,
> - sizeof(backing_filename2));
> - printf("backing file: %s", backing_filename);
> - if (strcmp(backing_filename, backing_filename2) != 0) {
> - printf(" (actual path: %s)", backing_filename2);
> - }
> - putchar('\n');
> +
> + if (json) {
> + collect_snapshots(bs, info);
> + dump_json_image_info(info);
> }
> - dump_snapshots(bs);
> +
> + qapi_free_ImageInfo(info);
...you will end up with no output. You want to default to human output
for back-compat to older usage.
--
Eric Blake eblake@redhat.com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 620 bytes --]
prev parent reply other threads:[~2012-08-22 14:27 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-08-22 12:45 [Qemu-devel] [PATCH V4 0/2] Add JSON output to qemu-img info Benoît Canet
2012-08-22 12:45 ` [Qemu-devel] [PATCH V4 1/2] qapi: Add SnapshotInfo and ImageInfo Benoît Canet
2012-08-22 14:03 ` Eric Blake
2012-08-22 14:32 ` Benoît Canet
2012-08-22 14:56 ` Eric Blake
2012-08-22 15:51 ` Benoît Canet
2012-08-23 7:34 ` Benoît Canet
2012-08-23 7:38 ` Benoît Canet
2012-08-22 12:45 ` [Qemu-devel] [PATCH V4 2/2] qemu-img: Add json output option to the info command Benoît Canet
2012-08-22 14:26 ` Eric Blake [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5034EC26.2070604@redhat.com \
--to=eblake@redhat.com \
--cc=aliguori@us.ibm.com \
--cc=benoit.canet@gmail.com \
--cc=benoit@irqsave.net \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@linux.vnet.ibm.com \
--cc=xiawenc@linux.vnet.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.