Files
grafana/pkg/util/cmd/cmd.go
Grot (@grafanabot) e7c8704090 [v9.3.x] Server: Switch from separate server & cli to a unified grafana binary (#62443)
* Server: Switch from separate server & cli to a unified grafana binary (#58286)

* avoid the need for a second bulky binary for grafana-cli

* look for grafana-server in $PATH as well as same directory

* implement unified "grafana" command

* update dockerfiles, fix grafana-cli -v

* update packaging to work with single binary

- add wrapper scripts for grafana and grafana-server
- update and sync package files
- implement --sign flag of build package command
- stop packaging scripts folder, they are not useful for end users
- add support for --configOverrides in server command
- remove unused nfpm.yaml config file

* windows support

(cherry picked from commit de99ce139c)

* Build: don't remove grafana-server and grafana-cli binaries from deb and rpm packages (#59890)

* don't remove grafana-server and grafana-cli binaries from /usr/share/grafana/bin in deb and rpm packages

* don't add config overrides in /usr/sbin/grafana-server

---------

Co-authored-by: Dan Cech <dcech@grafana.com>
2023-02-02 10:41:18 +02:00

66 lines
1.4 KiB
Go

package cmd
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
)
func RunGrafanaCmd(subCmd string) int {
curr, err := os.Executable()
if err != nil {
fmt.Println("Error locating executable:", err)
return 1
}
executable := "grafana"
if runtime.GOOS == "windows" {
executable += ".exe"
}
binary := filepath.Join(filepath.Dir(filepath.Clean(curr)), executable)
if _, err := os.Stat(binary); err != nil {
binary, err = exec.LookPath(executable)
if err != nil {
fmt.Printf("Error locating %s: %s\n", executable, err)
return 1
}
}
// windows doesn't support syscall.Exec so we just run the main binary as a command
if runtime.GOOS == "windows" {
// bypassing gosec G204 because we need to build the command programmatically
// nolint:gosec
cmd := exec.Command(binary, append([]string{subCmd}, os.Args[1:]...)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
err := cmd.Run()
if err == nil {
return 0
}
var exitError *exec.ExitError
if errors.As(err, &exitError) {
return exitError.ExitCode()
}
return 1
}
args := append([]string{"grafana", subCmd}, os.Args[1:]...)
// bypassing gosec G204 because we need to build the command programmatically
// nolint:gosec
execErr := syscall.Exec(binary, args, os.Environ())
if execErr != nil {
fmt.Printf("Error running %s: %s\n", binary, execErr)
return 1
}
return 0
}