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

View File

@ -28,6 +28,7 @@ var (
func main() { func main() {
c := appcontext.New() c := appcontext.New()
c.Init() c.Init()
router.New(c) router.New(c)
migrations.New(c) migrations.New(c)
c.RunDatabaseMigrations() 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/talkers/talkersinterface"
"lab.pztrn.name/fat0troll/i2_bot/lib/users/usersinterface" "lab.pztrn.name/fat0troll/i2_bot/lib/users/usersinterface"
"lab.pztrn.name/fat0troll/i2_bot/lib/welcomer/welcomerinterface" "lab.pztrn.name/fat0troll/i2_bot/lib/welcomer/welcomerinterface"
"lab.pztrn.name/golibs/flagger"
"lab.pztrn.name/golibs/mogrus" "lab.pztrn.name/golibs/mogrus"
"os" "os"
) )
// Context is an application context struct // Context is an application context struct
type Context struct { type Context struct {
Cfg *config.Config StartupFlags *flagger.Flagger
Log *mogrus.LoggerHandler Cfg *config.Config
Bot *tgbotapi.BotAPI Log *mogrus.LoggerHandler
Forwarder forwarderinterface.ForwarderInterface Bot *tgbotapi.BotAPI
Migrations migrationsinterface.MigrationsInterface Forwarder forwarderinterface.ForwarderInterface
Router routerinterface.RouterInterface Migrations migrationsinterface.MigrationsInterface
Pokedexer pokedexerinterface.PokedexerInterface Router routerinterface.RouterInterface
Db *sqlx.DB Pokedexer pokedexerinterface.PokedexerInterface
Talkers talkersinterface.TalkersInterface Db *sqlx.DB
Broadcaster broadcasterinterface.BroadcasterInterface Talkers talkersinterface.TalkersInterface
Welcomer welcomerinterface.WelcomerInterface Broadcaster broadcasterinterface.BroadcasterInterface
Pinner pinnerinterface.PinnerInterface Welcomer welcomerinterface.WelcomerInterface
Chatter chatterinterface.ChatterInterface Pinner pinnerinterface.PinnerInterface
Squader squaderinterface.SquaderInterface Chatter chatterinterface.ChatterInterface
Users usersinterface.UsersInterface Squader squaderinterface.SquaderInterface
Statistics statisticsinterface.StatisticsInterface Users usersinterface.UsersInterface
Statistics statisticsinterface.StatisticsInterface
} }
// Init is a initialization function for context // Init is a initialization function for context
func (c *Context) Init() { func (c *Context) Init() {
c.Cfg = config.New()
c.Cfg.Init()
l := mogrus.New() l := mogrus.New()
l.Initialize() l.Initialize()
log := l.CreateLogger("i2_bot") log := l.CreateLogger("i2_bot")
log.CreateOutput("stdout", os.Stdout, true, "debug") 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) logFile, err := os.OpenFile(c.Cfg.Logs.LogPath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
log.CreateOutput("file="+c.Cfg.Logs.LogPath, logFile, true, "debug") c.Log.CreateOutput("file="+c.Cfg.Logs.LogPath, logFile, true, "debug")
c.Log = log
c.Bot = connections.BotInit(c.Cfg, c.Log) c.Bot = connections.BotInit(c.Cfg, c.Log)
c.Db = connections.DBInit(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 // RegisterRouterInterface registering router interface in application
func (c *Context) RegisterRouterInterface(ri routerinterface.RouterInterface) { func (c *Context) RegisterRouterInterface(ri routerinterface.RouterInterface) {
c.Router = ri c.Router = ri

View File

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