-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/app: define flasher app type
The App type is used to initialize an instance of flasher with its configuration, logger and other attributes.
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |