Skip to content

Commit

Permalink
feat(list): add last connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ermos committed Mar 10, 2023
1 parent 5505eb0 commit e2ba18e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
3 changes: 2 additions & 1 deletion internal/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -42,7 +43,7 @@ func RunList(cmd *cobra.Command, args []string) {
item.Port,
item.Username,
item.DefaultDatabase,
item.LastConnectionAt,
utils.FormatXTimeAgo(item.LastConnectionAt, "never"),
})
}

Expand Down
19 changes: 8 additions & 11 deletions internal/commands/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
13 changes: 13 additions & 0 deletions internal/pkg/config/stores/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/ermos/dbm/internal/pkg/goliath"
"os"
"path/filepath"
"time"
)

var config = &Config{
Expand Down Expand Up @@ -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()
}
42 changes: 42 additions & 0 deletions internal/pkg/utils/print_time.go
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit e2ba18e

Please sign in to comment.