Skip to content

Commit

Permalink
fix command to delete passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
Phani Raj committed Aug 13, 2021
1 parent 364d561 commit f1459be
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 99 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/passwords/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
createReq := &ps.DatabaseBranchPasswordRequest{}
cmd := &cobra.Command{
Use: "create <database> <branch> <name>",
Short: "create a password to access a branch's data",
Short: "create password to access a branch's data",
Args: cmdutil.RequiredArgs("database", "branch", "name"),
Aliases: []string{"p"},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
16 changes: 9 additions & 7 deletions internal/cmd/passwords/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"

"github.com/planetscale/planetscale-go/planetscale"
ps "github.com/planetscale/planetscale-go/planetscale"

"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
Expand Down Expand Up @@ -69,15 +69,17 @@ func DeleteCmd(ch *cmdutil.Helper) *cobra.Command {
end := ch.Printer.PrintProgress(fmt.Sprintf("Deleting password %s from %s", printer.BoldBlue(password), printer.BoldBlue(branch)))
defer end()

err = client.Passwords.Delete(ctx, &planetscale.DatabaseBranchPasswordRequest{
Organization: ch.Config.Organization,
Database: database,
Branch: branch,
DisplayName: password,
err = client.Passwords.Delete(ctx, &ps.DeleteDatabaseBranchPasswordRequest{
DatabaseBranchPasswordRequest: ps.DatabaseBranchPasswordRequest{
Organization: ch.Config.Organization,
Database: database,
Branch: branch,
},
PasswordId: password,
})
if err != nil {
switch cmdutil.ErrCode(err) {
case planetscale.ErrNotFound:
case ps.ErrNotFound:
return fmt.Errorf("password %s does not exist in branch %s of %s (organization: %s)",
printer.BoldBlue(password), printer.BoldBlue(branch), printer.BoldBlue(database), printer.BoldBlue(ch.Config.Organization))
default:
Expand Down
43 changes: 23 additions & 20 deletions internal/cmd/passwords/passwords.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,32 @@ func PasswordCmd(ch *cmdutil.Helper) *cobra.Command {
type Passwords []*Password

type Password struct {
PublicID string `header:"id" json:"id"`
Name string `header:"name" json:"name"`
PublicID string `header:"username" json:"username"`
UserName string `header:"username" json:"username"`
Role string `header:"role" json:"role"`
RoleDesc string `header:"role description" json:"-"`
CreatedAt int64 `json:"created_at"`
orig *ps.DatabaseBranchPassword
}

type DeletedPasswords struct {
type DeletedPassword struct {
Name string `header:"name" json:"name"`
Role string `header:"role" json:"role"`
CreatedAt int64 `header:"created_at,timestamp(ms|utc|human)" json:"created_at"`
DeletedAt int64 `header:"deleted_at,timestamp(ms|utc|human)" json:"deleted_at"`
orig *ps.DatabaseBranchPassword
}

type ConnectionStrings struct {
MysqlCLI string `header:"MySQL CLI"`
}

type PasswordWithPlainText struct {
Name string `header:"name" json:"name"`
PublicID string `header:"username" json:"username"`
AccessHostUrl string `header:"access host url" json:"access_host_url"`
Role string `header:"role" json:"role"`
PlainText string `header:"plain text" json:"plain_text"`
ConnectionStrings ConnectionStrings
orig *ps.DatabaseBranchPassword
Name string `header:"name" json:"name"`
PublicID string `header:"username" json:"username"`
AccessHostUrl string `header:"access host url" json:"access_host_url"`
Role string `header:"role" json:"role"`
RoleDesc string `header:"role description" json:"role_description"`
PlainText string `header:"plain text" json:"plain_text"`
ConnectionStrings ps.ConnectionStrings `json:"connection_strings"`
orig *ps.DatabaseBranchPassword
}

func (b *Password) MarshalJSON() ([]byte, error) {
Expand All @@ -82,7 +81,9 @@ func toPassword(password *ps.DatabaseBranchPassword) *Password {
return &Password{
Name: password.Name,
PublicID: password.PublicID,
Role: toRoleDesc(password.Role),
UserName: password.PublicID,
Role: password.Role,
RoleDesc: toRoleDesc(password.Role),
CreatedAt: password.CreatedAt.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)),
orig: password,
}
Expand All @@ -98,12 +99,14 @@ func toPasswords(passwords []*ps.DatabaseBranchPassword) []*Password {

func toPasswordWithPlainText(password *ps.DatabaseBranchPassword) *PasswordWithPlainText {
return &PasswordWithPlainText{
Name: password.Name,
PublicID: password.PublicID,
PlainText: password.PlainText,
AccessHostUrl: password.Branch.AccessHostUrl,
Role: toRoleDesc(password.Role),
orig: password,
Name: password.Name,
PublicID: password.PublicID,
PlainText: password.PlainText,
AccessHostUrl: password.Branch.AccessHostUrl,
Role: password.Role,
RoleDesc: toRoleDesc(password.Role),
orig: password,
ConnectionStrings: password.ConnectionStrings,
}
}

Expand Down
71 changes: 0 additions & 71 deletions internal/cmd/passwords/show.go.txt

This file was deleted.

0 comments on commit f1459be

Please sign in to comment.