提交 d5ecfb59 编写于 作者: 何延龙 提交者: kezhenxu94

[Enhancement] Detect configuration file (#13)

上级 8091f05a
......@@ -77,9 +77,9 @@ func main() {
}
app.Before = interceptor.BeforeChain([]cli.BeforeFunc{
expandConfigFile,
altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("config")),
setUpCommandLineContext,
expandConfigFile,
tryConfigFile(flags),
})
app.Flags = flags
......@@ -93,19 +93,31 @@ func expandConfigFile(c *cli.Context) error {
return c.Set("config", util.ExpandFilePath(c.String("config")))
}
func tryConfigFile(flags []cli.Flag) cli.BeforeFunc {
return func(c *cli.Context) error {
configFile := c.String("config")
if bytes, err := ioutil.ReadFile(configFile); err == nil {
log.Debug("Using configurations:\n", string(bytes))
err = altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("config"))(c)
if err != nil {
return err
}
} else if os.IsNotExist(err) {
log.Debugf("open %s no such file, skip loading configuration file\n", c.GlobalString("config"))
} else {
return err
}
return nil
}
}
func setUpCommandLineContext(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(logrus.DebugLevel)
log.Debugln("Debug mode is enabled")
}
configFile := c.String("config")
if bytes, err := ioutil.ReadFile(configFile); err == nil {
log.Debug("Using configurations:\n", string(bytes))
} else {
return err
}
return nil
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册