Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: David Sterba <dsterba@suse.cz>
To: Christoph Heiss <christoph@c8h4.io>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 6/7] btrfs-progs: subvol get-default: implement json format output
Date: Thu, 17 Aug 2023 22:04:49 +0200	[thread overview]
Message-ID: <20230817200449.GX2420@twin.jikos.cz> (raw)
In-Reply-To: <20230813094555.106052-7-christoph@c8h4.io>

On Sun, Aug 13, 2023 at 11:45:01AM +0200, Christoph Heiss wrote:
> Implements JSON-formatted output for the `subvolume get-default` command
> using the `--format json` global option, much like it is implemented for
> other commands.
> 
> Signed-off-by: Christoph Heiss <christoph@c8h4.io>
> ---
>  cmds/subvolume.c | 27 +++++++++++++++++++++++----
>  1 file changed, 23 insertions(+), 4 deletions(-)
> 
> diff --git a/cmds/subvolume.c b/cmds/subvolume.c
> index cb863ac7..f7076655 100644
> --- a/cmds/subvolume.c
> +++ b/cmds/subvolume.c
> @@ -701,6 +701,8 @@ static DEFINE_SIMPLE_COMMAND(subvolume_snapshot, "snapshot");
>  static const char * const cmd_subvolume_get_default_usage[] = {
>  	"btrfs subvolume get-default <path>",
>  	"Get the default subvolume of a filesystem",
> +	HELPINFO_INSERT_GLOBALS,
> +	HELPINFO_INSERT_FORMAT,
>  	NULL
>  };
>  
> @@ -712,6 +714,7 @@ static int cmd_subvolume_get_default(const struct cmd_struct *cmd, int argc, cha
>  	DIR *dirstream = NULL;
>  	enum btrfs_util_error err;
>  	struct btrfs_util_subvolume_info subvol;
> +	struct format_ctx fctx;
>  	char *path;
>  
>  	clean_args_no_options(cmd, argc, argv);
> @@ -731,7 +734,14 @@ static int cmd_subvolume_get_default(const struct cmd_struct *cmd, int argc, cha
>  
>  	/* no need to resolve roots if FS_TREE is default */
>  	if (default_id == BTRFS_FS_TREE_OBJECTID) {
> -		pr_verbose(LOG_DEFAULT, "ID 5 (FS_TREE)\n");
> +		if (bconf.output_format == CMD_FORMAT_JSON) {
> +			fmt_start(&fctx, btrfs_subvolume_rowspec, 1, 0);
> +			fmt_print(&fctx, "ID", 5);
> +			fmt_end(&fctx);
> +		} else {
> +			pr_verbose(LOG_DEFAULT, "ID 5 (FS_TREE)\n");
> +		}
> +
>  		ret = 0;
>  		goto out;
>  	}
> @@ -748,8 +758,17 @@ static int cmd_subvolume_get_default(const struct cmd_struct *cmd, int argc, cha
>  		goto out;
>  	}
>  
> -	pr_verbose(LOG_DEFAULT, "ID %" PRIu64 " gen %" PRIu64 " top level %" PRIu64 " path %s\n",
> -	       subvol.id, subvol.generation, subvol.parent_id, path);
> +	if (bconf.output_format == CMD_FORMAT_JSON) {
> +		fmt_start(&fctx, btrfs_subvolume_rowspec, 1, 0);
> +		fmt_print(&fctx, "ID", subvol.id);
> +		fmt_print(&fctx, "gen", subvol.generation);
> +		fmt_print(&fctx, "top level", subvol.parent_id);
> +		fmt_print(&fctx, "path", path);
> +		fmt_end(&fctx);

Such block can be in a helper and used for 'list' and 'get-default' so
it's unified.

> +	} else {
> +		pr_verbose(LOG_DEFAULT, "ID %" PRIu64 " gen %" PRIu64 " top level %" PRIu64 " path %s\n",
> +		       subvol.id, subvol.generation, subvol.parent_id, path);

The formatter always prints '\n' at the end of the plain text values, so
with a minor update the same helper can be used to produce the plain
output.

  reply	other threads:[~2023-08-17 20:12 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-13  9:44 [PATCH 0/7] btrfs-progs: implement json output for subvolume subcommands Christoph Heiss
2023-08-13  9:44 ` [PATCH 1/7] btrfs-progs: common: document `time-long` output format Christoph Heiss
2023-08-13  9:44 ` [PATCH 2/7] btrfs-progs: subvol show: remove duplicated quotas error check Christoph Heiss
2023-08-13  9:44 ` [PATCH 3/7] btrfs-progs: subvol show: factor out text printing to own function Christoph Heiss
2023-08-13  9:44 ` [PATCH 4/7] btrfs-progs: subvol: introduce rowspec definition for json output Christoph Heiss
2023-08-13  9:45 ` [PATCH 5/7] btrfs-progs: subvol list: implement json format output Christoph Heiss
2023-08-17 19:59   ` David Sterba
2023-08-13  9:45 ` [PATCH 6/7] btrfs-progs: subvol get-default: " Christoph Heiss
2023-08-17 20:04   ` David Sterba [this message]
2023-08-18 18:49     ` Christoph Heiss
2023-08-13  9:45 ` [PATCH 7/7] btrfs-progs: subvol show: " Christoph Heiss
2023-08-17 20:02   ` David Sterba
2023-08-17 19:34 ` [PATCH 0/7] btrfs-progs: implement json output for subvolume subcommands David Sterba
2023-08-18 18:39   ` Christoph Heiss
2023-08-21 15:19     ` David Sterba

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=20230817200449.GX2420@twin.jikos.cz \
    --to=dsterba@suse.cz \
    --cc=christoph@c8h4.io \
    --cc=linux-btrfs@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox