2021-02-05 01:44:03 +00:00
|
|
|
package cliutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"github.com/urfave/cli/v2/altsrc"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/config"
|
|
|
|
"github.com/cloudflare/cloudflared/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Action(actionFunc cli.ActionFunc) cli.ActionFunc {
|
2021-03-16 22:36:46 +00:00
|
|
|
return WithErrorHandler(actionFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConfiguredAction(actionFunc cli.ActionFunc) cli.ActionFunc {
|
2021-02-05 01:44:03 +00:00
|
|
|
return WithErrorHandler(func(c *cli.Context) error {
|
|
|
|
if err := setFlagsFromConfigFile(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return actionFunc(c)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func setFlagsFromConfigFile(c *cli.Context) error {
|
|
|
|
const errorExitCode = 1
|
|
|
|
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
|
|
|
|
inputSource, err := config.ReadConfigFile(c, log)
|
|
|
|
if err != nil {
|
|
|
|
if err == config.ErrNoConfigFile {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return cli.Exit(err, errorExitCode)
|
|
|
|
}
|
|
|
|
|
2021-03-24 02:52:14 +00:00
|
|
|
if err := altsrc.ApplyInputSource(c, inputSource); err != nil {
|
2021-02-05 01:44:03 +00:00
|
|
|
return cli.Exit(err, errorExitCode)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|