Skip to content

Commit

Permalink
Merge branch 'main' into Club-Search-(SAC31+SAC32)
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye authored Mar 3, 2024
2 parents 7fd83f5 + c4089bc commit e2b4812
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 85 deletions.
47 changes: 47 additions & 0 deletions cli/commands/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package commands

import (
"fmt"
"os"
"os/exec"

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

func BackendCommand() *cli.Command {
command := &cli.Command{
Name: "backend",
Usage: "Starts the backend server",
Aliases: []string{"be"},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "use-dev-dot-env",
Usage: "Use the .env file in the backend directory",
Aliases: []string{"d"},
},
},
Action: func(c *cli.Context) error {
err := RunBackend()
if err != nil {
return cli.Exit(err.Error(), 1)
}

return nil
},
}

return command
}

func RunBackend() error {
cmd := exec.Command("go", "run", "main.go")
cmd.Dir = BACKEND_DIR
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("error starting backend: %w", err)
}
return nil
}
66 changes: 0 additions & 66 deletions cli/commands/be.go

This file was deleted.

20 changes: 14 additions & 6 deletions cli/commands/clean_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ func CleanTestDBs() error {
return fmt.Errorf("failed to get current user: %w", err)
}

rows, err := db.Query("SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres' AND datname != $1 AND datname != $2 AND datname LIKE 'sac_test_%';", currentUser.Username, CONFIG.Database.DatabaseName)
query := "SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'postgres' AND datname != $1 AND datname != $2 AND datname LIKE 'sac_test_%';"
rows, err := db.Query(query, currentUser.Username, CONFIG.Database.DatabaseName)
if err != nil {
return err
}

defer rows.Close()

var wg sync.WaitGroup
var dropped, failed int

for rows.Next() {
var dbName string
Expand All @@ -71,17 +73,23 @@ func CleanTestDBs() error {

fmt.Printf("Dropping database %s\n", dbName)

if _, err := db.Exec(fmt.Sprintf("DROP DATABASE %s", dbName)); err != nil {
_, err := db.Exec(fmt.Sprintf("DROP DATABASE %s", dbName))
if err != nil {
fmt.Printf("Failed to drop database %s: %v\n", dbName, err)
failed++
} else {
dropped++
}
}(dbName)
}

if err := rows.Err(); err != nil {
return err
}

wg.Wait()

fmt.Printf("\nSummary:\n - Databases dropped: %d\n - Databases failed to drop: %d\n", dropped, failed)

if failed > 0 {
return fmt.Errorf("failed to drop %d database(s)", failed)
}

return nil
}
2 changes: 1 addition & 1 deletion cli/commands/fe.go → cli/commands/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/urfave/cli/v2"
)

func RunFrontendCommand() *cli.Command {
func FrontendCommand() *cli.Command {
command := cli.Command{
Name: "fe",
Usage: "Run the frontend",
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func LintFrontend(target string, fix bool) error {
func LintBackend() error {
fmt.Println("Linting backend")

cmd := exec.Command("golangci-lint", "run", "--fix")
cmd := exec.Command("go", "vet", "./...")
cmd.Dir = BACKEND_DIR

err := cmd.Run()
Expand Down
15 changes: 8 additions & 7 deletions cli/commands/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"os"
"os/exec"

"github.com/urfave/cli/v2"
Expand All @@ -11,15 +12,15 @@ func SwaggerCommand() *cli.Command {
command := cli.Command{
Name: "swagger",
Aliases: []string{"swag"},
Usage: "Updates the swagger documentation",
Usage: "Runs `swag init` to update Swagger documentation for the backend API",
Action: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("Invalid arguments", 1)
}

err := Swagger()
cmd := exec.Command("swag", "init")
cmd.Dir = BACKEND_DIR
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return cli.Exit(err.Error(), 1)
return fmt.Errorf("error running swag init: %w", err)
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func Test(folder string, runFrontend bool, runBackend bool) error {

func BackendTest() error {
cmd := exec.Command("go", "test", "./...")
cmd.Dir = fmt.Sprintf("%s/..", BACKEND_DIR)
cmd.Dir = BACKEND_DIR

out, err := cmd.CombinedOutput()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import (
func main() {
app := &cli.App{
Name: "sac-cli",
Usage: "CLI for SAC",
Usage: "CLI for the GenerateNU SAC",
Commands: []*cli.Command{
commands.SwaggerCommand(),
commands.ClearDBCommand(),
commands.MigrateCommand(),
commands.ResetCommand(),
commands.InsertCommand(),
commands.DropCommand(),
commands.RunBackendCommand(),
commands.RunFrontendCommand(),
commands.BackendCommand(),
commands.FrontendCommand(),
commands.TestCommand(), // TODO: frontend tests
commands.FormatCommand(),
commands.LintCommand(),
Expand Down

0 comments on commit e2b4812

Please sign in to comment.