|
| 1 | +package get |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "text/tabwriter" |
| 7 | + |
| 8 | + "github.com/crossplane/crossplane-runtime/pkg/resource" |
| 9 | + "github.com/ninech/nctl/api" |
| 10 | + "github.com/ninech/nctl/internal/format" |
| 11 | + runtimeclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | +) |
| 13 | + |
| 14 | +type Database struct { |
| 15 | + Namespace, Name, FQDN, Location, Size, Connections string |
| 16 | +} |
| 17 | + |
| 18 | +type databaseCmd struct { |
| 19 | + resourceCmd |
| 20 | + PrintPassword bool `help:"Print the password of the database. Requires name to be set." xor:"print"` |
| 21 | + PrintDatabaseUser bool `help:"Print the database name and user of the database. Requires name to be set." xor:"print"` |
| 22 | + PrintConnectionString bool `help:"Print the connection string of the database. Requires name to be set." xor:"print"` |
| 23 | + |
| 24 | + out io.Writer |
| 25 | +} |
| 26 | + |
| 27 | +func (cmd *databaseCmd) runDatabaseCmd(ctx context.Context, client *api.Client, get *Cmd, |
| 28 | + databaseList runtimeclient.ObjectList, databaseResources []resource.Managed, databaseKind string, |
| 29 | + connectionStringHandler func(context.Context, *api.Client, resource.Managed) error, |
| 30 | + databasesHandler func([]resource.Managed, *Cmd, bool) error, |
| 31 | +) error { |
| 32 | + cmd.out = defaultOut(cmd.out) |
| 33 | + |
| 34 | + if err := get.list(ctx, client, databaseList, api.MatchName(cmd.Name)); err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + if len(databaseResources) == 0 { |
| 39 | + get.printEmptyMessage(cmd.out, databaseKind, client.Project) |
| 40 | + return nil |
| 41 | + } |
| 42 | + |
| 43 | + if cmd.Name != "" && cmd.PrintConnectionString { |
| 44 | + return connectionStringHandler(ctx, client, databaseResources[0]) |
| 45 | + } |
| 46 | + |
| 47 | + if cmd.Name != "" && cmd.PrintDatabaseUser { |
| 48 | + return cmd.printSecret(cmd.out, ctx, client, databaseResources[0], func(db, _ string) string { return db }) |
| 49 | + } |
| 50 | + |
| 51 | + if cmd.Name != "" && cmd.PrintPassword { |
| 52 | + return cmd.printSecret(cmd.out, ctx, client, databaseResources[0], func(_, pw string) string { return pw }) |
| 53 | + } |
| 54 | + |
| 55 | + switch get.Output { |
| 56 | + case full: |
| 57 | + return databasesHandler(databaseResources, get, true) |
| 58 | + case noHeader: |
| 59 | + return databasesHandler(databaseResources, get, false) |
| 60 | + case yamlOut: |
| 61 | + return format.PrettyPrintObjects(databaseResources, format.PrintOpts{}) |
| 62 | + case jsonOut: |
| 63 | + return format.PrettyPrintObjects( |
| 64 | + databaseResources, |
| 65 | + format.PrintOpts{ |
| 66 | + Format: format.OutputFormatTypeJSON, |
| 67 | + JSONOpts: format.JSONOutputOptions{ |
| 68 | + PrintSingleItem: cmd.Name != "", |
| 69 | + }, |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func printDatabases(out io.Writer, get *Cmd, databases []Database, header bool) error { |
| 77 | + w := tabwriter.NewWriter(out, 0, 0, 5, ' ', 0) |
| 78 | + |
| 79 | + if header { |
| 80 | + get.writeHeader(w, "NAME", "FQDN", "LOCATION", "SIZE", "CONNECTIONS") |
| 81 | + } |
| 82 | + |
| 83 | + for _, db := range databases { |
| 84 | + get.writeTabRow(w, |
| 85 | + db.Namespace, |
| 86 | + db.Name, |
| 87 | + db.FQDN, |
| 88 | + db.Location, |
| 89 | + db.Size, |
| 90 | + db.Connections, |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + return w.Flush() |
| 95 | +} |
0 commit comments