refactor: refactor project structure to use golang best practices
This commit is contained in:
parent
5b16cef525
commit
4ed6a61b1d
10 changed files with 617 additions and 702 deletions
31
main.go
31
main.go
|
|
@ -1,43 +1,50 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
configDir, err := os.UserConfigDir()
|
||||
if err != nil {
|
||||
slog.Error("Cant get user config dir")
|
||||
panic(err)
|
||||
fmt.Fprintf(os.Stderr, "Error getting config dir: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logFile := filepath.Join(configDir, "work", "workctl.log")
|
||||
_ = os.MkdirAll(filepath.Dir(logFile), 0750)
|
||||
|
||||
file, err := os.OpenFile(filepath.Join(configDir, "work", "workctl.log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
||||
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
fmt.Fprintf(os.Stderr, "Failed to open log file: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(file, nil))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
|
||||
app, err := NewApp()
|
||||
if err != nil {
|
||||
slog.Error("Unable to setup application", "Error", err)
|
||||
slog.Error("Unable to setup application", "error", err)
|
||||
fmt.Fprintf(os.Stderr, "Error setting up application: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer func() {
|
||||
if err := app.Close(); err != nil {
|
||||
slog.Error("Failed to close application resources", "Error", err)
|
||||
slog.Error("Failed to close application resources", "error", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if len(os.Args) > 1 {
|
||||
if err := app.setupCommands().Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
app.makeChoice()
|
||||
if err := app.Execute(ctx); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue