Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add create branch from timestamp #219

Merged
merged 9 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/generate_doc/ticloud_serverless_branch_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ ticloud serverless branch create [flags]
### Options

```
-c, --cluster-id string The ID of the cluster, in which the branch will be created.
-n, --display-name string The displayName of the branch to be created.
-h, --help help for create
--parent-id string The ID of the branch parent, default is cluster id.
-c, --cluster-id string The ID of the cluster, in which the branch will be created.
-n, --display-name string The displayName of the branch to be created.
-h, --help help for create
--parent-id string The ID of the branch parent, default is cluster id.
--parent-timestamp string The timestamp of the parent branch, default is current time. (RFC3339 format, e.g., 2024-01-01T00:00:00Z)
```

### Options inherited from parent commands
Expand Down
42 changes: 36 additions & 6 deletions internal/cli/serverless/branch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/fatih/color"
"github.com/go-openapi/strfmt"
"github.com/juju/errors"
"github.com/spf13/cobra"
)

var createBranchField = map[string]int{
flag.DisplayName: 0,
flag.DisplayName: 0,
flag.ParentTimestamp: 1,
}

const (
Expand All @@ -52,6 +54,14 @@ func (c CreateOpts) NonInteractiveFlags() []string {
return []string{
flag.DisplayName,
flag.ClusterID,
flag.ParentTimestamp,
}
}

func (c CreateOpts) RequiredFlags() []string {
return []string{
flag.ClusterID,
flag.DisplayName,
}
}

Expand All @@ -66,7 +76,7 @@ func (c *CreateOpts) MarkInteractive(cmd *cobra.Command) error {
}
// Mark required flags
if !c.interactive {
for _, fn := range flags {
for _, fn := range c.RequiredFlags() {
err := cmd.MarkFlagRequired(fn)
if err != nil {
return err
Expand Down Expand Up @@ -108,6 +118,8 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
var branchName string
var clusterId string
var parentID string
var parentTimestampStr string
var parentTimestamp *strfmt.DateTime
if opts.interactive {
if !h.IOStreams.CanPrompt {
return errors.New("The terminal doesn't support interactive mode, please use non-interactive mode")
Expand Down Expand Up @@ -138,6 +150,7 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
if len(branchName) == 0 {
return errors.New("branch name is required")
}
parentTimestampStr = inputModel.(ui.TextInputModel).Inputs[createBranchField[flag.ParentTimestamp]].Value()
} else {
// non-interactive mode, get values from flags
var err error
Expand All @@ -153,11 +166,25 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
if err != nil {
return errors.Trace(err)
}
parentTimestampStr, err = cmd.Flags().GetString(flag.ParentTimestamp)
if err != nil {
return errors.Trace(err)
}
}

if len(parentTimestampStr) != 0 {
t, err := time.Parse(time.RFC3339, parentTimestampStr)
if err != nil {
return errors.New("Invalid parent timestamp format, please use RFC3339 format")
}
tFormated := strfmt.DateTime(t)
parentTimestamp = &tFormated
}

params := branchApi.NewBranchServiceCreateBranchParams().WithClusterID(clusterId).WithBranch(&branchModel.V1beta1Branch{
DisplayName: &branchName,
ParentID: parentID,
DisplayName: &branchName,
ParentID: parentID,
ParentTimestamp: parentTimestamp,
}).WithContext(ctx)

if h.IOStreams.CanPrompt {
Expand All @@ -179,6 +206,7 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
createCmd.Flags().StringP(flag.DisplayName, flag.DisplayNameShort, "", "The displayName of the branch to be created.")
createCmd.Flags().StringP(flag.ClusterID, flag.ClusterIDShort, "", "The ID of the cluster, in which the branch will be created.")
createCmd.Flags().StringP(flag.ParentID, "", "", "The ID of the branch parent, default is cluster id.")
createCmd.Flags().StringP(flag.ParentTimestamp, "", "", "The timestamp of the parent branch, default is current time. (RFC3339 format, e.g., 2024-01-01T00:00:00Z)")
return createCmd
}

Expand Down Expand Up @@ -280,9 +308,11 @@ func initialCreateBranchInputModel() ui.TextInputModel {
t.Focus()
t.PromptStyle = config.FocusedStyle
t.TextStyle = config.FocusedStyle

m.Inputs[v] = t
case flag.ParentTimestamp:
timestampExample := time.Now().Format(time.RFC3339)
t.Placeholder = fmt.Sprintf("Parent Timestamp (optional, e.g., %s)", timestampExample)
}
m.Inputs[v] = t
}
return m
}
Expand Down
1 change: 1 addition & 0 deletions internal/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const (
TableWhere string = "where"
TableFilter string = "filter"
ParentID string = "parent-id"
ParentTimestamp string = "parent-timestamp"
PublicEndpointDisabled string = "disable-public-endpoint"
)

Expand Down
6 changes: 6 additions & 0 deletions pkg/tidbcloud/v1beta1/branch/branch.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@
"parentDisplayName": {
"type": "string",
"description": "OPTIONAL. The parent display name of this branch."
},
"parentTimestamp": {
"type": "string",
"format": "date-time",
"x-nullable": true,
"description": "Optional. The point in time on the parent branch the branch will be created from."
}
},
"title": "Message for branch",
Expand Down
3 changes: 1 addition & 2 deletions pkg/tidbcloud/v1beta1/branch/models/branch_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions pkg/tidbcloud/v1beta1/branch/models/v1beta1_branch.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading