-
Notifications
You must be signed in to change notification settings - Fork 11.6k
/
ifaces.go
95 lines (82 loc) · 2.2 KB
/
ifaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package repo
import (
"context"
"fmt"
"strings"
)
// Service is responsible for retrieving plugin archive information from a repository.
type Service interface {
// GetPluginArchive fetches the requested plugin archive.
GetPluginArchive(ctx context.Context, pluginID, version string, opts CompatOpts) (*PluginArchive, error)
// GetPluginArchiveByURL fetches the requested plugin from the specified URL.
GetPluginArchiveByURL(ctx context.Context, archiveURL string, opts CompatOpts) (*PluginArchive, error)
// GetPluginArchiveInfo fetches information needed for downloading the requested plugin.
GetPluginArchiveInfo(ctx context.Context, pluginID, version string, opts CompatOpts) (*PluginArchiveInfo, error)
}
type CompatOpts struct {
grafanaVersion string
system SystemCompatOpts
}
type SystemCompatOpts struct {
os string
arch string
}
func (co CompatOpts) GrafanaVersion() (string, bool) {
if len(co.grafanaVersion) > 0 {
return co.grafanaVersion, true
}
return "", false
}
func (co CompatOpts) System() (SystemCompatOpts, bool) {
os, osSet := co.system.OS()
arch, archSet := co.system.Arch()
if !osSet || !archSet {
return SystemCompatOpts{}, false
}
return SystemCompatOpts{os: os, arch: arch}, true
}
func (co SystemCompatOpts) OS() (string, bool) {
if len(co.os) > 0 {
return co.os, true
}
return "", false
}
func (co SystemCompatOpts) Arch() (string, bool) {
if len(co.arch) > 0 {
return co.arch, true
}
return "", false
}
func NewCompatOpts(grafanaVersion, os, arch string) CompatOpts {
return CompatOpts{
grafanaVersion: grafanaVersion,
system: SystemCompatOpts{
os: os,
arch: arch,
},
}
}
func NewSystemCompatOpts(os, arch string) CompatOpts {
return CompatOpts{
system: SystemCompatOpts{
os: os,
arch: arch,
},
}
}
func (co SystemCompatOpts) OSAndArch() string {
if os, exists := co.OS(); !exists {
return ""
} else if arch, exists := co.Arch(); !exists {
return ""
} else {
return fmt.Sprintf("%s-%s", strings.ToLower(os), arch)
}
}
func (co CompatOpts) String() string {
grafanaVersion, exists := co.GrafanaVersion()
if !exists {
return co.system.OSAndArch()
}
return fmt.Sprintf("Grafana v%s %s", grafanaVersion, co.system.OSAndArch())
}