This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new d5ecfb5  [Enhancement] Detect configuration file (#13)
d5ecfb5 is described below

commit d5ecfb5985e92b939d45c314154ae4b8a79982d8
Author: 何延龙 <[email protected]>
AuthorDate: Sat Nov 30 16:43:56 2019 +0800

    [Enhancement] Detect configuration file (#13)
---
 swctl/main.go | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/swctl/main.go b/swctl/main.go
index dfc5248..f260fc2 100644
--- a/swctl/main.go
+++ b/swctl/main.go
@@ -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
 }

Reply via email to