Skip to content

Commit

Permalink
Added error handling for seed db
Browse files Browse the repository at this point in the history
  • Loading branch information
Andriiklymiuk committed Oct 5, 2022
1 parent f0e69f0 commit 8eb3ddc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
22 changes: 11 additions & 11 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ func showMakeCommands(
fmt.Println("Container id: ", containerId)

case "seed":
SeedDb(targetService)
err = SeedDb(targetService)
if err != nil {
fmt.Println(err)
}
case "getDump":
GetDump(serviceConfig)
default:
Expand All @@ -136,7 +139,7 @@ func showMakeCommands(
}
}

func SeedDb(targetService string) {
func SeedDb(targetService string) error {
serviceIsRunning, err := utils.GetStatusOfService(targetService)
if err != nil {
fmt.Printf("Getting target service info failed: %s\n", err)
Expand All @@ -147,15 +150,13 @@ func SeedDb(targetService string) {
)

if err != nil {
fmt.Printf("Couldn't check for db dump file, error %s\n", err)
return
return fmt.Errorf("error in checking dump file: %s", err)
}
if !dumpFileExists {
fmt.Printf(
"Db dump file doesn't exist in %s. Please add one its directory\n",
return fmt.Errorf(
"db dump file doesn't exist in %s. Please add one its directory",
targetService,
)
return
}
if !serviceIsRunning {
_, err := utils.ExecuteMakeCommand(targetService, "up")
Expand All @@ -167,8 +168,7 @@ func SeedDb(targetService string) {

containerId, err := utils.GetContainerId(targetService)
if err != nil {
log.Println(err)
return
return err
}

s := spinner.New(spinner.CharSets[70], 100*time.Millisecond)
Expand All @@ -183,10 +183,10 @@ func SeedDb(targetService string) {

s.Stop()
if err != nil {
fmt.Println("Make command failed", err)
return
return fmt.Errorf("make command failed: %s", err)
}
fmt.Println(string(output))
return nil
}

func GetDump(serviceConfig utils.DatabaseService) {
Expand Down
7 changes: 6 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ func runRun(cmd *cobra.Command, args []string) {
}
fmt.Println(string("\n\033[34m"), "⛅ GETTING DATABASE DUMP for", dbService.ServiceName, string("\033[0m"))
GetDump(dbService)
SeedDb(dbService.ServiceName)
err = SeedDb(dbService.ServiceName)
if err != nil {
fmt.Println(err)
continue
}
fmt.Println(string("\n\033[34m"), "🎉 ", dbService.ServiceName, " IS SEEDED", string("\033[0m"))
}
}

Expand Down

0 comments on commit 8eb3ddc

Please sign in to comment.