-
Notifications
You must be signed in to change notification settings - Fork 191
/
export.go
143 lines (115 loc) · 3 KB
/
export.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
"go.flipt.io/flipt/internal/ext"
"go.flipt.io/flipt/rpc/flipt"
)
type exportCommand struct {
filename string
address string
token string
namespaces string // comma delimited list of namespaces
allNamespaces bool
}
func newExportCommand() *cobra.Command {
export := &exportCommand{}
cmd := &cobra.Command{
Use: "export",
Short: "Export Flipt data to file/stdout",
RunE: export.run,
}
cmd.Flags().StringVarP(
&export.filename,
"output", "o",
"",
"export to filename (default STDOUT)",
)
cmd.Flags().StringVarP(
&export.address,
"address", "a",
"",
"address of Flipt instance (defaults to direct DB export if not supplied).",
)
cmd.Flags().StringVarP(
&export.token,
"token", "t",
"",
"client token used to authenticate access to Flipt instance.",
)
cmd.Flags().StringVarP(
&export.namespaces,
"namespace", "n",
flipt.DefaultNamespace,
"source namespace for exported resources.",
)
cmd.Flags().StringVar(
&export.namespaces,
"namespaces",
flipt.DefaultNamespace,
"comma-delimited list of namespaces to export from. (mutually exclusive with --all-namespaces)",
)
cmd.Flags().BoolVar(
&export.allNamespaces,
"all-namespaces",
false,
"export all namespaces. (mutually exclusive with --namespaces)",
)
cmd.Flags().StringVar(&providedConfigFile, "config", "", "path to config file")
cmd.MarkFlagsMutuallyExclusive("all-namespaces", "namespaces", "namespace")
// We can ignore the error here since "namespace" will be a flag that exists.
_ = cmd.Flags().MarkDeprecated("namespace", "please use namespaces instead")
return cmd
}
func (c *exportCommand) run(cmd *cobra.Command, _ []string) error {
var (
ctx = cmd.Context()
// default to stdout
out io.Writer = os.Stdout
enc = ext.EncodingYML
)
// export to file
if c.filename != "" {
fi, err := os.Create(c.filename)
if err != nil {
return fmt.Errorf("creating output file: %w", err)
}
defer fi.Close()
fmt.Fprintf(fi, "# exported by Flipt (%s) on %s\n\n", version, time.Now().UTC().Format(time.RFC3339))
out = fi
if extn := filepath.Ext(c.filename); len(extn) > 0 {
// strip off the leading .
enc = ext.Encoding(extn[1:])
}
}
// Use client when remote address is configured.
if c.address != "" {
client, err := fliptClient(c.address, c.token)
if err != nil {
return err
}
return c.export(ctx, enc, out, client)
}
// Otherwise, go direct to the DB using Flipt configuration file.
logger, cfg, err := buildConfig(ctx)
if err != nil {
return err
}
defer func() {
_ = logger.Sync()
}()
server, cleanup, err := fliptServer(logger, cfg)
if err != nil {
return err
}
defer cleanup()
return c.export(ctx, enc, out, server)
}
func (c *exportCommand) export(ctx context.Context, enc ext.Encoding, dst io.Writer, lister ext.Lister) error {
return ext.NewExporter(lister, c.namespaces, c.allNamespaces).Export(ctx, enc, dst)
}