mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(cli): move cli into main repo
This commit is contained in:
28
pkg/cmd/grafana-cli/services/io_util.go
Normal file
28
pkg/cmd/grafana-cli/services/io_util.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
var IoUtil m.IoUtil = IoUtilImp{}
|
||||
|
||||
type IoUtilImp struct {
|
||||
}
|
||||
|
||||
func (i IoUtilImp) Stat(path string) (os.FileInfo, error) {
|
||||
return os.Stat(path)
|
||||
}
|
||||
|
||||
func (i IoUtilImp) RemoveAll(path string) error {
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
func (i IoUtilImp) ReadDir(path string) ([]os.FileInfo, error) {
|
||||
return ioutil.ReadDir(path)
|
||||
}
|
||||
|
||||
func (i IoUtilImp) ReadFile(filename string) ([]byte, error) {
|
||||
return ioutil.ReadFile(filename)
|
||||
}
|
||||
70
pkg/cmd/grafana-cli/services/services.go
Normal file
70
pkg/cmd/grafana-cli/services/services.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/franela/goreq"
|
||||
m "github.com/grafana/grafana/pkg/cmd/grafana-cli/models"
|
||||
"path"
|
||||
)
|
||||
|
||||
var IoHelper m.IoUtil = IoUtilImp{}
|
||||
|
||||
func ListAllPlugins() (m.PluginRepo, error) {
|
||||
res, _ := goreq.Request{Uri: "https://raw.githubusercontent.com/grafana/grafana-plugin-repository/master/repo.json"}.Do()
|
||||
|
||||
var resp m.PluginRepo
|
||||
err := res.Body.FromJsonTo(&resp)
|
||||
if err != nil {
|
||||
return m.PluginRepo{}, errors.New("Could not load plugin data")
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func ReadPlugin(pluginDir, pluginName string) m.InstalledPlugin {
|
||||
pluginDataPath := path.Join(pluginDir, pluginName, "plugin.json")
|
||||
pluginData, _ := IoHelper.ReadFile(pluginDataPath)
|
||||
|
||||
res := m.InstalledPlugin{}
|
||||
json.Unmarshal(pluginData, &res)
|
||||
|
||||
if res.Info.Version == "" {
|
||||
res.Info.Version = "0.0.0"
|
||||
}
|
||||
|
||||
if res.Id == "" {
|
||||
res.Id = res.Name
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func GetLocalPlugins(pluginDir string) []m.InstalledPlugin {
|
||||
result := make([]m.InstalledPlugin, 0)
|
||||
files, _ := IoHelper.ReadDir(pluginDir)
|
||||
for _, f := range files {
|
||||
res := ReadPlugin(pluginDir, f.Name())
|
||||
result = append(result, res)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func RemoveInstalledPlugin(pluginPath, id string) error {
|
||||
return IoHelper.RemoveAll(path.Join(pluginPath, id))
|
||||
}
|
||||
|
||||
func GetPlugin(id string) (m.Plugin, error) {
|
||||
resp, err := ListAllPlugins()
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
for _, i := range resp.Plugins {
|
||||
if i.Id == id {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
|
||||
return m.Plugin{}, errors.New("could not find plugin named \"" + id + "\"")
|
||||
}
|
||||
Reference in New Issue
Block a user