init application routines
This commit is contained in:
+1
-11
@@ -2,8 +2,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -20,10 +18,7 @@ type loggerKey struct{}
|
|||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
func Execute(log *slog.Logger) {
|
func Execute() {
|
||||||
ctx := context.WithValue(context.Background(), loggerKey{}, log)
|
|
||||||
rootCmd.SetContext(ctx)
|
|
||||||
|
|
||||||
err := rootCmd.Execute()
|
err := rootCmd.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -31,11 +26,6 @@ func Execute(log *slog.Logger) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initApplication)
|
|
||||||
|
|
||||||
rootCmd.Flags().StringP("out", "o", "", "output file")
|
rootCmd.Flags().StringP("out", "o", "", "output file")
|
||||||
rootCmd.Flags().StringP("session", "s", "", "output session info to file")
|
rootCmd.Flags().StringP("session", "s", "", "output session info to file")
|
||||||
}
|
}
|
||||||
|
|
||||||
func initApplication() {
|
|
||||||
}
|
|
||||||
|
|||||||
+3
-8
@@ -1,21 +1,16 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"esdi/tui"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
|
"esdi/tui"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func tuiCmdAction(cmd *cobra.Command, args []string) {
|
func tuiCmdAction(cmd *cobra.Command, args []string) {
|
||||||
logger, ok := cmd.Context().Value(loggerKey{}).(*slog.Logger)
|
err := tui.Run(slog.Default())
|
||||||
if !ok {
|
|
||||||
fmt.Printf("Error loading logger from context: %s\n", logger)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err := tui.Run(logger)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error running TUI: %s\n", err.Error())
|
fmt.Printf("Error running TUI: %s\n", err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,40 +1,79 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"esdi/cmd"
|
"esdi/cmd"
|
||||||
|
"esdi/config"
|
||||||
"esdi/telemetry"
|
"esdi/telemetry"
|
||||||
|
|
||||||
"github.com/arl/statsviz"
|
"github.com/arl/statsviz"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func initApplication() {
|
||||||
// NOTE: pass all these things to cobra
|
// 1. This is the first initialization setup we do so we can log
|
||||||
output, err := os.OpenFile("./output.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o755)
|
err := setupLogger()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to open logging file: " + err.Error())
|
panic("failed to open logging file: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. This is the second thing we setup because we need some stuff to run
|
||||||
|
// other things
|
||||||
|
err = config.Setup("./config/config.yaml")
|
||||||
|
if err != nil {
|
||||||
|
panic("failed to setup config: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Performance analysis
|
||||||
|
if config.GetCfg().MetricsServer {
|
||||||
|
slog.Info("Setting up metrics server...")
|
||||||
|
err = runStatviz()
|
||||||
|
if err != nil {
|
||||||
|
slog.Error(fmt.Sprintf("failed to setup metrics server: %+v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setting up some internal data structures
|
||||||
|
telemetry.Init()
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupLogger() error {
|
||||||
|
output, err := os.OpenFile("./output.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o755)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
logger := slog.New(
|
logger := slog.New(
|
||||||
slog.NewTextHandler(output, &slog.HandlerOptions{
|
slog.NewTextHandler(output, &slog.HandlerOptions{
|
||||||
Level: slog.LevelDebug,
|
Level: slog.LevelDebug,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
// Performance analysis
|
slog.SetDefault(logger)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runStatviz() error {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
statsviz.Register(mux)
|
err := statsviz.Register(mux)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
http.ListenAndServe("localhost:8001", mux)
|
http.ListenAndServe("localhost:8001", mux)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Setting up some internal data structures
|
return nil
|
||||||
telemetry.Init()
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
initApplication()
|
||||||
|
|
||||||
// Launches the cobra package stuff
|
// Launches the cobra package stuff
|
||||||
cmd.Execute(logger)
|
cmd.Execute()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user