diff mbox series

[6/7] btrfs-progs: subvol get-default: implement json format output

Message ID 20230813094555.106052-7-christoph@c8h4.io (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: implement json output for subvolume subcommands | expand

Commit Message

Christoph Heiss Aug. 13, 2023, 9:45 a.m. UTC
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(-)

Comments

David Sterba Aug. 17, 2023, 8:04 p.m. UTC | #1
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.
Christoph Heiss Aug. 18, 2023, 6:49 p.m. UTC | #2
Thanks for the feedback.

On Thu, Aug 17, 2023 at 10:04:49PM +0200, David Sterba wrote:
>
> 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
> > [..]
> > @@ -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.
Looks easy enough, I'll get on it.

>
> > +	} 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.
Ditto.
diff mbox series

Patch

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);
+	} else {
+		pr_verbose(LOG_DEFAULT, "ID %" PRIu64 " gen %" PRIu64 " top level %" PRIu64 " path %s\n",
+		       subvol.id, subvol.generation, subvol.parent_id, path);
+	}
 
 	free(path);
 
@@ -758,7 +777,7 @@  out:
 	close_file_or_dir(fd, dirstream);
 	return ret;
 }
-static DEFINE_SIMPLE_COMMAND(subvolume_get_default, "get-default");
+static DEFINE_COMMAND_WITH_FLAGS(subvolume_get_default, "get-default", CMD_FORMAT_JSON);
 
 static const char * const cmd_subvolume_set_default_usage[] = {
 	"btrfs subvolume set-default <subvolume>\n"