pkg/cmd: Check errors (#19700)

* pkg/cmd: Check errors
* pkg/cmd: Make sure server waits on services, even in case of error
* pkg/cmd: Inform of error to show help
* pkg/cmd: Only warn on failure to send systemd notification
* pkg/cmd: Don't log errors stemming from context cancelation
* pkg/cmd: Don't fail if unable to write to systemd
This commit is contained in:
Arve Knudsen
2019-10-15 16:44:15 +02:00
committed by GitHub
parent 7da2156237
commit 573e78feeb
7 changed files with 70 additions and 34 deletions
+17 -5
View File
@@ -22,11 +22,14 @@ func runDbCommand(command func(commandLine utils.CommandLine, sqlStore *sqlstore
cfg := setting.NewCfg()
configOptions := strings.Split(cmd.GlobalString("configOverrides"), " ")
cfg.Load(&setting.CommandLineArgs{
if err := cfg.Load(&setting.CommandLineArgs{
Config: cmd.ConfigFile(),
HomePath: cmd.HomePath(),
Args: append(configOptions, cmd.Args()...), // tailing arguments have precedence over the options string
})
}); err != nil {
logger.Errorf("\n%s: Failed to load configuration", color.RedString("Error"))
os.Exit(1)
}
if debug {
cfg.LogConfigSources()
@@ -35,13 +38,19 @@ func runDbCommand(command func(commandLine utils.CommandLine, sqlStore *sqlstore
engine := &sqlstore.SqlStore{}
engine.Cfg = cfg
engine.Bus = bus.GetBus()
engine.Init()
if err := engine.Init(); err != nil {
logger.Errorf("\n%s: Failed to initialize SQL engine", color.RedString("Error"))
os.Exit(1)
}
if err := command(cmd, engine); err != nil {
logger.Errorf("\n%s: ", color.RedString("Error"))
logger.Errorf("%s\n\n", err)
cmd.ShowHelp()
if err := cmd.ShowHelp(); err != nil {
logger.Errorf("\n%s: Failed to show help: %s %s\n\n", color.RedString("Error"),
color.RedString("✗"), err)
}
os.Exit(1)
}
@@ -57,7 +66,10 @@ func runPluginCommand(command func(commandLine utils.CommandLine) error) func(co
logger.Errorf("\n%s: ", color.RedString("Error"))
logger.Errorf("%s %s\n\n", color.RedString("✗"), err)
cmd.ShowHelp()
if err := cmd.ShowHelp(); err != nil {
logger.Errorf("\n%s: Failed to show help: %s %s\n\n", color.RedString("Error"),
color.RedString("✗"), err)
}
os.Exit(1)
}
@@ -80,8 +80,9 @@ func (fcli *FakeCommandLine) FlagNames() []string {
return flagNames
}
func (fcli *FakeCommandLine) ShowHelp() {
func (fcli *FakeCommandLine) ShowHelp() error {
fcli.HelpShown = true
return nil
}
func (fcli *FakeCommandLine) Application() *cli.App {
@@ -121,7 +121,10 @@ func InstallPlugin(pluginName, version string, c utils.CommandLine) error {
res, _ := s.ReadPlugin(pluginFolder, pluginName)
for _, v := range res.Dependencies.Plugins {
InstallPlugin(v.Id, "", c)
if err := InstallPlugin(v.Id, "", c); err != nil {
return errutil.Wrapf(err, "Failed to install plugin '%s'", v.Id)
}
logger.Infof("Installed dependency: %v ✔\n", v.Id)
}
@@ -5,6 +5,7 @@ import (
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
s "github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
"github.com/grafana/grafana/pkg/util/errutil"
)
func upgradeCommand(c utils.CommandLine) error {
@@ -24,7 +25,10 @@ func upgradeCommand(c utils.CommandLine) error {
}
if shouldUpgrade(localPlugin.Info.Version, &plugin) {
s.RemoveInstalledPlugin(pluginsDir, pluginName)
if err := s.RemoveInstalledPlugin(pluginsDir, pluginName); err != nil {
return errutil.Wrapf(err, "Failed to remove plugin '%s'", pluginName)
}
return InstallPlugin(pluginName, "", c)
}
+3 -1
View File
@@ -80,7 +80,9 @@ func ReadPlugin(pluginDir, pluginName string) (m.InstalledPlugin, error) {
}
res := m.InstalledPlugin{}
json.Unmarshal(data, &res)
if err := json.Unmarshal(data, &res); err != nil {
return res, err
}
if res.Info.Version == "" {
res.Info.Version = "0.0.0"
+3 -3
View File
@@ -7,7 +7,7 @@ import (
)
type CommandLine interface {
ShowHelp()
ShowHelp() error
ShowVersion()
Application() *cli.App
Args() cli.Args
@@ -35,8 +35,8 @@ type ContextCommandLine struct {
*cli.Context
}
func (c *ContextCommandLine) ShowHelp() {
cli.ShowCommandHelp(c.Context, c.Command.Name)
func (c *ContextCommandLine) ShowHelp() error {
return cli.ShowCommandHelp(c.Context, c.Command.Name)
}
func (c *ContextCommandLine) ShowVersion() {