-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
222 lines (208 loc) · 4.86 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"hash/maphash"
"math/rand"
"os"
"path"
"github.com/xata/cli/cmd"
"github.com/xata/cli/config"
"github.com/xata/cli/filesystem"
"github.com/urfave/cli/v2"
)
var (
GitCommit string = "unset"
BuildDate string = "unset"
Version string = "unset"
)
func printVersion(c *cli.Context) error {
fmt.Printf("%s %s\n", c.App.Name, Version)
return nil
}
func getSchemaStatus(c *cli.Context) error {
dir := c.String("dir")
settings, err := cmd.ReadSettings(dir)
if err != nil {
return err
}
schemaFile := path.Join(dir, "schema.json")
if settings.SchemaFileFormat == cmd.SettingsYAML {
schemaFile = path.Join(dir, "schema.yaml")
}
exists, errr := filesystem.FileExists(schemaFile)
if errr != nil {
return fmt.Errorf("Error checking if schema file exists: %w.", err)
}
if !exists {
return fmt.Errorf("Schema file [%s] doesn't exist\n", schemaFile)
}
sha, err := cmd.GitGetLastSHA()
if err != nil {
return fmt.Errorf("Error getting git sha: %s", err)
}
localChanges, err := cmd.GitHasLocalChanges(schemaFile)
if err != nil {
return fmt.Errorf("Error checking for local changes: %s", err)
}
status := ""
if localChanges {
status = "modified"
}
fmt.Printf("%s: %s %s\n", schemaFile, sha, status)
return nil
}
func main() {
// initialize global seed via runtime.fastrand
rand.Seed(int64(new(maphash.Hash).Sum64()))
app := &cli.App{
Name: "Xata CLI",
Usage: "Command Line Interface for the xata.io serverless database service.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dir",
Aliases: []string{"d"},
Value: "xata",
Usage: "The `DIR` containing the Xata files, including the schema",
},
&cli.BoolFlag{
Name: "nocolor",
Usage: "Disable colors",
},
&cli.BoolFlag{
Name: "lightbg",
Usage: "Switch color scheme to light colors",
},
&cli.StringFlag{
Name: config.ArgKey,
Usage: "Xata global config directory",
},
},
Commands: []*cli.Command{
{
Name: "auth",
Usage: "Configure CLI authentication",
Subcommands: cmd.GetAuthSubcommands(),
},
{
Name: "init",
Usage: "Start a new database in the current folder.",
Action: cmd.InitCommand,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Overwrite files",
},
&cli.BoolFlag{
Name: "yaml",
Usage: "Use YAML format for the schema file.",
},
&cli.StringFlag{
Name: "dbname",
Usage: "The name of the database to create.",
},
&cli.StringFlag{
Name: "workspaceid",
Usage: "The id of the workspace to use.",
},
},
},
{
Name: "deploy",
Usage: "Deploy database to xata.io",
Action: cmd.DeployCommand,
},
{
Name: "pull",
Usage: "Pull schema file from the remote branch",
Action: cmd.PullCommand,
},
{
Name: "log",
Usage: "Get the log history of your database.",
Action: cmd.HistoryCommand,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "follow",
Usage: "Follow log history across branches.",
},
},
},
{
Name: "shell",
Usage: "Interactive shell to explore your data.",
Action: cmd.ShellCommand,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dbname",
Usage: "The database to select.",
},
&cli.BoolFlag{
Name: "current",
Aliases: []string{"c"},
Usage: "Use current database and branch as a prefix",
},
&cli.BoolFlag{
Name: "nopretty",
Usage: "Disable automatic pretty-printing.",
},
},
},
{
Name: "schema",
Usage: "Operations on the schema file",
Subcommands: []*cli.Command{
{
Name: "status",
Usage: "Get schema status",
Action: getSchemaStatus,
},
},
},
{
Name: "dbs",
Usage: "Operations on databases",
Subcommands: cmd.GetDBsSubcommands(),
},
{
Name: "branches",
Usage: "Operations on branches",
Subcommands: cmd.GetBranchesSubcommands(),
},
{
Name: "workspaces",
Usage: "Operations on workspaces",
Subcommands: cmd.GetWorkspacesSubcommands(),
},
{
Name: "random-data",
Usage: "Insert random data in table.",
Action: cmd.GenerateRandomData,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "records",
Aliases: []string{"n"},
Usage: "Number of records to generate per table.",
Value: 25,
},
&cli.StringSliceFlag{
Name: "table",
Aliases: []string{"t"},
Usage: "Table in which to add data (default: all). Can be specified multiple times.",
},
},
},
{
Name: "version",
Usage: "Build information",
Action: printVersion,
},
},
}
app.EnableBashCompletion = true
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}