|
| 1 | +package get |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/crossplane/crossplane-runtime/pkg/resource" |
| 8 | + storage "github.com/ninech/apis/storage/v1alpha1" |
| 9 | + "github.com/ninech/nctl/api" |
| 10 | +) |
| 11 | + |
| 12 | +type mysqlDatabaseCmd struct { |
| 13 | + databaseCmd |
| 14 | + PrintCharacterSet bool `help:"Print the character set of the MySQL database. Requires name to be set." xor:"print"` |
| 15 | +} |
| 16 | + |
| 17 | +func (cmd *mysqlDatabaseCmd) Run(ctx context.Context, client *api.Client, get *Cmd) error { |
| 18 | + databaseList := &storage.MySQLDatabaseList{} |
| 19 | + databaseResources := make([]resource.Managed, 0) |
| 20 | + |
| 21 | + if err := get.list(ctx, client, databaseList, api.MatchName(cmd.Name)); err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + |
| 25 | + for i := range databaseList.Items { |
| 26 | + databaseResources = append(databaseResources, &databaseList.Items[i]) |
| 27 | + } |
| 28 | + |
| 29 | + if cmd.Name != "" && cmd.PrintCharacterSet { |
| 30 | + return cmd.printMySQLCharacterSet(databaseResources[0].(*storage.MySQLDatabase)) |
| 31 | + } |
| 32 | + |
| 33 | + return cmd.runDatabaseCmd(ctx, client, get, |
| 34 | + databaseList, databaseResources, storage.MySQLDatabaseKind, |
| 35 | + func(ctx context.Context, client *api.Client, mg resource.Managed) error { |
| 36 | + return cmd.printConnectionString(ctx, client, mg.(*storage.MySQLDatabase)) |
| 37 | + }, |
| 38 | + func(res []resource.Managed, get *Cmd, header bool) error { |
| 39 | + dbs := make([]storage.MySQLDatabase, len(res)) |
| 40 | + for i, r := range res { |
| 41 | + dbs[i] = *r.(*storage.MySQLDatabase) |
| 42 | + } |
| 43 | + |
| 44 | + return cmd.printMySQLDatabases(dbs, get, header) |
| 45 | + }, |
| 46 | + ) |
| 47 | +} |
| 48 | + |
| 49 | +func (cmd *mysqlDatabaseCmd) printMySQLDatabases(list []storage.MySQLDatabase, get *Cmd, header bool) error { |
| 50 | + databases := make([]Database, len(list)) |
| 51 | + for i, db := range list { |
| 52 | + databases[i] = Database{ |
| 53 | + Namespace: db.Namespace, |
| 54 | + Name: db.Name, |
| 55 | + FQDN: db.Status.AtProvider.FQDN, |
| 56 | + Location: string(db.Spec.ForProvider.Location), |
| 57 | + Size: db.Status.AtProvider.Size.String(), |
| 58 | + Connections: fmt.Sprintf("%d", db.Status.AtProvider.Connections), |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return printDatabases(cmd.out, get, databases, header) |
| 63 | +} |
| 64 | + |
| 65 | +// printConnectionString according to the MySQL documentation: |
| 66 | +// https://dev.mysql.com/doc/refman/8.4/en/connecting-using-uri-or-key-value-pairs.html#connecting-using-uri |
| 67 | +func (cmd *mysqlDatabaseCmd) printConnectionString(ctx context.Context, client *api.Client, mdb *storage.MySQLDatabase) error { |
| 68 | + secrets, err := getConnectionSecretMap(ctx, client, mdb) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + for db, pw := range secrets { |
| 74 | + fmt.Fprintf(cmd.out, "mysql://%s:%s@%s/%s", |
| 75 | + db, |
| 76 | + pw, |
| 77 | + mdb.Status.AtProvider.FQDN, |
| 78 | + db, |
| 79 | + ) |
| 80 | + break |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (cmd *mysqlDatabaseCmd) printMySQLCharacterSet(mdb *storage.MySQLDatabase) error { |
| 87 | + fmt.Fprintln(defaultOut(cmd.out), mdb.Spec.ForProvider.CharacterSet.Name) |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
0 commit comments