Skip to content

Commit a939e59

Browse files
committed
include the latest api
1 parent ec47344 commit a939e59

File tree

447 files changed

+81319
-59757
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

447 files changed

+81319
-59757
lines changed

Diff for: Magefile.go

+3-278
Original file line numberDiff line numberDiff line change
@@ -3,284 +3,9 @@
33
package main
44

55
import (
6-
"bufio"
7-
"encoding/json"
8-
"fmt"
9-
"io/ioutil"
10-
"log"
11-
"os"
12-
"path"
13-
"path/filepath"
14-
"runtime"
15-
"strconv"
16-
"strings"
17-
"syscall"
18-
"time"
19-
20-
"github.com/magefile/mage/mg"
21-
"github.com/magefile/mage/sh"
6+
// mage:import
7+
build "github.com/grafana/grafana-plugin-sdk-go/build"
228
)
239

24-
var exname string = ""
25-
26-
func getExecutableName(os string, arch string) string {
27-
if exname == "" {
28-
var err error
29-
exname, err = getExecutableFromPluginJSON()
30-
if err != nil {
31-
exname = "set_exe_name_in_plugin_json" // warning in the final name?
32-
}
33-
}
34-
35-
exeName := fmt.Sprintf("%s_%s_%s", exname, os, arch)
36-
if "windows" == os {
37-
exeName = fmt.Sprintf("%s.exe", exeName)
38-
}
39-
return exeName
40-
}
41-
42-
func getExecutableFromPluginJSON() (string, error) {
43-
byteValue, err := readFileBytes(path.Join("src", "plugin.json"))
44-
if err != nil {
45-
return "", err
46-
}
47-
48-
var result map[string]interface{}
49-
err = json.Unmarshal([]byte(byteValue), &result)
50-
if err != nil {
51-
return "", err
52-
}
53-
return result["executable"].(string), nil
54-
}
55-
56-
func readFileBytes(file string) ([]byte, error) {
57-
jsonFile, err := os.Open(file)
58-
if err != nil {
59-
return nil, err
60-
}
61-
defer func() {
62-
_ = jsonFile.Close()
63-
}()
64-
65-
return ioutil.ReadAll(jsonFile)
66-
}
67-
68-
func findRunningPIDs(exe string) []int {
69-
pids := []int{}
70-
out, err := sh.Output("pgrep", exe[:15]) // full name does not match, only the prefix (on linux anyway)
71-
72-
if err != nil || out == "" {
73-
return pids
74-
}
75-
for _, txt := range strings.Fields(out) {
76-
pid, err := strconv.Atoi(txt)
77-
if err == nil {
78-
pids = append(pids, pid)
79-
} else {
80-
fmt.Printf("Unable to format %s (%s)", txt, err)
81-
}
82-
}
83-
return pids
84-
}
85-
86-
func killAllPIDs(pids []int) error {
87-
for _, pid := range pids {
88-
fmt.Printf("Killing process: %d\n", pid)
89-
err := syscall.Kill(pid, 9)
90-
if err != nil {
91-
return err
92-
}
93-
}
94-
return nil
95-
}
96-
97-
func buildBackend(os string, arch string, enableDebug bool) error {
98-
exeName := getExecutableName(os, arch)
99-
100-
args := []string{
101-
"build", "-o", path.Join("dist", exeName), "-tags", "netgo",
102-
}
103-
if enableDebug {
104-
args = append(args, "-gcflags=all=-N -l")
105-
} else {
106-
args = append(args, "-ldflags", "-w")
107-
}
108-
args = append(args, "./pkg")
109-
110-
env := map[string]string{
111-
"GOARCH": arch,
112-
"GOOS": os,
113-
}
114-
115-
// TODO: Change to sh.RunWithV once available.
116-
return sh.RunWith(env, "go", args...)
117-
}
118-
119-
// Build is a namespace.
120-
type Build mg.Namespace
121-
122-
// Linux builds the back-end plugin for Linux.
123-
func (Build) Linux() error {
124-
return buildBackend("linux", "amd64", false)
125-
}
126-
127-
// Windows builds the back-end plugin for Windows.
128-
func (Build) Windows() error {
129-
return buildBackend("windows", "amd64", false)
130-
}
131-
132-
// Darwin builds the back-end plugin for OSX.
133-
func (Build) Darwin() error {
134-
return buildBackend("darwin", "amd64", false)
135-
}
136-
137-
// Debug builds the debug version for the current platform
138-
func (Build) Debug() error {
139-
return buildBackend(runtime.GOOS, runtime.GOARCH, true)
140-
}
141-
142-
// Backend build a production build for all platforms
143-
func (Build) Backend() {
144-
b := Build{}
145-
mg.Deps(b.Linux, b.Windows, b.Darwin)
146-
}
147-
148-
// BuildAll builds production back-end components.
149-
func BuildAll() {
150-
b := Build{}
151-
mg.Deps(b.Backend)
152-
}
153-
154-
// Test runs backend tests.
155-
func Test() error {
156-
if err := sh.RunV("go", "test", "./pkg/..."); err != nil {
157-
return nil
158-
}
159-
return nil
160-
}
161-
162-
// Coverage runs backend tests and makes a coverage report.
163-
func Coverage() error {
164-
// Create a coverage file if it does not already exist
165-
_ = os.MkdirAll(filepath.Join(".", "coverage"), os.ModePerm)
166-
167-
if err := sh.RunV("go", "test", "./pkg/...", "-v", "-cover", "-coverprofile=coverage/backend.out"); err != nil {
168-
return nil
169-
}
170-
171-
if err := sh.RunV("go", "tool", "cover", "-html=coverage/backend.out", "-o", "coverage/backend.html"); err != nil {
172-
return nil
173-
}
174-
175-
return nil
176-
}
177-
178-
// Lint audits the source style
179-
func Lint() error {
180-
return sh.RunV("golangci-lint", "run", "./...")
181-
}
182-
183-
// Format formats the sources.
184-
func Format() error {
185-
if err := sh.RunV("gofmt", "-w", "."); err != nil {
186-
return err
187-
}
188-
189-
return nil
190-
}
191-
192-
// Clean cleans build artifacts, by deleting the dist directory.
193-
func Clean() error {
194-
err := os.RemoveAll("dist")
195-
if err != nil {
196-
return err
197-
}
198-
199-
err = os.RemoveAll("coverage")
200-
if err != nil {
201-
return err
202-
}
203-
204-
err = os.RemoveAll("ci")
205-
if err != nil {
206-
return err
207-
}
208-
return nil
209-
}
210-
211-
func checkLinuxPtraceScope() {
212-
ptracePath := "/proc/sys/kernel/yama/ptrace_scope"
213-
byteValue, err := readFileBytes(ptracePath)
214-
if err != nil {
215-
fmt.Printf("Unable to read ptrace_scope\n")
216-
}
217-
val := strings.TrimSpace(string(byteValue[:]))
218-
if "0" != val {
219-
fmt.Printf("WARNING: \n")
220-
fmt.Printf("ptrace_scope set to value other than 0 (currenlty:%s), this might prevent debugger from connecting\n", val)
221-
fmt.Printf("try writing \"0\" to %s\n", ptracePath)
222-
fmt.Printf("Set ptrace_scope to 0? y/N (default N)\n")
223-
224-
scanner := bufio.NewScanner(os.Stdin)
225-
if scanner.Scan() {
226-
if scanner.Text() == "y" || scanner.Text() == "Y" {
227-
// if err := sh.RunV("echo", "0", "|", "sudo", "tee", ptracePath); err != nil {
228-
// return // Error?
229-
// }
230-
fmt.Printf("TODO, run: echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope\n")
231-
} else {
232-
fmt.Printf("Did not write\n")
233-
}
234-
}
235-
}
236-
}
237-
238-
// Debugger makes a new debug build and attaches dlv (go-delve).
239-
func Debugger() error {
240-
// Debug build
241-
b := Build{}
242-
mg.Deps(b.Debug)
243-
244-
// 1. kill any running instance
245-
exeName := getExecutableName(runtime.GOOS, runtime.GOARCH)
246-
247-
// Kill any running processs
248-
_ = killAllPIDs(findRunningPIDs(exeName))
249-
_ = sh.RunV("pkill", "dlv")
250-
251-
if runtime.GOOS == "linux" {
252-
checkLinuxPtraceScope()
253-
}
254-
255-
// Wait for grafana to start plugin
256-
for i := 0; i < 20; i++ {
257-
pids := findRunningPIDs(exeName)
258-
if len(pids) > 1 {
259-
return fmt.Errorf("multiple instances already running")
260-
}
261-
if len(pids) > 0 {
262-
pid := strconv.Itoa(pids[0])
263-
log.Printf("Running PID: %s", pid)
264-
265-
// dlv attach ${PLUGIN_PID} --headless --listen=:${PORT} --api-version 2 --log
266-
if err := sh.RunV("dlv",
267-
"attach",
268-
pid,
269-
"--headless",
270-
"--listen=:3222",
271-
"--api-version", "2",
272-
"--log"); err != nil {
273-
return err
274-
}
275-
fmt.Printf("dlv finished running (%s)\n", pid)
276-
return nil
277-
}
278-
279-
log.Printf("waiting for grafana to start: %s...", exeName)
280-
time.Sleep(250 * time.Millisecond)
281-
}
282-
return fmt.Errorf("could not find process: %s, perhaps grafana is not running?", exeName)
283-
}
284-
28510
// Default configures the default target.
286-
var Default = BuildAll
11+
var Default = build.BuildAll

Diff for: go.mod

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ go 1.14
55
replace github.com/aws/aws-sdk-go => ./tmp/github.com/aws/aws-sdk-go
66

77
require (
8-
github.com/aws/aws-sdk-go v1.29.27
9-
github.com/golang/protobuf v1.3.3 // indirect; indirect github.com/grafana/grafana-plugin-sdk-go v0.28.0
10-
github.com/grafana/grafana-plugin-sdk-go v0.31.0
8+
github.com/aws/aws-sdk-go v1.30.20
9+
github.com/grafana/grafana-plugin-sdk-go v0.60.0
1110
github.com/kr/pretty v0.2.0 // indirect
1211
github.com/magefile/mage v1.9.0
1312
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
1413
github.com/oklog/run v1.1.0 // indirect
15-
github.com/patrickmn/go-cache v2.1.0+incompatible
1614
github.com/prometheus/client_golang v1.3.0
1715
)

0 commit comments

Comments
 (0)