Skip to content

Commit 4415f50

Browse files
authored
Add import command (#61)
- Generate import client from swagger JSON. - Mock client add import interface. - Fix bug the time.After() never gets called. https://stackoverflow.com/questions/37691887/why-does-time-after-never-fire-when-it-is-paired-with-a-ticker-in-a-select-block - Add import command. - `start` to start a new import task. - `cancel` to cancel an import task. - `list` to list all import tasks. - `describe` to get import task info.
1 parent 595d8d1 commit 4415f50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+6741
-72
lines changed

.github/license-checker-config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ header:
77
- '**/*.go'
88
paths-ignore:
99
- '**/mock/**'
10+
- 'pkg/tidbcloud/import/**'
1011
comment: on-failure

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ devtools: ## Install dev tools
1818
.PHONY: setup
1919
setup: deps devtools ## Set up dev env
2020

21+
.PHONY: generate-mocks
22+
generate-mocks: ## Generate mock objects
23+
@echo "==> Generating mock objects"
24+
go install github.com/vektra/mockery/v2@latest
25+
mockery --name TiDBCloudClient --recursive --output=internal/mock --outpkg mock --filename api_client.go
26+
27+
# Required to install go-swagger https://goswagger.io/install.html
28+
.PHONY: generate-import-client
29+
generate-import-client: ## Generate import client
30+
@echo "==> Generating import client"
31+
go install github.com/go-swagger/go-swagger/cmd/swagger@latest
32+
swagger generate client -f pkg/tidbcloud/import/import-api.json -A tidbcloud-import -t pkg/tidbcloud/import
33+
2134
.PHONY: addcopy
2235
addcopy: ## Add copyright to all files
2336
@scripts/add-copy.sh

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ require (
1010
github.com/charmbracelet/lipgloss v0.6.0
1111
github.com/emirpasic/gods v1.18.1
1212
github.com/fatih/color v1.13.0
13+
github.com/go-openapi/errors v0.20.3
1314
github.com/go-openapi/runtime v0.24.1
1415
github.com/go-openapi/strfmt v0.21.3
16+
github.com/go-openapi/swag v0.22.3
17+
github.com/go-openapi/validate v0.22.0
1518
github.com/go-resty/resty/v2 v2.7.0
1619
github.com/hashicorp/go-version v1.6.0
1720
github.com/icholy/digest v0.1.15
@@ -23,7 +26,7 @@ require (
2326
github.com/spf13/cobra v1.6.1
2427
github.com/spf13/viper v1.13.0
2528
github.com/stretchr/testify v1.8.0
26-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab
29+
golang.org/x/sys v0.1.0
2730
gopkg.in/yaml.v3 v3.0.1
2831
)
2932

@@ -36,13 +39,10 @@ require (
3639
github.com/davecgh/go-spew v1.1.1 // indirect
3740
github.com/fsnotify/fsnotify v1.5.4 // indirect
3841
github.com/go-openapi/analysis v0.21.2 // indirect
39-
github.com/go-openapi/errors v0.20.3 // indirect
4042
github.com/go-openapi/jsonpointer v0.19.5 // indirect
4143
github.com/go-openapi/jsonreference v0.19.6 // indirect
4244
github.com/go-openapi/loads v0.21.1 // indirect
4345
github.com/go-openapi/spec v0.20.4 // indirect
44-
github.com/go-openapi/swag v0.22.3 // indirect
45-
github.com/go-openapi/validate v0.22.0 // indirect
4646
github.com/hashicorp/hcl v1.0.0 // indirect
4747
github.com/inconshreveable/mousetrap v1.0.1 // indirect
4848
github.com/josharian/intern v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,9 @@ golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBc
536536
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
537537
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
538538
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
539-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
540539
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
540+
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
541+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
541542
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
542543
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
543544
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=

internal/cli/cluster/cluster.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,9 @@ package cluster
1717
import (
1818
"tidbcloud-cli/internal"
1919

20-
"github.com/charmbracelet/lipgloss"
2120
"github.com/spf13/cobra"
2221
)
2322

24-
var (
25-
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
26-
cursorStyle = focusedStyle.Copy()
27-
)
28-
2923
func ClusterCmd(h *internal.Helper) *cobra.Command {
3024
var clusterCmd = &cobra.Command{
3125
Use: "cluster",

internal/cli/cluster/create.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,10 @@ func CreateAndWaitReady(h *internal.Helper, d cloud.TiDBCloudClient, projectID s
297297
ticker := time.NewTicker(1 * time.Second)
298298

299299
fmt.Fprintln(h.IOStreams.Out, "... Waiting for cluster to be ready")
300+
timer := time.After(2 * time.Minute)
300301
for {
301302
select {
302-
case <-time.After(2 * time.Minute):
303+
case <-timer:
303304
return errors.New("Timeout waiting for cluster to be ready, please check status on dashboard.")
304305
case <-ticker.C:
305306
clusterResult, err := d.GetCluster(clusterApi.NewGetClusterParams().
@@ -327,9 +328,10 @@ func CreateAndSpinnerWait(d cloud.TiDBCloudClient, projectID string, clusterDefB
327328
newClusterID := *createClusterResult.GetPayload().ID
328329

329330
ticker := time.NewTicker(1 * time.Second)
331+
timer := time.After(2 * time.Minute)
330332
for {
331333
select {
332-
case <-time.After(2 * time.Minute):
334+
case <-timer:
333335
return ui.Result("Timeout waiting for cluster to be ready, please check status on dashboard.")
334336
case <-ticker.C:
335337
clusterResult, err := d.GetCluster(clusterApi.NewGetClusterParams().
@@ -367,16 +369,16 @@ func initialCreateInputModel() ui.TextInputModel {
367369
var t textinput.Model
368370
for i := range m.Inputs {
369371
t = textinput.New()
370-
t.CursorStyle = cursorStyle
372+
t.CursorStyle = config.CursorStyle
371373
t.CharLimit = 64
372374
f := createClusterField(i)
373375

374376
switch f {
375377
case clusterNameIdx:
376378
t.Placeholder = "Cluster Name"
377379
t.Focus()
378-
t.PromptStyle = focusedStyle
379-
t.TextStyle = focusedStyle
380+
t.PromptStyle = config.FocusedStyle
381+
t.TextStyle = config.FocusedStyle
380382
case passwordIdx:
381383
t.Placeholder = "Password"
382384
t.EchoMode = textinput.EchoPassword

internal/cli/cluster/create_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
type CreateClusterSuite struct {
3535
suite.Suite
3636
h *internal.Helper
37-
mockClient *mock.ApiClient
37+
mockClient *mock.TiDBCloudClient
3838
}
3939

4040
func (suite *CreateClusterSuite) SetupTest() {
@@ -43,7 +43,7 @@ func (suite *CreateClusterSuite) SetupTest() {
4343
}
4444

4545
var pageSize int64 = 10
46-
suite.mockClient = new(mock.ApiClient)
46+
suite.mockClient = new(mock.TiDBCloudClient)
4747
suite.h = &internal.Helper{
4848
Client: func() (cloud.TiDBCloudClient, error) {
4949
return suite.mockClient, nil

internal/cli/cluster/delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ func DeleteCmd(h *internal.Helper) *cobra.Command {
156156
}
157157

158158
ticker := time.NewTicker(1 * time.Second)
159+
timer := time.After(2 * time.Minute)
159160
for {
160161
select {
161-
case <-time.After(2 * time.Minute):
162+
case <-timer:
162163
return errors.New("timeout waiting for deleting cluster, please check status on dashboard")
163164
case <-ticker.C:
164165
_, err := d.GetCluster(clusterApi.NewGetClusterParams().

internal/cli/cluster/delete_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
type DeleteClusterSuite struct {
3434
suite.Suite
3535
h *internal.Helper
36-
mockClient *mock.ApiClient
36+
mockClient *mock.TiDBCloudClient
3737
}
3838

3939
func (suite *DeleteClusterSuite) SetupTest() {
@@ -42,7 +42,7 @@ func (suite *DeleteClusterSuite) SetupTest() {
4242
}
4343

4444
var pageSize int64 = 10
45-
suite.mockClient = new(mock.ApiClient)
45+
suite.mockClient = new(mock.TiDBCloudClient)
4646
suite.h = &internal.Helper{
4747
Client: func() (cloud.TiDBCloudClient, error) {
4848
return suite.mockClient, nil

internal/cli/cluster/describe_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const getClusterResultStr = `{
8080
type DescribeClusterSuite struct {
8181
suite.Suite
8282
h *internal.Helper
83-
mockClient *mock.ApiClient
83+
mockClient *mock.TiDBCloudClient
8484
}
8585

8686
func (suite *DescribeClusterSuite) SetupTest() {
@@ -89,7 +89,7 @@ func (suite *DescribeClusterSuite) SetupTest() {
8989
}
9090

9191
var pageSize int64 = 10
92-
suite.mockClient = new(mock.ApiClient)
92+
suite.mockClient = new(mock.TiDBCloudClient)
9393
suite.h = &internal.Helper{
9494
Client: func() (cloud.TiDBCloudClient, error) {
9595
return suite.mockClient, nil

0 commit comments

Comments
 (0)