aboutsummaryrefslogtreecommitdiffstats
path: root/commands/export.go
blob: aacad1e5aa2f2f5858d8d3cae0c7aed2f228915e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package commands

import (
	"strings"
	"encoding/json"

	"github.com/spf13/cobra"

	"github.com/git-bug/git-bug/entities/bug"
	"github.com/git-bug/git-bug/entity"
	"github.com/git-bug/git-bug/commands/execenv"
	"github.com/git-bug/git-bug/query"
)

type exportOptions struct {
	actorQuery          []string
	authorQuery         []string
	labelQuery          []string
	noQuery             []string
	participantQuery    []string
	queryOptions        []string
	statusQuery         []string
	titleQuery          []string
	sortBy              string
	sortDirection       string
}

func newExportCommand(env *execenv.Env) *cobra.Command {
	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:  execenv.LoadBackend(env),
		PostRunE: execenv.CloseBackend(env, 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 *execenv.Env, opts exportOptions, args []string) error {
	var q *query.Query
	var err error

	if len(args) >= 1 {
		q, err = query.Parse(strings.Join(args, " "))
		if err != nil {
			return err
		}
	} else {
		q = query.NewQuery()
	}

	// FIXME we are throwing away opts!
	allIds, err := env.Backend.Bugs().Query(q)
	if err != nil {
		return err
	}

	out := json.NewEncoder(env.Out)

	for _, id := range allIds {
		b, err := env.Backend.Bugs().Resolve(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
}