-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathview.go
112 lines (100 loc) · 2.89 KB
/
view.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
108
109
110
111
112
package create
import (
"context"
"errors"
"fmt"
"strings"
"github.com/charmbracelet/huh"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/registry"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type CreateView struct {
ProjectName string
Public bool
RegistryID string
StorageLimit string
ProxyCache bool
}
func getRegistryList() (*registry.ListRegistriesOK, error) {
credentialName := viper.GetString("current-credential-name")
client := utils.GetClientByCredentialName(credentialName)
ctx := context.Background()
response, err := client.Registry.ListRegistries(ctx, ®istry.ListRegistriesParams{})
if err != nil {
return nil, err
}
return response, nil
}
func CreateProjectView(createView *CreateView) {
theme := huh.ThemeCharm()
// I want it to be a map of registry ID to registry name
registries, _ := getRegistryList()
registryOptions := map[string]string{}
for _, registry := range registries.Payload {
regiId := fmt.Sprintf("%d", registry.ID)
registryOptions[regiId] = fmt.Sprintf("%s (%s)", registry.Name, registry.URL)
}
var registrySelectOptions []huh.Option[string]
for id, name := range registryOptions {
registrySelectOptions = append(registrySelectOptions, huh.NewOption(name, id))
}
err := huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Project Name").
Value(&createView.ProjectName).
Validate(func(str string) error {
if strings.TrimSpace(str) == "" {
return errors.New("project name cannot be empty or only spaces")
}
if isValid := utils.ValidateProjectName(str); !isValid {
return errors.New("please enter correct project name format")
}
return nil
}),
huh.NewConfirm().
Title("Public").
Value(&createView.Public).
Affirmative("yes").
Negative("no"),
huh.NewInput().
Title("Storage Limit").
Value(&createView.StorageLimit).
Validate(func(str string) error {
// Assuming StorageLimit is an int64
if strings.TrimSpace(str) == "" {
return errors.New("storage limit cannot be empty or only spaces")
}
if err := utils.ValidateStorageLimit(str); err != nil {
return err
}
return nil
}),
huh.NewConfirm().
Title("Proxy Cache").
Value(&createView.ProxyCache).
Affirmative("yes").
Negative("no"),
),
huh.NewGroup(
huh.NewSelect[string]().
Validate(func(str string) error {
if createView.ProxyCache && str == "" {
return errors.New("registry ID cannot be empty")
}
return nil
}).
Description("Select a registry to reference when creating the proxy cache project").
Title("Registry ID").
Value(&createView.RegistryID).
Options(registrySelectOptions...),
).WithHideFunc(func() bool {
return !createView.ProxyCache || len(registryOptions) == 0
}),
).WithTheme(theme).Run()
if err != nil {
log.Fatal(err)
}
}