-
Notifications
You must be signed in to change notification settings - Fork 224
/
export.go
107 lines (87 loc) · 2.02 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
package lfsc
import (
"context"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)
func newExport() *cobra.Command {
const (
long = `Exports the current state of the database to a file.`
short = "Export LiteFS Cloud database"
usage = "export"
)
cmd := command.New(usage, short, long, runExport,
command.RequireSession,
command.LoadAppNameIfPresentNoFlag,
)
cmd.Args = cobra.NoArgs
flag.Add(cmd,
flag.String{
Name: "output",
Description: "Output filename",
},
flag.Bool{
Name: "force",
Shorthand: "f",
Description: "Overwrite output file if it already exists",
},
urlFlag(),
clusterFlag(),
databaseFlag(),
flag.Org(),
flag.JSONOutput(),
)
return cmd
}
func runExport(ctx context.Context) error {
out := iostreams.FromContext(ctx).Out
output := flag.GetString(ctx, "output")
clusterName := flag.GetString(ctx, "cluster")
if clusterName == "" {
return errors.New("required: --cluster NAME")
}
databaseName := flag.GetString(ctx, "database")
if databaseName == "" {
return errors.New("required: --database NAME")
}
if output == "" {
return errors.New("required: --output PATH")
}
force := flag.GetBool(ctx, "force")
if !force {
if _, err := os.Stat(output); err == nil {
return errors.New("output file already exists, use --force to overwrite")
}
}
lfscClient, err := newLFSCClient(ctx, clusterName)
if err != nil {
return err
}
startTime := time.Now()
rc, err := lfscClient.ExportDatabase(ctx, databaseName)
if err != nil {
return err
}
defer func() { _ = rc.Close() }()
f, err := os.Create(output)
if err != nil {
return err
}
defer func() { _ = f.Close() }()
if _, err := io.Copy(f, rc); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
fmt.Fprintf(out, "Database exported to %s in %s\n",
output, time.Since(startTime).Truncate(time.Millisecond))
return nil
}