aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-11-22 22:15:51 +0100
committerMatěj Cepl <mcepl@cepl.eu>2024-11-03 00:51:41 +0100
commitdbb9526b247f1d197ec9a524e34f6ca4c25cdd4a (patch)
treee5e4d4bbddf2d76029e3f42567cc8b500a3af0ad /commands
parentfb705bb8f4ab7e44900520e018518d12885e0eca (diff)
downloadgit-bug-dbb9526b247f1d197ec9a524e34f6ca4c25cdd4a.tar.gz
commands: add a command to export bugs as raw operations (NDJSON)
Diffstat (limited to 'commands')
-rw-r--r--commands/export.go92
-rw-r--r--commands/root.go14
2 files changed, 106 insertions, 0 deletions
diff --git a/commands/export.go b/commands/export.go
new file mode 100644
index 00000000..8a770fc1
--- /dev/null
+++ b/commands/export.go
@@ -0,0 +1,92 @@
+package commands
+
+import (
+ "encoding/json"
+
+ "github.com/spf13/cobra"
+
+ "github.com/MichaelMure/git-bug/bug"
+ "github.com/MichaelMure/git-bug/entity"
+)
+
+type exportOptions struct {
+ queryOptions
+}
+
+func newExportCommand() *cobra.Command {
+ env := newEnv()
+ options := exportOptions{}
+
+ cmd := &cobra.Command{
+ Use: "export [QUERY]",
+ Short: "Export bugs as a series of operations.",
+ Long: `Export bugs as a series of operations.
+
+The output format is NDJSON (Newline delimited JSON).
+
+You can pass an additional query to filter and order the list. This query can be expressed either with a simple query language, flags, a natural language full text search, or a combination of the aforementioned.`,
+ Example: `See ls`,
+ PreRunE: loadBackend(env),
+ PostRunE: closeBackend(env),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return runExport(env, options, args)
+ },
+ }
+
+ flags := cmd.Flags()
+ flags.SortFlags = false
+
+ flags.StringSliceVarP(&options.statusQuery, "status", "s", nil,
+ "Filter by status. Valid values are [open,closed]")
+ flags.StringSliceVarP(&options.authorQuery, "author", "a", nil,
+ "Filter by author")
+ flags.StringSliceVarP(&options.participantQuery, "participant", "p", nil,
+ "Filter by participant")
+ flags.StringSliceVarP(&options.actorQuery, "actor", "A", nil,
+ "Filter by actor")
+ flags.StringSliceVarP(&options.labelQuery, "label", "l", nil,
+ "Filter by label")
+ flags.StringSliceVarP(&options.titleQuery, "title", "t", nil,
+ "Filter by title")
+ flags.StringSliceVarP(&options.noQuery, "no", "n", nil,
+ "Filter by absence of something. Valid values are [label]")
+ flags.StringVarP(&options.sortBy, "by", "b", "creation",
+ "Sort the results by a characteristic. Valid values are [id,creation,edit]")
+ flags.StringVarP(&options.sortDirection, "direction", "d", "asc",
+ "Select the sorting direction. Valid values are [asc,desc]")
+
+ return cmd
+}
+
+func runExport(env *Env, opts exportOptions, args []string) error {
+ q, err := makeQuery(args, &opts.queryOptions)
+ if err != nil {
+ return err
+ }
+
+ allIds := env.backend.QueryBugs(q)
+
+ out := json.NewEncoder(env.out)
+
+ for _, id := range allIds {
+ b, err := env.backend.ResolveBug(id)
+ if err != nil {
+ return err
+ }
+
+ wrapper := struct {
+ Id entity.Id `json:"id"`
+ Operations []bug.Operation `json:"operations"`
+ }{
+ Id: id,
+ Operations: b.Snapshot().Operations,
+ }
+
+ err = out.Encode(wrapper)
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
diff --git a/commands/root.go b/commands/root.go
index e966751c..3cda27b5 100644
--- a/commands/root.go
+++ b/commands/root.go
@@ -79,6 +79,20 @@ the same git remote you are already using to collaborate with other people.
cmd.AddCommand(newVersionCommand(env))
cmd.AddCommand(newWipeCommand(env))
+ // Added with export NDJSON
+ cmd.AddCommand(newAddCommand(env))
+ cmd.AddCommand(newCommentCommand(env))
+ cmd.AddCommand(newDeselectCommand(env))
+ cmd.AddCommand(newExportCommand(env))
+ cmd.AddCommand(newLsCommand(env))
+ cmd.AddCommand(newLsIdCommand(env))
+ cmd.AddCommand(newLsLabelCommand(env))
+ cmd.AddCommand(newRmCommand(env))
+ cmd.AddCommand(newSelectCommand(env))
+ cmd.AddCommand(newShowCommand(env))
+ cmd.AddCommand(newStatusCommand(env))
+ cmd.AddCommand(newTitleCommand(env))
+
return cmd
}