From 74df9c721dbc5715e7490b9d636a2e7f0d74095f Mon Sep 17 00:00:00 2001 From: Dan O'Neill Date: Sun, 10 Dec 2023 14:43:50 -0800 Subject: [PATCH 1/6] create data utility open database from utility --- cmd/open.go | 13 ++----------- data/connect.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 data/connect.go diff --git a/cmd/open.go b/cmd/open.go index 177a6c5..56639bd 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -3,8 +3,8 @@ package cmd import ( "fmt" + "github.com/doneill/er-cli-go/data" "github.com/spf13/cobra" - "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -29,7 +29,7 @@ var openCmd = &cobra.Command{ // ---------------------------------------------- func open(file string) { - db, err := connectToSQLite(file) + db, err := data.DbConnect(file) if err != nil { fmt.Println(err) return @@ -51,15 +51,6 @@ func open(file string) { } } -func connectToSQLite(file string) (*gorm.DB, error) { - db, err := gorm.Open(sqlite.Open(file), &gorm.Config{}) - if err != nil { - return nil, err - } - - return db, nil -} - func getTables(db gorm.DB) (tableList []string, err error) { tables, err := db.Migrator().GetTables() if err != nil { diff --git a/data/connect.go b/data/connect.go new file mode 100644 index 0000000..7276018 --- /dev/null +++ b/data/connect.go @@ -0,0 +1,19 @@ +package data + +import ( + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +// ---------------------------------------------- +// exported funtions +// ---------------------------------------------- + +func DbConnect(file string) (*gorm.DB, error) { + db, err := gorm.Open(sqlite.Open(file), &gorm.Config{}) + if err != nil { + return nil, err + } + + return db, nil +} From ed9d0025eb0ad593a84698eefd1e2684b2354856 Mon Sep 17 00:00:00 2001 From: Dan O'Neill Date: Sun, 10 Dec 2023 14:46:18 -0800 Subject: [PATCH 2/6] get tables from utility --- cmd/open.go | 12 +----------- data/connect.go | 9 +++++++++ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/cmd/open.go b/cmd/open.go index 56639bd..bb682be 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -5,7 +5,6 @@ import ( "github.com/doneill/er-cli-go/data" "github.com/spf13/cobra" - "gorm.io/gorm" ) var displayTables bool @@ -37,7 +36,7 @@ func open(file string) { switch { case displayTables: - tables, err := getTables(*db) + tables, err := data.GetTables(*db) if err != nil { fmt.Println(err) return @@ -51,15 +50,6 @@ func open(file string) { } } -func getTables(db gorm.DB) (tableList []string, err error) { - tables, err := db.Migrator().GetTables() - if err != nil { - return nil, err - } - - return tables, nil -} - // ---------------------------------------------- // initialize // ---------------------------------------------- diff --git a/data/connect.go b/data/connect.go index 7276018..3b98fce 100644 --- a/data/connect.go +++ b/data/connect.go @@ -17,3 +17,12 @@ func DbConnect(file string) (*gorm.DB, error) { return db, nil } + +func GetTables(db gorm.DB) (tableList []string, err error) { + tables, err := db.Migrator().GetTables() + if err != nil { + return nil, err + } + + return tables, nil +} From 9185d970f4222fb909c49ecee2b5eca0ed6f312e Mon Sep 17 00:00:00 2001 From: Dan O'Neill Date: Sun, 10 Dec 2023 14:47:12 -0800 Subject: [PATCH 3/6] add emphasis --- cmd/open.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/open.go b/cmd/open.go index bb682be..0f8b7e3 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -45,7 +45,7 @@ func open(file string) { fmt.Println(tableName) } default: - message := fmt.Sprintf("%s successfully opened", file) + message := fmt.Sprintf("%s successfully opened!", file) fmt.Println(message) } } From dc185a2357cef23cf5f16cfc35c80eeefcf2bdbc Mon Sep 17 00:00:00 2001 From: Dan O'Neill Date: Fri, 15 Dec 2023 00:39:44 -0800 Subject: [PATCH 4/6] update package --- cmd/open.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/open.go b/cmd/open.go index 0f8b7e3..9bba22e 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" - "github.com/doneill/er-cli-go/data" + "github.com/doneill/er-cli/data" "github.com/spf13/cobra" ) From 8aa4eba976e242a81c01f96b2cbf27bc467de8be Mon Sep 17 00:00:00 2001 From: Dan O'Neill Date: Sat, 16 Dec 2023 14:55:31 -0800 Subject: [PATCH 5/6] format table output add row count to display --- cmd/open.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cmd/open.go b/cmd/open.go index 9bba22e..90c1a67 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -2,8 +2,10 @@ package cmd import ( "fmt" + "os" "github.com/doneill/er-cli/data" + "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" ) @@ -28,6 +30,8 @@ var openCmd = &cobra.Command{ // ---------------------------------------------- func open(file string) { + var count int64 + db, err := data.DbConnect(file) if err != nil { fmt.Println(err) @@ -41,9 +45,16 @@ func open(file string) { fmt.Println(err) return } + + table := tablewriter.NewWriter(os.Stdout) + table.SetHeader([]string{"Name", "Count"}) + for _, tableName := range tables { - fmt.Println(tableName) + db.Table(tableName).Count(&count) + table.Append([]string{tableName, fmt.Sprintf("%d", count)}) } + + table.Render() default: message := fmt.Sprintf("%s successfully opened!", file) fmt.Println(message) From cbebbc8f574b8644e32e3cf872a36555ff64677d Mon Sep 17 00:00:00 2001 From: Dan O'Neill Date: Sun, 17 Dec 2023 09:38:47 -0800 Subject: [PATCH 6/6] add user flag returns username --- cmd/open.go | 6 ++++++ data/tables.go | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 data/tables.go diff --git a/cmd/open.go b/cmd/open.go index 90c1a67..6fbd4d8 100644 --- a/cmd/open.go +++ b/cmd/open.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/cobra" ) +var dbUser bool var displayTables bool // ---------------------------------------------- @@ -39,6 +40,10 @@ func open(file string) { } switch { + case dbUser: + var user []data.Accounts_User + db.First(&user) + fmt.Println(user[0].Username) case displayTables: tables, err := data.GetTables(*db) if err != nil { @@ -67,5 +72,6 @@ func open(file string) { func init() { rootCmd.AddCommand(openCmd) + openCmd.Flags().BoolVarP(&dbUser, "user", "u", false, "Display database account user") openCmd.Flags().BoolVarP(&displayTables, "tables", "t", false, "Display all database tables") } diff --git a/data/tables.go b/data/tables.go new file mode 100644 index 0000000..12fe685 --- /dev/null +++ b/data/tables.go @@ -0,0 +1,26 @@ +package data + +// ---------------------------------------------- +// table stucts +// ---------------------------------------------- + +type Accounts_User struct { + ID int `gorm:"primaryKey;autoIncrement"` + RemoteID string `json:"remote_id" gorm:"column:remote_id"` + Username string `json:"username" gorm:"column:username"` + FirstName string `json:"first_name" gorm:"column:first_name"` + LastName string `json:"last_name" gorm:"column:last_name"` + Email string `json:"email" gorm:"column:email"` +} + +// ---------------------------------------------- +// table configurations +// ---------------------------------------------- + +type Tabler interface { + TableName() string +} + +func (Accounts_User) TableName() string { + return "accounts_user" +}