grafana/pkg/cmd/grafana-cli/main.go

103 lines
2.3 KiB
Go
Raw Normal View History

2016-02-15 07:09:34 -06:00
package main
import (
"fmt"
2016-03-28 14:42:26 -05:00
"os"
"runtime"
"github.com/fatih/color"
2016-02-15 07:09:34 -06:00
"github.com/grafana/grafana/pkg/cmd/grafana-cli/commands"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/services"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
"github.com/urfave/cli/v2"
2016-02-15 07:09:34 -06:00
)
2016-02-15 09:11:37 -06:00
var version = "master"
2016-02-15 07:09:34 -06:00
func main() {
setupLogging()
2016-02-15 07:09:34 -06:00
app := &cli.App{
Name: "Grafana CLI",
Authors: []*cli.Author{
{
Name: "Grafana Project",
Email: "hello@grafana.com",
},
Grafana-CLI: Wrapper for `grafana-cli` within RPM/DEB packages and config/homepath are now global flags (#17695) * Feature: Introduce a grafana-cli wrapper When our users install the *nix packed version of grafana, tendency is to use the services and scripts installed as part of the package for grafana-server. These leverage the default configuration options by specifying the several default paths. This introduces a similar approach for the grafana-cli binary. We exposed it through a wrapper to ensure a proper configuration is in place. To enable that, we add the .real suffix to the original binary (grafana-cli.real) and then use a bash script named grafana-cli as the wrapper. * Make the config and homepath flags global * Introduce `configOverrides` as a global flag This flag allows us to pass configuration overrides as a string. The string follows the convention of configuration arguments separated by a space e.g. "cfg:default.paths.data=/dev/nullX cfg:default.paths.logs=/dev/nullX" Also, it is backwards compatible with similar the previous configuration method through tailing arguments. Tailing arguments take presedence over the configuration options string. * Only log configuration information in debug mode * Move the grafana-cli binary to $GRAFANA_HOME/bin As part of the package install process, we copy all the release files and directories into the grafana home directory. This includes the /bin folder from where we copied the binaries into their respective destinations. After that, the /bin folder gets deleted as we don't want to keep duplicates of the binaries around. As part of this commit, we moved the re-creation of /bin within grafana-home and the copy of the original binary (again) after the folder gets deleted.
2019-06-24 14:20:21 -05:00
},
Version: version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pluginsDir",
Usage: "Path to the Grafana plugin directory",
Value: utils.GetGrafanaPluginDir(runtime.GOOS),
EnvVars: []string{"GF_PLUGIN_DIR"},
},
&cli.StringFlag{
Name: "repo",
Usage: "URL to the plugin repository",
Value: "https://grafana.com/api/plugins",
EnvVars: []string{"GF_PLUGIN_REPO"},
},
&cli.StringFlag{
Name: "pluginUrl",
Usage: "Full url to the plugin zip file instead of downloading the plugin from grafana.com/api",
Value: "",
EnvVars: []string{"GF_PLUGIN_URL"},
},
&cli.BoolFlag{
Name: "insecure",
Usage: "Skip TLS verification (insecure)",
},
&cli.BoolFlag{
Name: "debug, d",
Usage: "Enable debug logging",
},
&cli.StringFlag{
Name: "configOverrides",
Usage: "Configuration options to override defaults as a string. e.g. cfg:default.paths.log=/dev/null",
},
&cli.StringFlag{
Name: "homepath",
Usage: "Path to Grafana install/home path, defaults to working directory",
},
&cli.StringFlag{
Name: "config",
Usage: "Path to config file",
},
Grafana-CLI: Wrapper for `grafana-cli` within RPM/DEB packages and config/homepath are now global flags (#17695) * Feature: Introduce a grafana-cli wrapper When our users install the *nix packed version of grafana, tendency is to use the services and scripts installed as part of the package for grafana-server. These leverage the default configuration options by specifying the several default paths. This introduces a similar approach for the grafana-cli binary. We exposed it through a wrapper to ensure a proper configuration is in place. To enable that, we add the .real suffix to the original binary (grafana-cli.real) and then use a bash script named grafana-cli as the wrapper. * Make the config and homepath flags global * Introduce `configOverrides` as a global flag This flag allows us to pass configuration overrides as a string. The string follows the convention of configuration arguments separated by a space e.g. "cfg:default.paths.data=/dev/nullX cfg:default.paths.logs=/dev/nullX" Also, it is backwards compatible with similar the previous configuration method through tailing arguments. Tailing arguments take presedence over the configuration options string. * Only log configuration information in debug mode * Move the grafana-cli binary to $GRAFANA_HOME/bin As part of the package install process, we copy all the release files and directories into the grafana home directory. This includes the /bin folder from where we copied the binaries into their respective destinations. After that, the /bin folder gets deleted as we don't want to keep duplicates of the binaries around. As part of this commit, we moved the re-creation of /bin within grafana-home and the copy of the original binary (again) after the folder gets deleted.
2019-06-24 14:20:21 -05:00
},
Commands: commands.Commands,
CommandNotFound: cmdNotFound,
2016-02-15 07:09:34 -06:00
}
app.Before = func(c *cli.Context) error {
services.Init(version, c.Bool("insecure"), c.Bool("debug"))
return nil
}
2016-02-15 07:09:34 -06:00
if err := app.Run(os.Args); err != nil {
logger.Errorf("%s: %s %s\n", color.RedString("Error"), color.RedString("✗"), err)
os.Exit(1)
2016-02-15 07:09:34 -06:00
}
}
func setupLogging() {
2016-02-15 07:09:34 -06:00
for _, f := range os.Args {
if f == "-d" || f == "--debug" || f == "-debug" {
logger.SetDebug(true)
2016-02-15 07:09:34 -06:00
}
}
}
func cmdNotFound(c *cli.Context, command string) {
fmt.Printf(
"%s: '%s' is not a %s command. See '%s --help'.\n",
c.App.Name,
command,
c.App.Name,
os.Args[0],
)
os.Exit(1)
}