Skip to content

Commit

Permalink
feat - rds logs and view logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Strappazzon C committed Oct 15, 2023
1 parent 450a8c7 commit 71894f7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 44 deletions.
59 changes: 59 additions & 0 deletions aws/database/logs/list/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package list

import (
"fmt"

"github.com/debeando/go-common/aws/rds"
"github.com/debeando/go-common/table"

"github.com/spf13/cobra"
)

var filter string
var sort bool

func NewCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "list [IDENTIFIER] [FILENAME]",
Short: "List all logs about database.",
Example: `
# List logs of specific database instance:
zenit aws database logs test-rds
# List slow query logs of specific database instance:
zenit aws database logs test-rds --filter=slowquery
# Sort list slow query logs of specific database instance:
zenit aws database logs test-rds --filter=slowquery --sort`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 || len(args) > 1 {
cmd.Help()
return
}

r := rds.Config{}
r.Init()

logs, err := r.Logs(args[0], filter)
if err != nil {
fmt.Println(err)
return
}

if sort {
logs.SortBySize()
}

tbl := table.New("FILE", "SIZE")
for _, log := range logs {
tbl.AddRow(log.FileName, log.Size)
}
tbl.Print()
},
}

cmd.Flags().StringVar(&filter, "filter", "", "Filters the available log files for log file names that contain the specified string.")
cmd.Flags().BoolVar(&sort, "sort", false, "Sort list of logs by file size.")

return cmd
}
52 changes: 8 additions & 44 deletions aws/database/logs/main.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,23 @@
package logs

import (
"fmt"

"github.com/debeando/go-common/aws/rds"
"github.com/debeando/go-common/table"
"zenit/aws/database/logs/list"
"zenit/aws/database/logs/view"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "logs [IDENTIFIER] [FILENAME]",
Short: "List all logs about database.",
Example: `
# List logs of specific database instance:
zenit aws database logs test-rds
# Show logs to specific path and file name:
zenit aws database logs test-rds error/mysql-error-running.log`,
Use: "logs",
Short: "Logs about database; error, slow logs, general log, ...",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 || len(args) > 2 {
cmd.Help()
return
}

r := rds.Config{}
r.Init()

if len(args) == 1 && len(args[0]) > 0 {
logs, err := r.Logs(args[0])
if err != nil {
fmt.Println(err)
return
}
tbl := table.New("FILE", "SIZE")
for _, log := range logs {
tbl.AddRow(log.FileName, log.Size)
}
tbl.Print()
}

if len(args) == 2 && len(args[0]) > 0 && len(args[1]) > 0 {
data, err := r.PollLogs(args[0], args[1])
if err != nil {
fmt.Println(err)
return
}

if len(data) > 0 {
fmt.Println(data)
}
}
cmd.Help()
},
}

cmd.AddCommand(list.NewCommand())
cmd.AddCommand(view.NewCommand())

return cmd
}
42 changes: 42 additions & 0 deletions aws/database/logs/view/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package view

import (
"fmt"

"github.com/debeando/go-common/aws/rds"

"github.com/spf13/cobra"
)

func NewCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "view [IDENTIFIER] [FILENAME]",
Short: "View logs details of specific database instance.",
Example: `
# View logs details to specific path and file name:
zenit aws database logs view test-rds error/mysql-error-running.log`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
cmd.Help()
return
}

r := rds.Config{}
r.Init()

if len(args) == 2 && len(args[0]) > 0 && len(args[1]) > 0 {
data, err := r.PollLogs(args[0], args[1])
if err != nil {
fmt.Println(err)
return
}

if len(data) > 0 {
fmt.Println(data)
}
}
},
}

return cmd
}

0 comments on commit 71894f7

Please sign in to comment.