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-05-06 20:10:47 +00:00
|
|
|
// Adapt actionFunc to the type signature required by ConfiguredActionWithWarnings
|
|
|
|
f := func(context *cli.Context, _ string) error {
|
|
|
|
return actionFunc(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConfiguredActionWithWarnings(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just like ConfiguredAction, but accepts a second parameter with configuration warnings.
|
|
|
|
func ConfiguredActionWithWarnings(actionFunc func(*cli.Context, string) error) cli.ActionFunc {
|
2021-02-05 01:44:03 +00:00
|
|
|
return WithErrorHandler(func(c *cli.Context) error {
|
2021-05-06 20:10:47 +00:00
|
|
|
warnings, err := setFlagsFromConfigFile(c)
|
|
|
|
if err != nil {
|
2021-02-05 01:44:03 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-05-06 20:10:47 +00:00
|
|
|
return actionFunc(c, warnings)
|
2021-02-05 01:44:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-06 20:10:47 +00:00
|
|
|
func setFlagsFromConfigFile(c *cli.Context) (configWarnings string, err error) {
|
2021-02-05 01:44:03 +00:00
|
|
|
const errorExitCode = 1
|
|
|
|
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
|
2021-05-06 20:10:47 +00:00
|
|
|
inputSource, warnings, err := config.ReadConfigFile(c, log)
|
2021-02-05 01:44:03 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == config.ErrNoConfigFile {
|
2021-05-06 20:10:47 +00:00
|
|
|
return "", nil
|
2021-02-05 01:44:03 +00:00
|
|
|
}
|
2021-05-06 20:10:47 +00:00
|
|
|
return "", cli.Exit(err, errorExitCode)
|
2021-02-05 01:44:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 02:52:14 +00:00
|
|
|
if err := altsrc.ApplyInputSource(c, inputSource); err != nil {
|
2021-05-06 20:10:47 +00:00
|
|
|
return "", cli.Exit(err, errorExitCode)
|
2021-02-05 01:44:03 +00:00
|
|
|
}
|
2021-05-06 20:10:47 +00:00
|
|
|
return warnings, nil
|
2021-02-05 01:44:03 +00:00
|
|
|
}
|