Skip to content

Commit

Permalink
feat: updated cli (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye committed Feb 2, 2024
1 parent 1673ef0 commit 66ba683
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 58 deletions.
32 changes: 25 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,31 @@

To install use `./install.sh` and then run `sac-cli` to see all commands.

```console
sac-cli migrate // run migrations
sac-cli reset // reset database
sac-cli swagger // generate swagger docs
sac-cli lint // lint code
sac-cli format // format code
sac-cli test // run tests
```console
NAME:
sac-cli - CLI for SAC

USAGE:
sac-cli [global options] command [command options]

COMMANDS:
swagger, swag Updates the swagger documentation
be Run the backend
test, t Runs tests
format, f Runs formatting tools
lint, l Runs linting tools
help, h Shows a list of commands or help for one command
Database Operations:
clean Remove databases used for testing
migrate Migrate the database, creating tables and relationships
insert, i Inserts mock data into the database
reset, r Resets the database, dropping all tables, clearing data, and re-running migrations
* can use --data to just reset data and not drop tables
drop, d Drop data with a migration or drops the entire database
* can use --data to just drop data and not drop tables

GLOBAL OPTIONS:
--help, -h show help
```

# Git Flow
Expand Down
6 changes: 4 additions & 2 deletions cli/commands/clean_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (

func ClearDBCommand() *cli.Command {
command := cli.Command{
Name: "clean",
Usage: "Remove databases used for testing",
Name: "clean",
Category: "Database Operations",
Aliases: []string{"c"},
Usage: "Remove databases used for testing",
Action: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("Invalid arguments", 1)
Expand Down
94 changes: 74 additions & 20 deletions cli/commands/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,38 @@ import (
"fmt"
"sync"

_ "github.com/lib/pq"
"github.com/urfave/cli/v2"
)

func DropDBCommand() *cli.Command {
var dbMutex sync.Mutex

func DropCommand() *cli.Command {
command := cli.Command{
Name: "drop",
Usage: "Drops the database",
Name: "drop",
Aliases: []string{"d"},
Usage: "Drop data with a migration or drops the entire database",
Category: "Database Operations",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "data",
Usage: "Drop only data, not the entire database",
},
},
Action: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("Invalid arguments", 1)
}

err := DropDB()
if err != nil {
return cli.Exit(err.Error(), 1)
if c.Bool("data") {
err := DropData()
if err != nil {
return cli.Exit(err.Error(), 1)
}
} else {
err := DropDB()
if err != nil {
return cli.Exit(err.Error(), 1)
}
}

return nil
Expand All @@ -30,9 +46,54 @@ func DropDBCommand() *cli.Command {
return &command
}

func DropData() error {
fmt.Println("Clearing database")

dbMutex.Lock()
defer dbMutex.Unlock()

db, err := sql.Open("postgres", CONFIG.Database.WithDb())
if err != nil {
return err
}
defer db.Close()

rows, err := db.Query("SELECT tablename FROM pg_tables WHERE schemaname = 'public'")
if err != nil {
return fmt.Errorf("error retrieving tables: %w", err)
}
defer rows.Close()

for rows.Next() {
var tablename string
if err := rows.Scan(&tablename); err != nil {
return fmt.Errorf("error scanning table name: %w", err)
}

deleteStmt := fmt.Sprintf("DELETE FROM \"%s\"", tablename)
_, err := db.Exec(deleteStmt)
if err != nil {
return fmt.Errorf("error deleting rows from table %s: %w", tablename, err)
}
fmt.Printf("Removed all rows from table %s\n", tablename)
}

if err := rows.Err(); err != nil {
return fmt.Errorf("error in rows handling: %w", err)
}

Migrate()

fmt.Println("All rows removed successfully.")
return nil
}

func DropDB() error {
fmt.Println("Dropping database")

dbMutex.Lock()
defer dbMutex.Unlock()

db, err := sql.Open("postgres", CONFIG.Database.WithDb())
if err != nil {
return err
Expand Down Expand Up @@ -61,8 +122,6 @@ func DropDB() error {

defer rows.Close()

var wg sync.WaitGroup

fmt.Println("Dropping tables...")

for rows.Next() {
Expand All @@ -71,23 +130,18 @@ func DropDB() error {
return fmt.Errorf("error reading table name: %w", err)
}

wg.Add(1)
go func(table string) {
defer wg.Done()
dropStmt := fmt.Sprintf("DROP TABLE IF EXISTS \"%s\" CASCADE", table)
if _, err := db.Exec(dropStmt); err != nil {
fmt.Printf("Error dropping table %s: %v\n", table, err)
} else {
fmt.Printf("Dropped table %s\n", table)
}
}(tablename)
dropStmt := fmt.Sprintf("DROP TABLE IF EXISTS \"%s\" CASCADE", tablename)
if _, err := db.Exec(dropStmt); err != nil {
fmt.Printf("Error dropping table %s: %v\n", tablename, err)
} else {
fmt.Printf("Dropped table %s\n", tablename)
}
}

if err := rows.Err(); err != nil {
return fmt.Errorf("error in rows handling: %w", err)
}

wg.Wait()
fmt.Println("All tables dropped successfully.")
return nil
}
5 changes: 3 additions & 2 deletions cli/commands/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

func FormatCommand() *cli.Command {
command := cli.Command{
Name: "format",
Usage: "Runs formatting tools",
Name: "format",
Usage: "Runs formatting tools",
Aliases: []string{"f"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "frontend",
Expand Down
8 changes: 5 additions & 3 deletions cli/commands/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"github.com/urfave/cli/v2"
)

func InsertDBCommand() *cli.Command {
func InsertCommand() *cli.Command {
command := cli.Command{
Name: "insert",
Usage: "Inserts mock data into the database",
Name: "insert",
Category: "Database Operations",
Aliases: []string{"i"},
Usage: "Inserts mock data into the database",
Action: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("Invalid arguments", 1)
Expand Down
5 changes: 3 additions & 2 deletions cli/commands/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

func LintCommand() *cli.Command {
command := cli.Command{
Name: "lint",
Usage: "Runs linting tools",
Name: "lint",
Aliases: []string{"l"},
Usage: "Runs linting tools",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "frontend",
Expand Down
5 changes: 3 additions & 2 deletions cli/commands/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

func MigrateCommand() *cli.Command {
command := cli.Command{
Name: "migrate",
Usage: "Migrate the database",
Name: "migrate",
Usage: "Migrate the database, creating tables and relationships",
Category: "Database Operations",
Action: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("Invalid arguments", 1)
Expand Down
63 changes: 56 additions & 7 deletions cli/commands/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,74 @@ import (
"github.com/urfave/cli/v2"
)

func ResetDBCommand() *cli.Command {
func ResetCommand() *cli.Command {
command := cli.Command{
Name: "reset",
Usage: "Resets the database",
Name: "reset",
Aliases: []string{"r"},
Usage: "Resets the database, dropping all tables, clearing data, and re-running migrations",
Category: "Database Operations",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "data",
Usage: "Reset only data, not the entire database, will re-run migrations",
},
},
Action: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("Invalid arguments", 1)
}

ResetDB()
if c.Bool("data") {
err := ResetData()
if err != nil {
return cli.Exit(err.Error(), 1)
}
} else {
err := ResetMigration()
if err != nil {
return cli.Exit(err.Error(), 1)
}
}

return nil
},
}

return &command
}

func ResetDB() error {
fmt.Println("Resetting database")
func ResetData() error {
fmt.Println("Clearing database")

DropData()

cmd := exec.Command("sleep", "1")
cmd.Dir = BACKEND_DIR

err := cmd.Run()
if err != nil {
return cli.Exit("Error running sleep", 1)
}

Migrate()

cmd = exec.Command("sleep", "1")
cmd.Dir = BACKEND_DIR

err = cmd.Run()
if err != nil {
return cli.Exit("Error running sleep", 1)
}

InsertDB()

fmt.Println("Data reset successfully")

return nil
}

func ResetMigration() error {
fmt.Println("Resetting migration")

DropDB()

Expand All @@ -39,7 +88,7 @@ func ResetDB() error {

Migrate()

fmt.Println("Done resetting database")
fmt.Println("Migration reset successfully")

return nil
}
5 changes: 3 additions & 2 deletions cli/commands/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

func SwaggerCommand() *cli.Command {
command := cli.Command{
Name: "swagger",
Usage: "Updates the swagger documentation",
Name: "swagger",
Aliases: []string{"swag"},
Usage: "Updates the swagger documentation",
Action: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("Invalid arguments", 1)
Expand Down
10 changes: 3 additions & 7 deletions cli/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

func TestCommand() *cli.Command {
command := cli.Command{
Name: "test",
Usage: "Runs tests",
Name: "test",
Aliases: []string{"t"},
Usage: "Runs tests",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "frontend",
Expand All @@ -37,13 +38,10 @@ func TestCommand() *cli.Command {
folder := c.String("frontend")
runFrontend := folder != ""
runBackend := c.Bool("backend")

Test(folder, runFrontend, runBackend)

return nil
},
}

return &command
}

Expand All @@ -69,7 +67,6 @@ func Test(folder string, runFrontend bool, runBackend bool) error {
}

wg.Wait()

return nil
}

Expand All @@ -86,7 +83,6 @@ func BackendTest() error {
}

fmt.Println(string(out))

return nil
}

Expand Down
Loading

0 comments on commit 66ba683

Please sign in to comment.