...
1 package helper
2
3 import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7 "net/url"
8 "regexp"
9 "strings"
10
11 . "github.com/onsi/ginkgo/v2"
12
13 "github.com/redhat-developer/odo/pkg/api"
14 )
15
16
17 var versionRe = regexp.MustCompile(`(\d.\d.\d)`)
18
19 type Registry struct {
20 url string
21 }
22
23 func NewRegistry(url string) Registry {
24 return Registry{
25 url: url,
26 }
27 }
28
29 func (o Registry) GetIndex() ([]api.DevfileStack, error) {
30 url, err := url.JoinPath(strings.TrimSuffix(o.url, "/"), "/v2index")
31 if err != nil {
32 return nil, err
33 }
34 resp, err := http.Get(url)
35 if err != nil {
36 return nil, err
37 }
38 defer func() {
39 if cErr := resp.Body.Close(); cErr != nil {
40 fmt.Fprintf(GinkgoWriter, "[warn] error closing response body: %v\n", cErr)
41 }
42 }()
43
44 var target []api.DevfileStack
45 err = json.NewDecoder(resp.Body).Decode(&target)
46 if err != nil {
47 return nil, err
48 }
49 return target, nil
50 }
51
52 func (o Registry) GetStack(name string) (api.DevfileStack, error) {
53 index, err := o.GetIndex()
54 if err != nil {
55 return api.DevfileStack{}, err
56 }
57 for _, stack := range index {
58 if stack.Name == name {
59 return stack, nil
60 }
61 }
62 return api.DevfileStack{}, fmt.Errorf("stack %q not found", name)
63 }
64
65
66
67
68
69 func GetVersions(registryName string, stackName string) []string {
70 devfileReg := "DefaultDevfileRegistry"
71 if registryName != "" {
72 devfileReg = registryName
73 }
74 out := Cmd("odo", "registry", "--devfile", stackName, "--devfile-registry", devfileReg).ShouldPass().Out()
75 return versionRe.FindAllString(out, -1)
76 }
77
78
79
80
81
82
83 func HasAtLeastTwoVersions(registryName string, stackName string) bool {
84 return len(GetVersions(registryName, stackName)) >= 2
85 }
86
87 type RegistryServer interface {
88 Start() (url string, err error)
89 Stop() error
90 IsStarted() bool
91 GetUrl() string
92 }
93
View as plain text