github.com/pavlo67/common@v0.5.3/common/db/db_sqlite/starter.go (about)

     1  package db_sqlite
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/pavlo67/common/common"
     8  	"github.com/pavlo67/common/common/config"
     9  	"github.com/pavlo67/common/common/errors"
    10  	"github.com/pavlo67/common/common/joiner"
    11  	"github.com/pavlo67/common/common/logger"
    12  	"github.com/pavlo67/common/common/sqllib/sqllib_sqlite"
    13  	"github.com/pavlo67/common/common/starter"
    14  )
    15  
    16  const InterfaceKey joiner.InterfaceKey = "db_sqlite"
    17  
    18  func Starter() starter.Operator {
    19  	return &connectSQLiteStarter{}
    20  }
    21  
    22  var l logger.Operator
    23  var _ starter.Operator = &connectSQLiteStarter{}
    24  
    25  type connectSQLiteStarter struct {
    26  	cfgSQLite config.Access
    27  
    28  	interfaceKey joiner.InterfaceKey
    29  }
    30  
    31  func (css *connectSQLiteStarter) Name() string {
    32  	return logger.GetCallInfo().PackageName
    33  }
    34  
    35  const onRun = "on connectSQLiteStarter.Run()"
    36  
    37  func (css *connectSQLiteStarter) Run(env *config.Envs, options common.Map, joinerOp joiner.Operator, l_ logger.Operator) error {
    38  	l = l_
    39  
    40  	if err := env.Value(options.StringDefault("db_key", "db_sqlite"), &css.cfgSQLite); err != nil {
    41  		return err
    42  	}
    43  
    44  	css.interfaceKey = joiner.InterfaceKey(options.StringDefault("interface_key", string(InterfaceKey)))
    45  
    46  	if os.Getenv("SHOW_CONNECTS") != "" {
    47  		l.Infof("CONNECTING TO SQLITE: %#v", css.cfgSQLite)
    48  	}
    49  
    50  	db, err := sqllib_sqlite.Connect(css.cfgSQLite)
    51  	if err != nil || db == nil {
    52  		return errors.CommonError(err, fmt.Sprintf(onRun+": got %#v", db))
    53  	}
    54  
    55  	if err = joinerOp.Join(db, css.interfaceKey); err != nil {
    56  		return errors.CommonError(err, fmt.Sprintf("can't join *sql.DB with key '%s'", css.interfaceKey))
    57  	}
    58  
    59  	return nil
    60  }