Skip to content

Commit

Permalink
fixed cli lints
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 5, 2024
1 parent 433a0f3 commit 3171ff1
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
lint:
name: Lint
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
checks: write
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
lint:
name: Lint
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
checks: write
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
Expand Down
7 changes: 4 additions & 3 deletions cli/commands/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func DropData() error {
return fmt.Errorf("error scanning table name: %w", err)
}

deleteStmt := fmt.Sprintf("DELETE FROM \"%s\"", tablename)
_, err := db.Exec(deleteStmt)
_, err := db.Exec("DELETE FROM $1", tablename)
if err != nil {
return fmt.Errorf("error deleting rows from table %s: %w", tablename, err)
}
Expand All @@ -82,7 +81,9 @@ func DropData() error {
return fmt.Errorf("error in rows handling: %w", err)
}

Migrate()
if Migrate() != nil {
return fmt.Errorf("error migrating database: %w", err)
}

fmt.Println("All rows removed successfully.")
return nil
Expand Down
15 changes: 12 additions & 3 deletions cli/commands/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ func FormatCommand() *cli.Command {
runFrontend := folder != ""
runBackend := c.Bool("backend")

Format(folder, runFrontend, runBackend)
err := Format(folder, runFrontend, runBackend)
if err != nil {
return cli.Exit(err.Error(), 1)
}

return nil
},
Expand All @@ -56,7 +59,10 @@ func Format(folder string, runFrontend bool, runBackend bool) error {
wg.Add(1)
go func() {
defer wg.Done()
BackendFormat()
err := BackendFormat()
if err != nil {
fmt.Println(err)
}
}()
}

Expand All @@ -65,7 +71,10 @@ func Format(folder string, runFrontend bool, runBackend bool) error {
wg.Add(1)
go func() {
defer wg.Done()
FrontendFormat(folder)
err := FrontendFormat(folder)
if err != nil {
fmt.Println(err)
}
}()
}

Expand Down
29 changes: 14 additions & 15 deletions cli/commands/insert.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package commands

import (
"bytes"
"database/sql"
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"

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

Expand Down Expand Up @@ -65,18 +62,20 @@ func InsertDB() error {
fmt.Println("Database exists with tables.")
}

insertCmd := exec.Command("psql", "-h", CONFIG.Database.Host, "-p", strconv.Itoa(int(CONFIG.Database.Port)), "-U", CONFIG.Database.Username, "-d", CONFIG.Database.DatabaseName, "-a", "-f", MIGRATION_FILE)

var output bytes.Buffer
insertCmd.Stdout = &output
insertCmd.Stderr = &output

if err := insertCmd.Run(); err != nil {
return fmt.Errorf("error inserting data: %w", err)
migrationSQL, err := os.ReadFile(MIGRATION_FILE)
if err != nil {
return fmt.Errorf("error reading migration file: %w", err)
}

if strings.Contains(output.String(), "ROLLBACK") {
return errors.New("insertion failed, rolling back")
_, err = db.Exec(string(migrationSQL))
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
fmt.Println("PostgreSQL Error:")
fmt.Println("Code:", pqErr.Code)
fmt.Println("Message:", pqErr.Message)
} else {
return fmt.Errorf("error executing migration: %w", err)
}
}

fmt.Println("Data inserted successfully.")
Expand Down
29 changes: 24 additions & 5 deletions cli/commands/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ func LintCommand() *cli.Command {
runFrontend := folder != ""
runBackend := c.Bool("backend")

Lint(folder, runFrontend, runBackend)
err := Lint(folder, runFrontend, runBackend)
if err != nil {
return cli.Exit(err.Error(), 1)
}

return nil
},
Expand All @@ -50,13 +53,17 @@ func LintCommand() *cli.Command {

func Lint(folder string, runFrontend bool, runBackend bool) error {
var wg sync.WaitGroup
errChan := make(chan error, 1)

// Start the backend if specified
if runBackend {
wg.Add(1)
go func() {
defer wg.Done()
BackendLint()
err := BackendLint()
if err != nil {
errChan <- err
}
}()
}

Expand All @@ -65,19 +72,31 @@ func Lint(folder string, runFrontend bool, runBackend bool) error {
wg.Add(1)
go func() {
defer wg.Done()
FrontendLint(folder)
err := FrontendLint(folder)
if err != nil {
errChan <- err
}
}()
}

wg.Wait()
go func() {
wg.Wait()
close(errChan)
}()

for err := range errChan {
if err != nil {
return err
}
}

return nil
}

func BackendLint() error {
fmt.Println("Linting backend")

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

err := cmd.Run()
Expand Down
5 changes: 4 additions & 1 deletion cli/commands/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ func MigrateCommand() *cli.Command {
return cli.Exit("Invalid arguments", 1)
}

Migrate()
err := Migrate()
if err != nil {
return cli.Exit(err.Error(), 1)
}
return nil
},
}
Expand Down
29 changes: 22 additions & 7 deletions cli/commands/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,23 @@ func ResetCommand() *cli.Command {
func ResetData() error {
fmt.Println("Clearing database")

DropData()
err := DropData()
if err != nil {
return cli.Exit(err.Error(), 1)
}

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

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

Migrate()
err = Migrate()
if err != nil {
return cli.Exit(err.Error(), 1)
}

cmd = exec.Command("sleep", "1")
cmd.Dir = BACKEND_DIR
Expand All @@ -66,7 +72,10 @@ func ResetData() error {
return cli.Exit("Error running sleep", 1)
}

InsertDB()
err = InsertDB()
if err != nil {
return cli.Exit(err.Error(), 1)
}

fmt.Println("Data reset successfully")

Expand All @@ -76,17 +85,23 @@ func ResetData() error {
func ResetMigration() error {
fmt.Println("Resetting migration")

DropDB()
err := DropDB()
if err != nil {
return cli.Exit(err.Error(), 1)
}

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

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

Migrate()
err = Migrate()
if err != nil {
return cli.Exit(err.Error(), 1)
}

fmt.Println("Migration reset successfully")

Expand Down
5 changes: 4 additions & 1 deletion cli/commands/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ func SwaggerCommand() *cli.Command {
return cli.Exit("Invalid arguments", 1)
}

Swagger()
err := Swagger()
if err != nil {
return cli.Exit(err.Error(), 1)
}
return nil
},
}
Expand Down
36 changes: 30 additions & 6 deletions cli/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func TestCommand() *cli.Command {
folder := c.String("frontend")
runFrontend := folder != ""
runBackend := c.Bool("backend")
Test(folder, runFrontend, runBackend)
err := Test(folder, runFrontend, runBackend)
if err != nil {
return cli.Exit(err.Error(), 1)
}
return nil
},
}
Expand All @@ -47,13 +50,17 @@ func TestCommand() *cli.Command {

func Test(folder string, runFrontend bool, runBackend bool) error {
var wg sync.WaitGroup
errChan := make(chan error, 1)

// Start the backend if specified
if runBackend {
wg.Add(1)
go func() {
defer wg.Done()
BackendTest()
err := BackendTest()
if err != nil {
errChan <- err
}
}()
}

Expand All @@ -62,27 +69,44 @@ func Test(folder string, runFrontend bool, runBackend bool) error {
wg.Add(1)
go func() {
defer wg.Done()
FrontendTest(folder)
err := FrontendTest(folder)
if err != nil {
errChan <- err
}
}()
}

wg.Wait()
go func() {
wg.Wait()
close(errChan)
}()

for err := range errChan {
if err != nil {
return err
}
}

return nil
}

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

defer CleanTestDBs()

out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out))
return cli.Exit("Failed to run backend tests", 1)
}

fmt.Println(string(out))

err = CleanTestDBs()
if err != nil {
return cli.Exit(err.Error(), 1)
}

return nil
}

Expand Down

0 comments on commit 3171ff1

Please sign in to comment.