-
Notifications
You must be signed in to change notification settings - Fork 72
/
get.go
118 lines (90 loc) · 2.76 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
108
109
110
111
112
113
114
115
116
117
118
package owner
import (
"github.com/AlecAivazis/survey/v2"
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/registrycmdutil"
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/rule/rulecmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
"k8s.io/utils/strings/slices"
)
type OwnerGetOptions struct {
artifact string
group string
registryID string
f *factory.Factory
}
// NewGetCommand creates a new command to get owner of an artifact
func NewGetCommand(f *factory.Factory) *cobra.Command {
opts := &OwnerGetOptions{
f: f,
}
cmd := &cobra.Command{
Use: "owner-get",
Short: f.Localizer.MustLocalize("artifact.cmd.owner.get.description.short"),
Long: f.Localizer.MustLocalize("artifact.cmd.owner.get.description.long"),
Example: f.Localizer.MustLocalize("artifact.cmd.owner.get.example"),
Args: cobra.NoArgs,
Hidden: true,
RunE: func(cmd *cobra.Command, _ []string) (err error) {
var missingFlags []string
if opts.artifact == "" {
missingFlags = append(missingFlags, "artifact-id")
}
if !opts.f.IOStreams.CanPrompt() && len(missingFlags) > 0 {
return flagutil.RequiredWhenNonInteractiveError(missingFlags...)
}
if len(missingFlags) > 0 {
err = runGetInteractivePrompt(opts, missingFlags)
if err != nil {
return err
}
}
if opts.registryID != "" {
return runGet(opts)
}
registryInstance, err := contextutil.GetCurrentRegistryInstance(f)
if err != nil {
return err
}
opts.registryID = registryInstance.GetId()
return runGet(opts)
},
}
flags := rulecmdutil.NewFlagSet(cmd, f)
flags.AddRegistryInstance(&opts.registryID)
flags.AddGroup(&opts.group)
flags.AddArtifactID(&opts.artifact)
return cmd
}
func runGet(opts *OwnerGetOptions) error {
conn, err := opts.f.Connection()
if err != nil {
return err
}
api := conn.API()
a, _, err := api.ServiceRegistryInstance(opts.registryID)
if err != nil {
return err
}
request := a.MetadataApi.GetArtifactOwner(opts.f.Context, opts.group, opts.artifact)
artifactOwner, _, err := request.Execute()
if err != nil {
return registrycmdutil.TransformInstanceError(err)
}
opts.f.Logger.Info(*artifactOwner.Owner)
return nil
}
func runGetInteractivePrompt(opts *OwnerGetOptions, missingFlags []string) (err error) {
if slices.Contains(missingFlags, "artifact-id") {
artifactIdPrompt := &survey.Input{
Message: opts.f.Localizer.MustLocalize("artifact.cmd.owner.get.input.artifactId.message"),
}
err = survey.AskOne(artifactIdPrompt, &opts.artifact)
if err != nil {
return err
}
}
return nil
}