From e2ba18ed8fabc4b83949d676c9d95084aae84b95 Mon Sep 17 00:00:00 2001 From: Kilian SMITI Date: Fri, 10 Mar 2023 16:26:39 +0100 Subject: [PATCH] feat(list): add last connection --- internal/commands/list.go | 3 +- internal/commands/open.go | 19 ++++----- .../config/stores/credentials/credentials.go | 13 ++++++ internal/pkg/utils/print_time.go | 42 +++++++++++++++++++ 4 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 internal/pkg/utils/print_time.go diff --git a/internal/commands/list.go b/internal/commands/list.go index 1b161cd..6c69510 100644 --- a/internal/commands/list.go +++ b/internal/commands/list.go @@ -3,6 +3,7 @@ package commands import ( "fmt" "github.com/ermos/dbm/internal/pkg/config/stores/credentials" + "github.com/ermos/dbm/internal/pkg/utils" "github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/text" "github.com/spf13/cobra" @@ -42,7 +43,7 @@ func RunList(cmd *cobra.Command, args []string) { item.Port, item.Username, item.DefaultDatabase, - item.LastConnectionAt, + utils.FormatXTimeAgo(item.LastConnectionAt, "never"), }) } diff --git a/internal/commands/open.go b/internal/commands/open.go index cb29cab..d26ec2f 100644 --- a/internal/commands/open.go +++ b/internal/commands/open.go @@ -23,16 +23,13 @@ func RunOpen(cmd *cobra.Command, args []string) { panic(err) } - switch dbConfig.Protocol { - case db.ProtocolMySQL: - if err = db.RunLinuxMySQL(dbConfig); err != nil { - panic(err) - } - case db.ProtocolRedis: - if err = db.RunLinuxRedis(dbConfig); err != nil { - panic(err) - } - default: - panic("unsupported protocol") + err = credentials.Get().UpdateLastConnection(dbConfig.Alias) + if err != nil { + panic(err) + } + + err = db.Run(dbConfig) + if err != nil { + panic(err) } } diff --git a/internal/pkg/config/stores/credentials/credentials.go b/internal/pkg/config/stores/credentials/credentials.go index d8cabec..c17d1fb 100644 --- a/internal/pkg/config/stores/credentials/credentials.go +++ b/internal/pkg/config/stores/credentials/credentials.go @@ -8,6 +8,7 @@ import ( "github.com/ermos/dbm/internal/pkg/goliath" "os" "path/filepath" + "time" ) var config = &Config{ @@ -94,3 +95,15 @@ func (c *Config) RemoveAlias(alias string) error { return c.Save() } + +func (c *Config) UpdateLastConnection(alias string) error { + dbConfig := c.Credentials[alias] + if dbConfig.Alias == "" { + return errors.New("alias not found") + } + + dbConfig.LastConnectionAt = time.Now() + c.Credentials[alias] = dbConfig + + return c.Save() +} diff --git a/internal/pkg/utils/print_time.go b/internal/pkg/utils/print_time.go new file mode 100644 index 0000000..8bc8e1a --- /dev/null +++ b/internal/pkg/utils/print_time.go @@ -0,0 +1,42 @@ +package utils + +import ( + "fmt" + "time" +) + +func FormatXTimeAgo(t time.Time, neverText string) string { + if t == (time.Time{}) { + return neverText + } + + duration := time.Since(t) + + years := int(duration.Hours() / 24 / 365) + months := int(duration.Hours()/24/30) % 12 + days := int(duration.Hours()/24) % 30 + hours := int(duration.Hours()) % 24 + minutes := int(duration.Minutes()) % 60 + + if years >= 1 { + return fmt.Sprintf("%d year%s ago", years, pluralize(years)) + } else if months >= 1 { + return fmt.Sprintf("%d month%s ago", months, pluralize(months)) + } else if days >= 1 { + return fmt.Sprintf("%d day%s ago", days, pluralize(days)) + } else if hours >= 1 { + return fmt.Sprintf("%d hour%s ago", hours, pluralize(hours)) + } else if minutes >= 1 { + return fmt.Sprintf("%d minute%s ago", minutes, pluralize(minutes)) + } + + return "less than a minute ago" +} + +func pluralize(count int) string { + if count == 1 { + return "" + } else { + return "s" + } +}