diff --git a/cli/commands/backend.go b/cli/commands/backend.go new file mode 100644 index 000000000..f058e3946 --- /dev/null +++ b/cli/commands/backend.go @@ -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 +} diff --git a/cli/commands/be.go b/cli/commands/be.go deleted file mode 100644 index e7b2fb1d7..000000000 --- a/cli/commands/be.go +++ /dev/null @@ -1,66 +0,0 @@ -package commands - -import ( - "fmt" - "os" - "os/exec" - - _ "github.com/lib/pq" - "github.com/urfave/cli/v2" -) - -func RunBackendCommand() *cli.Command { - command := cli.Command{ - Name: "be", - Usage: "Run the backend", - Category: "Development", - Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "use-dev-dot-env", - Usage: "Use the development .env file", - Value: false, - Aliases: []string{"d"}, - }, - }, - Action: func(c *cli.Context) error { - if c.Args().Len() > 0 { - return cli.Exit("Invalid arguments", 1) - } - - useDevDotEnv := c.Bool("use-dev-dot-env") - - err := RunBE(useDevDotEnv) - if err != nil { - return cli.Exit(err.Error(), 1) - } - - return nil - }, - } - - return &command -} - -func RunBE(useDevDotEnv bool) error { - var goCmd *exec.Cmd - - if useDevDotEnv { - goCmd = exec.Command("go", "run", "main.go", "--use-dev-dot-env") - } else { - goCmd = exec.Command("go", "run", "main.go") - } - - goCmd.Dir = BACKEND_DIR - - goCmd.Stdout = os.Stdout - goCmd.Stderr = os.Stderr - - fmt.Println("Running backend") - - err := goCmd.Run() - if err != nil { - return fmt.Errorf("error running backend: %w", err) - } - - return nil -} diff --git a/cli/commands/clean_tests.go b/cli/commands/clean_tests.go index e3d4c6973..da92e3db7 100644 --- a/cli/commands/clean_tests.go +++ b/cli/commands/clean_tests.go @@ -48,7 +48,8 @@ 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 } @@ -56,6 +57,7 @@ func CleanTestDBs() error { defer rows.Close() var wg sync.WaitGroup + var dropped, failed int for rows.Next() { var dbName string @@ -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 } diff --git a/cli/commands/fe.go b/cli/commands/frontend.go similarity index 97% rename from cli/commands/fe.go rename to cli/commands/frontend.go index 63d24fc82..c9f581ebf 100644 --- a/cli/commands/fe.go +++ b/cli/commands/frontend.go @@ -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", diff --git a/cli/commands/lint.go b/cli/commands/lint.go index f2eaeee9c..69a894ceb 100644 --- a/cli/commands/lint.go +++ b/cli/commands/lint.go @@ -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() diff --git a/cli/commands/swagger.go b/cli/commands/swagger.go index 3881d93cf..f830e9029 100644 --- a/cli/commands/swagger.go +++ b/cli/commands/swagger.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "os" "os/exec" "github.com/urfave/cli/v2" @@ -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 }, diff --git a/cli/commands/test.go b/cli/commands/test.go index be12543c4..b84617e94 100644 --- a/cli/commands/test.go +++ b/cli/commands/test.go @@ -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 { diff --git a/cli/main.go b/cli/main.go index abf20d447..880a7598a 100755 --- a/cli/main.go +++ b/cli/main.go @@ -11,7 +11,7 @@ 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(), @@ -19,8 +19,8 @@ func main() { commands.ResetCommand(), commands.InsertCommand(), commands.DropCommand(), - commands.RunBackendCommand(), - commands.RunFrontendCommand(), + commands.BackendCommand(), + commands.FrontendCommand(), commands.TestCommand(), // TODO: frontend tests commands.FormatCommand(), commands.LintCommand(),