-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
89 lines (76 loc) · 2.69 KB
/
main.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
//
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// spanner-dump is a command line tool for exporting a Cloud Spanner database in text format.
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/jessevdk/go-flags"
)
type options struct {
ProjectId string `short:"p" long:"project" env:"SPANNER_PROJECT_ID" description:"(required) GCP Project ID."`
InstanceId string `short:"i" long:"instance" env:"SPANNER_INSTANCE_ID" description:"(required) Cloud Spanner Instance ID."`
DatabaseId string `short:"d" long:"database" env:"SPANNER_DATABASE_ID" description:"(required) Cloud Spanner Database ID."`
Tables string `long:"tables" description:"comma-separated table names, e.g. \"table1,table2\" "`
NoDDL bool `long:"no-ddl" description:"No DDL information."`
NoData bool `long:"no-data" description:"Do not dump data."`
Timestamp string `long:"timestamp" description:"Timestamp for database snapshot in the RFC 3339 format."`
BulkSize uint `long:"bulk-size" description:"Bulk size for values in a single INSERT statement."`
}
func main() {
var opts options
if _, err := flags.Parse(&opts); err != nil {
exitf("Invalid options\n")
}
if opts.ProjectId == "" || opts.InstanceId == "" || opts.DatabaseId == "" {
exitf("Missing parameters: -p, -i, -d are required\n")
}
var timestamp *time.Time
if opts.Timestamp != "" {
t, err := time.Parse(time.RFC3339, opts.Timestamp)
if err != nil {
exitf("Failed to parse timestamp: %v\n", err)
}
timestamp = &t
}
var tables []string
if opts.Tables != "" {
tables = strings.Split(opts.Tables, ",")
}
ctx := context.Background()
dumper, err := NewDumper(ctx, opts.ProjectId, opts.InstanceId, opts.DatabaseId, os.Stdout, timestamp, opts.BulkSize, tables)
if err != nil {
exitf("Failed to create dumper: %v\n", err)
}
defer dumper.Cleanup()
if !opts.NoDDL {
if err := dumper.DumpDDLs(ctx); err != nil {
exitf("Failed to dump DDLs: %v\n", err)
}
}
if !opts.NoData {
if err := dumper.DumpTables(ctx); err != nil {
exitf("Failed to dump tables: %v\n", err)
}
}
}
func exitf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(1)
}