go(run.go): add a way to load local breach data
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2023-08-10 19:06:13 +02:00
parent d9c3fc1d68
commit 1e1f54023e
Signed by: wanderer
SSH Key Fingerprint: SHA256:MdCZyJ2sHLltrLBp0xQO0O1qTW9BT/xl5nXkDvhlMCI
2 changed files with 23 additions and 2 deletions

@ -8,4 +8,6 @@ import "errors"
var ( var (
errDBNotConfigured = errors.New("Database connection string or database type were not set") errDBNotConfigured = errors.New("Database connection string or database type were not set")
errUnsupportedDBType = errors.New("DBTYPE can only be one of postgres or sqlite3") errUnsupportedDBType = errors.New("DBTYPE can only be one of postgres or sqlite3")
errDBConnFailed = errors.New("Failed to open a connection to the database")
errImportFailed = errors.New("Import of local breach data failed")
) )

23
run.go

@ -25,6 +25,7 @@ import (
"git.dotya.ml/mirre-mt/pcmt/config" "git.dotya.ml/mirre-mt/pcmt/config"
"git.dotya.ml/mirre-mt/pcmt/ent" "git.dotya.ml/mirre-mt/pcmt/ent"
moddb "git.dotya.ml/mirre-mt/pcmt/modules/db" moddb "git.dotya.ml/mirre-mt/pcmt/modules/db"
"git.dotya.ml/mirre-mt/pcmt/modules/localbreach"
"git.dotya.ml/mirre-mt/pcmt/slogging" "git.dotya.ml/mirre-mt/pcmt/slogging"
) )
@ -160,10 +161,15 @@ func run() error { //nolint:gocognit
db, err := ent.Open(setting.DbType(), setting.DbConnstring()) db, err := ent.Open(setting.DbType(), setting.DbConnstring())
if err != nil { if err != nil {
if doingImport { if doingImport {
log.Error("import not possible") log.Error("Import was not possible", "error", err)
fmt.Fprintf(os.Stdout, "\033[31m✗\033[0m Import was not possible due to an error: %q\n", err)
return errImportFailed
} }
return fmt.Errorf("failed to open a connection to database: %v", err) log.Errorf("Error while connecting to DB: %q", err)
return errDBConnFailed
} }
defer db.Close() defer db.Close()
@ -179,6 +185,19 @@ func run() error { //nolint:gocognit
setting.SetDbIsSetUp(true) setting.SetDbIsSetUp(true)
// don't load the app, return early.
if doingImport {
lb, err := localbreach.Load(*importFlag)
if err != nil {
return err
}
log.Infof("Import succeeded: %+v", lb)
fmt.Fprint(os.Stdout, "\033[32m🗸\033[0m Import succeeded \n")
return nil
}
a := &app.App{} a := &app.App{}
if err = a.Init(setting, slogger, db); err != nil { if err = a.Init(setting, slogger, db); err != nil {