-
Notifications
You must be signed in to change notification settings - Fork 153
/
get.go
107 lines (85 loc) · 2.84 KB
/
get.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
96
97
98
99
100
101
102
103
104
105
106
107
package profiles
import (
"context"
"fmt"
"io"
"strings"
pb "github.com/weaveworks/weave-gitops/pkg/api/profiles"
"github.com/weaveworks/weave-gitops/pkg/helm/watcher/controller"
"k8s.io/apimachinery/pkg/api/errors"
)
type ProfilesRetriever interface {
Source() string
RetrieveProfiles() (*pb.GetProfilesResponse, error)
}
type GetOptions struct {
Name string
Version string
Cluster string
Namespace string
Writer io.Writer
Port string
}
func (s *ProfilesSvc) Get(ctx context.Context, r ProfilesRetriever, w io.Writer) error {
profiles, err := r.RetrieveProfiles()
if err != nil {
if e, ok := err.(*errors.StatusError); ok {
return fmt.Errorf("unable to retrieve profiles from %q: status code %d", r.Source(), e.ErrStatus.Code)
}
return fmt.Errorf("unable to retrieve profiles from %q: %w", r.Source(), err)
}
printProfiles(profiles, w)
return nil
}
// GetProfile returns a single available profile.
func (s *ProfilesSvc) GetProfile(ctx context.Context, r ProfilesRetriever, opts GetOptions) (*pb.Profile, string, error) {
s.Logger.Actionf("getting available profiles from %s", r.Source())
profilesList, err := r.RetrieveProfiles()
if err != nil {
return nil, "", fmt.Errorf("unable to retrieve profiles from %q: %w", r.Source(), err)
}
var version string
for _, p := range profilesList.Profiles {
if p.Name == opts.Name {
if len(p.AvailableVersions) == 0 {
return nil, "", fmt.Errorf("no version found for profile '%s' in %s/%s", p.Name, opts.Cluster, opts.Namespace)
}
switch {
case opts.Version == "latest":
versions, err := controller.ConvertStringListToSemanticVersionList(p.AvailableVersions)
if err != nil {
return nil, "", err
}
controller.SortVersions(versions)
version = versions[0].String()
default:
if !foundVersion(p.AvailableVersions, opts.Version) {
return nil, "", fmt.Errorf("version '%s' not found for profile '%s' in %s/%s", opts.Version, opts.Name, opts.Cluster, opts.Namespace)
}
version = opts.Version
}
if p.GetHelmRepository().GetName() == "" || p.GetHelmRepository().GetNamespace() == "" {
return nil, "", fmt.Errorf("HelmRepository's name or namespace is empty")
}
return p, version, nil
}
}
return nil, "", fmt.Errorf("no available profile '%s' found in %s/%s", opts.Name, opts.Cluster, opts.Namespace)
}
func foundVersion(availableVersions []string, version string) bool {
for _, v := range availableVersions {
if v == version {
return true
}
}
return false
}
func printProfiles(profiles *pb.GetProfilesResponse, w io.Writer) {
fmt.Fprintf(w, "NAME\tDESCRIPTION\tAVAILABLE_VERSIONS\n")
if profiles.Profiles != nil && len(profiles.Profiles) > 0 {
for _, p := range profiles.Profiles {
fmt.Fprintf(w, "%s\t%s\t%v", p.Name, p.Description, strings.Join(p.AvailableVersions, ","))
fmt.Fprintln(w, "")
}
}
}