Skip to content

Commit

Permalink
feat(mysql): add flag to print connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
thde committed Sep 18, 2024
1 parent 26440e3 commit 0e7c376
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions get/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (

type mySQLCmd struct {
resourceCmd
PrintPassword bool `help:"Print the password of the MySQL User. Requires name to be set." xor:"print"`
PrintUser bool `help:"Print the name of the MySQL User. Requires name to be set." xor:"print"`
PrintPassword bool `help:"Print the password of the MySQL User. Requires name to be set." xor:"print"`
PrintUser bool `help:"Print the name of the MySQL User. Requires name to be set." xor:"print"`
PrintConnectionString bool `help:"Print the connection string of the MySQL instance. Requires name to be set." xor:"print"`

out io.Writer
}
Expand All @@ -28,16 +29,18 @@ func (cmd *mySQLCmd) Run(ctx context.Context, client *api.Client, get *Cmd) erro
}

mysqlList := &storage.MySQLList{}

if err := get.list(ctx, client, mysqlList, matchName(cmd.Name)); err != nil {
return err
}

if len(mysqlList.Items) == 0 {
printEmptyMessage(cmd.out, storage.MySQLKind, client.Project)
return nil
}

if cmd.Name != "" && cmd.PrintConnectionString {
return cmd.printConnectionString(ctx, client, &mysqlList.Items[0])
}

if cmd.Name != "" && cmd.PrintPassword {
return cmd.printPassword(ctx, client, &mysqlList.Items[0])
}
Expand Down Expand Up @@ -77,3 +80,20 @@ func (cmd *mySQLCmd) printPassword(ctx context.Context, client *api.Client, mysq
fmt.Fprintln(cmd.out, pw)
return nil
}

// printConnectionString according to the MySQL documentation:
// https://dev.mysql.com/doc/refman/8.4/en/connecting-using-uri-or-key-value-pairs.html#connecting-using-uri
func (cmd *mySQLCmd) printConnectionString(ctx context.Context, client *api.Client, mysql *storage.MySQL) error {
pw, err := getConnectionSecret(ctx, client, storage.MySQLUser, mysql)
if err != nil {
return err
}

fmt.Fprintf(cmd.out, "mysql://%s:%s@%s",
storage.MySQLUser,
pw,
mysql.Status.AtProvider.FQDN,
)

return nil
}

0 comments on commit 0e7c376

Please sign in to comment.