Skip to content

Instantly share code, notes, and snippets.

@YouSysAdmin
Last active September 17, 2024 21:29
Show Gist options
  • Save YouSysAdmin/a5f01111d0f4d1a15a41d3472c1ce7b5 to your computer and use it in GitHub Desktop.
Save YouSysAdmin/a5f01111d0f4d1a15a41d3472c1ce7b5 to your computer and use it in GitHub Desktop.
// Database Information about RDS instance
type Database struct {
// Database identifier
Identifier string
// Database ARN
Arn string
// Database engine (mysql2, postgres, etc.)
Engine string
// Database engine version
EngineVersion string
// .........
// .........
// Database master user name
MasterUser string
// Database master user password
MasterPassword string
// Database tags
Tags map[string]string
}
// PrintDatabase outputs full information about database instance to STDOUT as ASCII
// ┌───────────────────────────────────────────────────────┐
// | Parameter | Value |
// | ----------------------------------------------------- |
// | Identifier | example-stage |
// | ..................................................... |
// └───────────────────────────────────────────────────────┘
func PrintDatabase(db Database) error {
tableData := pterm.TableData{
{"Parameter", "Value"},
}
for i := 0; i < reflect.TypeOf(db).NumField(); i++ {
var key string
var value string
key = reflect.TypeOf(db).Field(i).Name // structure key name
valueType := reflect.TypeOf(db).Field(i).Type.Kind() // structure value type
switch valueType {
// All Int and Int64 values
case reflect.Int, reflect.Int64:
value = strconv.FormatInt(reflect.ValueOf(db).Field(i).Int(), 10)
// All map[string]string
// make "key: value" string by elements and join all "\n"
case reflect.MapOf(reflect.TypeOf("string"), reflect.TypeOf("string")).Kind():
var v []string
for _, k := range reflect.ValueOf(db).Field(i).MapKeys() {
v = append(v, fmt.Sprintf("%s: %s", k.String(), reflect.ValueOf(db).Field(i).MapIndex(k).String()))
}
value = strings.Join(v, "\n")
// All Bool values
// true = Enabled (green)
// false = Disabled (red)
case reflect.Bool:
if reflect.ValueOf(db).Field(i).Bool() {
value = pterm.Green("Enabled")
} else {
value = pterm.Red("Disabled")
}
default:
value = reflect.ValueOf(db).Field(i).String()
}
// Colorize Database Status
if key == "Status" {
value = cases.Title(language.Tag{}).String(value)
switch value {
case "Available":
value = pterm.Green(value)
case "Stopped":
value = pterm.Red(value)
default:
break
}
}
// If a value is not empty and not equal to "0", then the data is added to the table.
if value != "" && value != "0" {
tableData = append(tableData, []string{key, value})
}
}
// add connection string information
tableData = append(tableData, []string{"Connection", db.ConnectionString()})
err := pterm.DefaultTable.WithHasHeader().
WithData(tableData).
WithBoxed(true).
WithHeaderRowSeparator("-").
WithRowSeparator(".").
Render()
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment