Skip to content

Commit

Permalink
internal/app: define flasher app type
Browse files Browse the repository at this point in the history
The App type is used to initialize an instance of flasher
with its configuration, logger and other attributes.
  • Loading branch information
joelrebel committed Feb 6, 2023
1 parent f7fed7f commit 7d20ff8
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package app

import (
"context"
"os"
"os/signal"
"sync"
"syscall"

runtime "github.com/banzaicloud/logrus-runtime-formatter"
"github.com/metal-toolbox/flasher/internal/model"
"github.com/sirupsen/logrus"
)

// Config holds configuration data when running mctl
// App holds attributes for the mtl application
type App struct {
// Sync waitgroup to wait for running go routines on termination.
SyncWG *sync.WaitGroup
// Flasher configuration.
Config *model.Config
// TermCh is the channel to terminate the app based on a signal
TermCh chan os.Signal
// Logger is the app logger
Logger *logrus.Logger
}

// New returns returns a new instance of the flasher app
func New(ctx context.Context, appKind model.AppKind, inventorySourceKind, cfgFile string, loglevel int) (*App, error) {
// load configuration
cfg := &model.Config{
AppKind: appKind,
InventorySource: inventorySourceKind,
}

if err := cfg.Load(cfgFile); err != nil {
return nil, err
}

app := &App{
Config: cfg,
SyncWG: &sync.WaitGroup{},
Logger: logrus.New(),
TermCh: make(chan os.Signal),
}

runtimeFormatter := &runtime.Formatter{
ChildFormatter: &logrus.JSONFormatter{},
File: true,
Line: true,
BaseNameOnly: true,
}

// set log level, format
switch loglevel {
case model.LogLevelDebug:
app.Logger.Level = logrus.DebugLevel

// set runtime formatter options
runtimeFormatter.BaseNameOnly = true
runtimeFormatter.File = true
runtimeFormatter.Line = true

case model.LogLevelTrace:
app.Logger.Level = logrus.TraceLevel

// set runtime formatter options
runtimeFormatter.File = true
runtimeFormatter.Line = true
runtimeFormatter.Package = true
default:
app.Logger.Level = logrus.InfoLevel
}

app.Logger.SetFormatter(runtimeFormatter)

// register for SIGINT, SIGTERM
signal.Notify(app.TermCh, syscall.SIGINT, syscall.SIGTERM)

return app, nil
}

0 comments on commit 7d20ff8

Please sign in to comment.