Archived
1

Using flagger for config path getting

So, now we can deploy via go get and handle config in separate folder.
This commit is contained in:
Vladimir Hodakov 2017-11-22 16:32:10 +04:00
parent dfe0d08ecc
commit ef9ba0310c
4 changed files with 63 additions and 26 deletions

8
Gopkg.lock generated
View File

@ -55,6 +55,12 @@
packages = ["."]
revision = "eb3733d160e74a9c7e442f435eb3bea458e1d19f"
[[projects]]
branch = "master"
name = "lab.pztrn.name/golibs/flagger"
packages = ["."]
revision = "5cf6a6dd8fe8a4f37d1bb1e3deb5121c6e923668"
[[projects]]
branch = "master"
name = "lab.pztrn.name/golibs/mogrus"
@ -64,6 +70,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "3e9df6d446d8789d138790622cf19935d4331f9903af640d13654c07b9f433a5"
inputs-digest = "72b8805a04554297ce6fcb28ecaad016e3398624e90621de1b067cfe2f662e40"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -28,6 +28,7 @@ var (
func main() {
c := appcontext.New()
c.Init()
router.New(c)
migrations.New(c)
c.RunDatabaseMigrations()

View File

@ -20,53 +20,80 @@ import (
"lab.pztrn.name/fat0troll/i2_bot/lib/talkers/talkersinterface"
"lab.pztrn.name/fat0troll/i2_bot/lib/users/usersinterface"
"lab.pztrn.name/fat0troll/i2_bot/lib/welcomer/welcomerinterface"
"lab.pztrn.name/golibs/flagger"
"lab.pztrn.name/golibs/mogrus"
"os"
)
// Context is an application context struct
type Context struct {
Cfg *config.Config
Log *mogrus.LoggerHandler
Bot *tgbotapi.BotAPI
Forwarder forwarderinterface.ForwarderInterface
Migrations migrationsinterface.MigrationsInterface
Router routerinterface.RouterInterface
Pokedexer pokedexerinterface.PokedexerInterface
Db *sqlx.DB
Talkers talkersinterface.TalkersInterface
Broadcaster broadcasterinterface.BroadcasterInterface
Welcomer welcomerinterface.WelcomerInterface
Pinner pinnerinterface.PinnerInterface
Chatter chatterinterface.ChatterInterface
Squader squaderinterface.SquaderInterface
Users usersinterface.UsersInterface
Statistics statisticsinterface.StatisticsInterface
StartupFlags *flagger.Flagger
Cfg *config.Config
Log *mogrus.LoggerHandler
Bot *tgbotapi.BotAPI
Forwarder forwarderinterface.ForwarderInterface
Migrations migrationsinterface.MigrationsInterface
Router routerinterface.RouterInterface
Pokedexer pokedexerinterface.PokedexerInterface
Db *sqlx.DB
Talkers talkersinterface.TalkersInterface
Broadcaster broadcasterinterface.BroadcasterInterface
Welcomer welcomerinterface.WelcomerInterface
Pinner pinnerinterface.PinnerInterface
Chatter chatterinterface.ChatterInterface
Squader squaderinterface.SquaderInterface
Users usersinterface.UsersInterface
Statistics statisticsinterface.StatisticsInterface
}
// Init is a initialization function for context
func (c *Context) Init() {
c.Cfg = config.New()
c.Cfg.Init()
l := mogrus.New()
l.Initialize()
log := l.CreateLogger("i2_bot")
log.CreateOutput("stdout", os.Stdout, true, "debug")
c.Log = log
c.StartupFlags = flagger.New(c.Log)
c.StartupFlags.Initialize()
// Adding available startup flags here
configFlag := flagger.Flag{}
configFlag.Name = "config"
configFlag.Description = "Configuration file path"
configFlag.Type = "string"
configFlag.DefaultValue = "./config.yaml"
err := c.StartupFlags.AddFlag(&configFlag)
if err != nil {
c.Log.Errorln(err)
}
c.StartupFlags.Parse()
configPath, err := c.StartupFlags.GetStringValue("config")
if err != nil {
c.Log.Errorln(err)
c.Log.Fatal("Can't get config file parameter from command line. Exiting.")
}
c.Cfg = config.New()
c.Cfg.Init(c.Log, configPath)
logFile, err := os.OpenFile(c.Cfg.Logs.LogPath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
log.Fatalln(err)
}
log.CreateOutput("file="+c.Cfg.Logs.LogPath, logFile, true, "debug")
c.Log = log
c.Log.CreateOutput("file="+c.Cfg.Logs.LogPath, logFile, true, "debug")
c.Bot = connections.BotInit(c.Cfg, c.Log)
c.Db = connections.DBInit(c.Cfg, c.Log)
}
// InitializeStartupFlags gives information about available startup flags
func (c *Context) InitializeStartupFlags() {
}
// RegisterRouterInterface registering router interface in application
func (c *Context) RegisterRouterInterface(ri routerinterface.RouterInterface) {
c.Router = ri

View File

@ -6,10 +6,11 @@ package config
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"lab.pztrn.name/golibs/mogrus"
"path/filepath"
)
// VERSION is the urrent bot's version
const VERSION = "0.51"
// DatabaseConnection handles database connection settings in config.yaml
@ -45,11 +46,13 @@ type Config struct {
}
// Init is a configuration initializer
func (c *Config) Init() {
fname, _ := filepath.Abs("./config.yml")
func (c *Config) Init(log *mogrus.LoggerHandler, configPath string) {
fname, _ := filepath.Abs(configPath)
yamlFile, yerr := ioutil.ReadFile(fname)
if yerr != nil {
log.Fatal("Can't read config file")
} else {
log.Info("Using " + configPath + " as config file.")
}
yperr := yaml.Unmarshal(yamlFile, c)