unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Subject: [PATCH 01/10] cli: Framework for structured output versioning
Date: Sat,  1 Dec 2012 21:39:53 -0500	[thread overview]
Message-ID: <1354416002-3557-1-git-send-email-amdragon@mit.edu> (raw)

Currently there is a period of pain whenever we make
backward-incompatible changes to the structured output format.  This
series of patches introduces a way for CLI callers to request a
specific schema version on the command line and to determine if the
CLI does not supported the requested version (and perhaps present a
useful diagnostic to the user).  Since the caller requests a schema
version, it's also possible for the CLI to support multiple
incompatible versions simultaneously, unlike the alternate approach of
including version information in the output.

This patch lays the groundwork by introducing a versioning convention,
standard exit codes, and a utility function to check the requested
version and produce standardized diagnostic messages and exit
statuses.
---
 Makefile.local   |    1 +
 notmuch-client.h |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 notmuch.c        |    3 +++
 schema.c         |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+)
 create mode 100644 schema.c

diff --git a/Makefile.local b/Makefile.local
index 2b91946..bfed50e 100644
--- a/Makefile.local
+++ b/Makefile.local
@@ -274,6 +274,7 @@ notmuch_client_srcs =		\
 	query-string.c		\
 	mime-node.c		\
 	crypto.c		\
+	schema.c		\
 
 notmuch_client_modules = $(notmuch_client_srcs:.c=.o)
 
diff --git a/notmuch-client.h b/notmuch-client.h
index ae9344b..14e7363 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -117,6 +117,52 @@ chomp_newline (char *str)
 	str[strlen(str)-1] = '\0';
 }
 
+/* Exit status code indicating the requested schema version is too old
+ * (support for that version has been dropped).  CLI code should use
+ * notmuch_exit_if_unsupported_schema rather than directly exiting
+ * with this code.
+ */
+#define NOTMUCH_EXIT_SCHEMA_TOO_OLD 20
+/* Exit status code indicating the requested schema version is newer
+ * than the version supported by the CLI.  CLI code should use
+ * notmuch_exit_if_unsupported_schema rather than directly exiting
+ * with this code.
+ */
+#define NOTMUCH_EXIT_SCHEMA_TOO_NEW 21
+
+/* The version number of the current structured output schema.
+ * Requests for schema versions above this will return an error.
+ * Backwards-incompatible changes such as removing map fields,
+ * changing the meaning of map fields, or changing the meanings of
+ * list elements should increase this.  New map fields can be added
+ * without increasing this.  Note that different notmuch commands may
+ * support different backwards-compatible version numbers, but the
+ * "current" version is consistent across all parts of the schema.
+ */
+#define NOTMUCH_SCHEMA_CUR 1
+
+/* The schema version requested by the caller on the command line.  If
+ * no schema version is requested, this should be set to
+ * NOTMUCH_SCHEMA_CUR.  This is global---rather than per
+ * command---because commands can share structured output code.
+ */
+extern int notmuch_schema_version;
+
+/* Commands that support structured output should support the
+ * following argument
+ *  { NOTMUCH_OPT_INT, &notmuch_schema_version, "use-schema", 0, 0 }
+ * and should invoke notmuch_exit_if_unsupported_schema to check
+ * against the per-command minimum supported schema version and the
+ * global maximum supported schema version.  If notmuch_schema_version
+ * is outside the supported range, this will print a detailed
+ * diagnostic message for the user and exit with
+ * NOTMUCH_EXIT_SCHEMA_TOO_{OLD,NEW} to inform the invoking program of
+ * the problem.
+ */
+void
+notmuch_exit_if_unsupported_schema (const char *command,
+				    int min_schema_version);
+
 notmuch_crypto_context_t *
 notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol);
 
diff --git a/notmuch.c b/notmuch.c
index 477a09c..ed860f2 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -242,6 +242,9 @@ main (int argc, char *argv[])
     g_mime_init (0);
     g_type_init ();
 
+    /* Globally default to the current schema version. */
+    notmuch_schema_version = NOTMUCH_SCHEMA_CUR;
+
     if (argc == 1)
 	return notmuch (local);
 
diff --git a/schema.c b/schema.c
new file mode 100644
index 0000000..4b6c4fa
--- /dev/null
+++ b/schema.c
@@ -0,0 +1,46 @@
+/* notmuch - Not much of an email program, (just index and search)
+ *
+ * This file is part of notmuch.
+ *
+ * Copyright © 2012 Austin Clements
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see http://www.gnu.org/licenses/ .
+ *
+ * Author: Austin Clements <aclements@csail.mit.edu>
+ */
+
+#include "notmuch-client.h"
+
+int notmuch_schema_version;
+
+void
+notmuch_exit_if_unsupported_schema (const char *command,
+				    int min_schema_version)
+{
+    if (notmuch_schema_version > NOTMUCH_SCHEMA_CUR) {
+	fprintf (stderr, "\
+A caller requested a newer output format (schema version %d) than\n\
+supported by the notmuch CLI (which supports up to version %d).  You\n\
+may need to upgrade your notmuch CLI.\n",
+		 notmuch_schema_version, NOTMUCH_SCHEMA_CUR);
+	exit (NOTMUCH_EXIT_SCHEMA_TOO_NEW);
+    } else if (notmuch_schema_version < min_schema_version) {
+	fprintf (stderr, "\
+A caller requested an older output format (schema version %d) than\n\
+supported by the %s command of the notmuch CLI (which requires at\n\
+least version %d).  You may need to upgrade your notmuch front-end.\n",
+		 notmuch_schema_version, command, min_schema_version);
+	exit (NOTMUCH_EXIT_SCHEMA_TOO_OLD);
+    }
+}
-- 
1.7.10.4

             reply	other threads:[~2012-12-02  2:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-02  2:39 Austin Clements [this message]
2012-12-02  2:39 ` [PATCH 02/10] search: Support --use-schema Austin Clements
2012-12-02  2:39 ` [PATCH 03/10] show: " Austin Clements
2012-12-02  2:39 ` [PATCH 04/10] reply: " Austin Clements
2012-12-02  2:39 ` [PATCH 05/10] test: Sanity tests for --use-schema argument Austin Clements
2012-12-02  2:39 ` [PATCH 06/10] emacs: Fix bug in resynchronizing after a JSON parse error Austin Clements
2012-12-08  8:32   ` Mark Walters
2012-12-02  2:39 ` [PATCH 07/10] emacs: Use --use-schema for search Austin Clements
2012-12-08  8:48   ` Mark Walters
2012-12-13  1:43     ` Austin Clements
2012-12-13 11:00       ` Mark Walters
2012-12-02  2:40 ` [PATCH 08/10] emacs: Factor out synchronous notmuch JSON invocations Austin Clements
2012-12-02  2:40 ` [PATCH 09/10] emacs: Improve error handling for notmuch-call-notmuch-json Austin Clements
2012-12-02  2:40 ` [PATCH 10/10] emacs: Use --use-schema for show and reply Austin Clements
2012-12-03  0:58 ` [PATCH 00/10] CLI output versioning Austin Clements
2012-12-08  9:29   ` Mark Walters
2012-12-13  1:46     ` Austin Clements

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

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1354416002-3557-1-git-send-email-amdragon@mit.edu \
    --to=amdragon@mit.edu \
    --cc=notmuch@notmuchmail.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.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).