Skip to content

Commit

Permalink
Merge pull request #46 from richbl/dev
Browse files Browse the repository at this point in the history
refactor(APP): ♻️ Refactor in logger and main packages; remove…
  • Loading branch information
richbl authored Dec 24, 2024
2 parents 3ad83f4 + 274dab1 commit 22013f3
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 345 deletions.
31 changes: 8 additions & 23 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"time"

ble "github.com/richbl/go-ble-sync-cycle/internal/ble"
Expand Down Expand Up @@ -34,16 +34,15 @@ type appControllers struct {
}

func main() {

// Hello world!
log.Println(appPrefix, "Starting", appName, appVersion)

cfg := loadConfig("config.toml")

// Initialize the shutdown manager and exit handler
sm := NewShutdownManager(shutdownTimeout)
exitHandler := NewExitHandler(sm)

// Add configureTerminal cleanup function to reset terminal settings on exit
sm.AddCleanupFn(configureTerminal())
sm.Start()

// Initialize the logger with the configured log level and exit handler
Expand All @@ -56,7 +55,7 @@ func main() {
// Initialize the application controllers
controllers, componentType, err := setupAppControllers(*cfg)
if err != nil {
logger.Fatal(componentType, "failed to create controllers: "+err.Error())
logger.Fatal(componentType, "failed to create controllers:", err.Error())
return
}

Expand All @@ -65,7 +64,7 @@ func main() {
if err != nil {

if err != context.Canceled {
logger.Fatal(logger.BLE, "failed to scan for BLE characteristic: "+err.Error())
logger.Fatal(logger.BLE, "failed to scan for BLE characteristic:", err.Error())
return
}

Expand All @@ -88,7 +87,7 @@ func main() {
// Wait for services to complete and check for errors
for _, runner := range []*ServiceRunner{bleRunner, videoRunner} {
if err := runner.Error(); err != nil {
logger.Fatal(logger.APP, "service error: "+err.Error())
logger.Fatal(logger.APP, "service error:", err.Error())
return
}
}
Expand Down Expand Up @@ -140,6 +139,7 @@ func scanForBLECharacteristic(ctx context.Context, controllers appControllers) (

select {
case <-ctx.Done():
fmt.Print("\r") // Clear the ^C character from the terminal line
logger.Info(logger.BLE, "user-generated interrupt, stopping BLE discovery...")
return nil, ctx.Err()
case result := <-resultsChan:
Expand All @@ -152,28 +152,13 @@ func loadConfig(file string) *config.Config {

cfg, err := config.LoadFile(file)
if err != nil {
log.Println(logger.Red + "[FTL]" + logger.Reset + " [APP] failed to load TOML configuration: " + err.Error())
log.Println(logger.Red+"[FTL] "+logger.Reset+"[APP] failed to load TOML configuration:", err.Error())
waveGoodbye()
}

return cfg
}

// configureTerminal handles terminal character echo settings, returning a cleanup function
// to restore original terminal settings
func configureTerminal() func() {

rawMode := exec.Command("stty", "-echo")
rawMode.Stdin = os.Stdin
_ = rawMode.Run()

return func() {
cooked := exec.Command("stty", "echo")
cooked.Stdin = os.Stdin
_ = cooked.Run()
}
}

// waveGoodbye outputs a goodbye message and exits the program
func waveGoodbye() {
log.Println(appPrefix, appName, appVersion, "shutdown complete. Goodbye!")
Expand Down
Loading

0 comments on commit 22013f3

Please sign in to comment.