Skip to content

Commit 3495d2d

Browse files
committed
fix: use yaml.v3 and address conflicts
2 parents bd40896 + b6427ee commit 3495d2d

9 files changed

Lines changed: 79 additions & 417 deletions

File tree

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ module github.com/ibm-verify/verifyctl
33
go 1.23.0
44

55
require (
6-
github.com/ibm-verify/verify-sdk-go v0.0.5
6+
github.com/ibm-verify/verify-sdk-go v0.0.7
77
github.com/oapi-codegen/oapi-codegen/v2 v2.4.1
88
github.com/oapi-codegen/runtime v1.1.1
99
github.com/spf13/cobra v1.8.1
1010
golang.org/x/oauth2 v0.26.0 // indirect
1111
gopkg.in/yaml.v3 v3.0.1
1212
)
1313

14+
require gopkg.in/yaml.v2 v2.4.0 // indirect
15+
1416
require (
1517
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
1618
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
@@ -28,7 +30,6 @@ require (
2830
golang.org/x/mod v0.17.0 // indirect
2931
golang.org/x/text v0.24.0 // indirect
3032
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
31-
gopkg.in/yaml.v2 v2.4.0 // indirect
3233
)
3334

3435
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4646
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4747
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
4848
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
49-
github.com/ibm-verify/verify-sdk-go v0.0.5 h1:+D1VdqZ9akfu0PZl4zBl1Sm5fNzXpmsadX+nQKOhkgA=
50-
github.com/ibm-verify/verify-sdk-go v0.0.5/go.mod h1:Vc1sOwMPmXCwHox3hLWQgt4DFaeXPpMyEd+IHZoAOgg=
49+
github.com/ibm-verify/verify-sdk-go v0.0.7 h1:nSaoOcm3VWmzDv6vcRlmTweNg2r3uA2oHrRV0KVgFpE=
50+
github.com/ibm-verify/verify-sdk-go v0.0.7/go.mod h1:Vc1sOwMPmXCwHox3hLWQgt4DFaeXPpMyEd+IHZoAOgg=
5151
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
5252
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
5353
github.com/invopop/yaml v0.3.1 h1:f0+ZpmhfBSS4MhG+4HYseMdJhoeeopbSKbq5Rpeelso=

pkg/cmd/create/api_client.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"io"
66
"os"
77

8+
"github.com/ibm-verify/verify-sdk-go/pkg/config/security"
89
"github.com/ibm-verify/verifyctl/pkg/cmd/resource"
910
"github.com/ibm-verify/verifyctl/pkg/config"
10-
"github.com/ibm-verify/verifyctl/pkg/module/security"
1111
cmdutil "github.com/ibm-verify/verifyctl/pkg/util/cmd"
1212
"github.com/ibm-verify/verifyctl/pkg/util/templates"
13+
"gopkg.in/yaml.v3"
1314

1415
contextx "github.com/ibm-verify/verify-sdk-go/pkg/core/context"
1516
errorsx "github.com/ibm-verify/verify-sdk-go/pkg/core/errors"
@@ -43,9 +44,13 @@ var (
4344
apiClientExamples = templates.Examples(cmdutil.TranslateExamples(apiClientMessagePrefix, `
4445
# Create an empty API client resource.
4546
verifyctl create apiclient --boilerplate
47+
48+
49+
# Create an API client using a YAML file.
50+
verifyctl create -f=./apiclient.yaml
4651
4752
# Create an API client using a JSON file.
48-
verifyctl create apiclient -f=./apiclient.json`))
53+
verifyctl create -f=./apiclient.json`))
4954
)
5055

5156
type apiClientOptions struct {
@@ -105,27 +110,26 @@ func (o *apiClientOptions) Run(cmd *cobra.Command, args []string) error {
105110
cmdutil.WriteString(cmd, entitlementsMessage+" "+apiClientEntitlements)
106111
return nil
107112
}
108-
109113
if o.boilerplate {
110114
resourceObj := &resource.ResourceObject{
111115
Kind: resource.ResourceTypePrefix + "ApiClient",
112116
APIVersion: "1.0",
113-
Data: &security.Client{},
117+
Data: security.APIClientExample(),
114118
}
115119

116120
cmdutil.WriteAsYAML(cmd, resourceObj, cmd.OutOrStdout())
117121
return nil
118122
}
119123

120-
auth, err := o.config.GetCurrentAuth()
124+
_, err := o.config.GetCurrentAuth()
121125
if err != nil {
122126
return err
123127
}
124128

125-
return o.createAPIClient(cmd, auth)
129+
return o.createAPIClient(cmd)
126130
}
127131

128-
func (o *apiClientOptions) createAPIClient(cmd *cobra.Command, auth *config.AuthConfig) error {
132+
func (o *apiClientOptions) createAPIClient(cmd *cobra.Command) error {
129133
ctx := cmd.Context()
130134
vc := contextx.GetVerifyContext(ctx)
131135

@@ -135,15 +139,15 @@ func (o *apiClientOptions) createAPIClient(cmd *cobra.Command, auth *config.Auth
135139
return err
136140
}
137141

138-
return o.createAPIClientWithData(cmd, auth, b)
142+
return o.createAPIClientWithData(cmd, b)
139143
}
140144

141-
func (o *apiClientOptions) createAPIClientWithData(cmd *cobra.Command, auth *config.AuthConfig, data []byte) error {
145+
func (o *apiClientOptions) createAPIClientWithData(cmd *cobra.Command, data []byte) error {
142146
ctx := cmd.Context()
143147
vc := contextx.GetVerifyContext(ctx)
144148

145149
apiclient := &security.APIClientConfig{}
146-
if err := json.Unmarshal(data, &apiclient); err != nil {
150+
if err := yaml.Unmarshal(data, &apiclient); err != nil {
147151
vc.Logger.Errorf("unable to unmarshal API client; err=%v", err)
148152
return err
149153
}
@@ -156,7 +160,7 @@ func (o *apiClientOptions) createAPIClientWithData(cmd *cobra.Command, auth *con
156160
}
157161

158162
client := security.NewAPIClient()
159-
resourceURI, err := client.CreateAPIClient(ctx, auth, apiclient)
163+
resourceURI, err := client.CreateAPIClient(ctx, apiclient)
160164
if err != nil {
161165
vc.Logger.Errorf("failed to create API client; err=%v", err)
162166
return err
@@ -166,11 +170,10 @@ func (o *apiClientOptions) createAPIClientWithData(cmd *cobra.Command, auth *con
166170
return nil
167171
}
168172

169-
func (o *apiClientOptions) createAPIClientFromDataMap(cmd *cobra.Command, auth *config.AuthConfig, data map[string]interface{}) error {
173+
func (o *apiClientOptions) createAPIClientFromDataMap(cmd *cobra.Command, data map[string]interface{}) error {
170174
ctx := cmd.Context()
171175
vc := contextx.GetVerifyContext(ctx)
172176

173-
// Convert map data to JSON
174177
apiclient := &security.APIClientConfig{}
175178
b, err := json.Marshal(data)
176179
if err != nil {
@@ -183,23 +186,20 @@ func (o *apiClientOptions) createAPIClientFromDataMap(cmd *cobra.Command, auth *
183186
return err
184187
}
185188

186-
// Validate required fields
187189
if apiclient.ClientName == "" {
188190
return errorsx.G11NError("clientName is required")
189191
}
190192
if len(apiclient.Entitlements) == 0 {
191193
return errorsx.G11NError("entitlements list is required")
192194
}
193195

194-
// Create API client
195196
client := security.NewAPIClient()
196-
resourceURI, err := client.CreateAPIClient(ctx, auth, apiclient)
197+
resourceURI, err := client.CreateAPIClient(ctx, apiclient)
197198
if err != nil {
198199
vc.Logger.Errorf("failed to create API client; err=%v", err)
199200
return err
200201
}
201202

202-
// Directly return the created resource URI
203203
cmdutil.WriteString(cmd, "Resource created: "+resourceURI)
204204
return nil
205205
}

pkg/cmd/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (o *options) Run(cmd *cobra.Command, args []string) error {
153153

154154
case resource.ResourceTypePrefix + "APIClient":
155155
options := &apiClientOptions{}
156-
err = options.createAPIClientFromDataMap(cmd, auth, resourceObject.Data.(map[string]interface{}))
156+
err = options.createAPIClientFromDataMap(cmd, resourceObject.Data.(map[string]interface{}))
157157
}
158158

159159
return err

pkg/cmd/delete/api_client.go

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package delete
33
import (
44
"io"
55

6+
"github.com/ibm-verify/verify-sdk-go/pkg/config/security"
67
"github.com/ibm-verify/verify-sdk-go/pkg/i18n"
78
"github.com/ibm-verify/verifyctl/pkg/config"
8-
"github.com/ibm-verify/verifyctl/pkg/module/security"
99
cmdutil "github.com/ibm-verify/verifyctl/pkg/util/cmd"
1010
"github.com/ibm-verify/verifyctl/pkg/util/templates"
1111
"github.com/spf13/cobra"
1212

13-
contextx "github.com/ibm-verify/verify-sdk-go/pkg/core/context"
1413
errorsx "github.com/ibm-verify/verify-sdk-go/pkg/core/errors"
1514
)
1615

@@ -37,14 +36,13 @@ You can identify the entitlement required by running:
3736
verifyctl delete apiclient --clientName="clientName",
3837
3938
# Delete an API client by ID
40-
verifyctl delete apiclient --clientId="12345"`,
39+
verifyctl delete apiclient --clientID="12345"`,
4140
))
4241
)
4342

4443
type apiclientsOptions struct {
4544
options
46-
id string
47-
45+
id string
4846
config *config.CLIConfig
4947
}
5048

@@ -77,8 +75,7 @@ func NewAPIClientCommand(config *config.CLIConfig, streams io.ReadWriter) *cobra
7775

7876
func (o *apiclientsOptions) AddFlags(cmd *cobra.Command) {
7977
o.addCommonFlags(cmd)
80-
cmd.Flags().StringVar(&o.name, "clientName", o.name, i18n.Translate("clientName to be deleted"))
81-
cmd.Flags().StringVar(&o.id, "clientId", o.id, i18n.Translate("clientId to be deleted"))
78+
cmd.Flags().StringVar(&o.id, "clientID", o.id, i18n.Translate("clientID to be deleted"))
8279
}
8380

8481
func (o *apiclientsOptions) Complete(cmd *cobra.Command, args []string) error {
@@ -91,8 +88,8 @@ func (o *apiclientsOptions) Validate(cmd *cobra.Command, args []string) error {
9188
}
9289

9390
calledAs := cmd.CalledAs()
94-
if calledAs == "apiclient" && o.name == "" && o.id == "" {
95-
return errorsx.G11NError("either 'clientName' or 'clientId' flag is required")
91+
if calledAs == "apiclient" && o.id == "" {
92+
return errorsx.G11NError("'clientId' flag is required")
9693
}
9794
return nil
9895
}
@@ -103,47 +100,33 @@ func (o *apiclientsOptions) Run(cmd *cobra.Command, args []string) error {
103100
return nil
104101
}
105102

106-
auth, err := o.config.SetAuthToContext(cmd.Context())
103+
_, err := o.config.SetAuthToContext(cmd.Context())
107104
if err != nil {
108105
return err
109106
}
110107

111-
// invoke the operation
112-
if cmd.CalledAs() == "apiclient" || len(o.name) > 0 {
113-
// deal with single API client
114-
return o.handleSingleAPIClient(cmd, auth, args)
108+
if cmd.CalledAs() == "apiclient" {
109+
return o.handleSingleAPIClient(cmd, args)
115110
}
116111
return nil
117112
}
118113

119-
func (o *apiclientsOptions) handleSingleAPIClient(cmd *cobra.Command, auth *config.AuthConfig, _ []string) error {
114+
func (o *apiclientsOptions) handleSingleAPIClient(cmd *cobra.Command, _ []string) error {
120115
c := security.NewAPIClient()
121116
var id string
122117
var err error
123118

124119
if o.id != "" {
125-
if o.name != "" {
126-
contextx.GetVerifyContext(cmd.Context()).Logger.Warnf("Both clientName and clientId are provided; using clientId")
127-
}
128120
id = o.id
129-
} else if o.name != "" {
130-
id, err = c.GetAPIClientId(cmd.Context(), auth, o.name)
121+
err = c.DeleteAPIClientById(cmd.Context(), id)
131122
if err != nil {
132123
return err
133124
}
134125
} else {
135126
return errorsx.G11NError("either clientName or clientId must be provided")
136127
}
137128

138-
err = c.DeleteAPIClientById(cmd.Context(), auth, id)
139-
if err != nil {
140-
return err
141-
}
142-
143-
resourceIdentifier := o.name
144-
if o.id != "" {
145-
resourceIdentifier = o.id
146-
}
147-
cmdutil.WriteString(cmd, "Resource deleted: "+resourceIdentifier)
129+
resourceIdentifier := o.id
130+
cmdutil.WriteString(cmd, "Resource deleted with ID: "+resourceIdentifier)
148131
return nil
149132
}

pkg/cmd/get/api_client.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package get
33
import (
44
"io"
55

6+
"github.com/ibm-verify/verify-sdk-go/pkg/config/security"
67
errorsx "github.com/ibm-verify/verify-sdk-go/pkg/core/errors"
78
"github.com/ibm-verify/verify-sdk-go/pkg/i18n"
89
"github.com/ibm-verify/verifyctl/pkg/cmd/resource"
910
"github.com/ibm-verify/verifyctl/pkg/config"
10-
"github.com/ibm-verify/verifyctl/pkg/module/security"
1111
cmdutil "github.com/ibm-verify/verifyctl/pkg/util/cmd"
1212
"github.com/ibm-verify/verifyctl/pkg/util/templates"
1313
"github.com/spf13/cobra"
@@ -33,9 +33,11 @@ You can identify the entitlement required by running:
3333

3434
apiclientsExamples = templates.Examples(cmdutil.TranslateExamples(messagePrefix, `
3535
# Get an apiclient and print the output in yaml
36-
verifyctl get apiclient -o=yaml --name=testApiclient
36+
verifyctl get apiclient -o=yaml --clientName=testApiclient
37+
verifyctl get apiclient -o=yaml --clientID=12345
3738
38-
# Get 10 apiclients based on a given search criteria and sort it in the ascending order by name.
39+
40+
# Get 2 apiclients based on a given search criteria and sort it in the ascending order by name.
3941
verifyctl get apiclients --count=2 --sort=apiclientName -o=yaml`))
4042
)
4143

@@ -76,6 +78,7 @@ func NewAPIClientsCommand(config *config.CLIConfig, streams io.ReadWriter) *cobr
7678
func (o *apiclientsOptions) AddFlags(cmd *cobra.Command) {
7779
o.addCommonFlags(cmd, apiclientResourceName)
7880
cmd.Flags().StringVar(&o.name, "clientName", o.name, i18n.Translate("clientName to get details"))
81+
cmd.Flags().StringVar(&o.id, "clientID", o.id, i18n.Translate("clientID to get details"))
7982
o.addSortFlags(cmd, apiclientResourceName)
8083
o.addCountFlags(cmd, apiclientResourceName)
8184
}
@@ -90,8 +93,11 @@ func (o *apiclientsOptions) Validate(cmd *cobra.Command, args []string) error {
9093
}
9194

9295
calledAs := cmd.CalledAs()
93-
if calledAs == "apiclient" && o.name == "" {
94-
return errorsx.G11NError("'clientName' flag is required.")
96+
if calledAs == "apiclient" && o.name == "" && o.id == "" {
97+
return errorsx.G11NError("either 'clientName' or 'clientID' flag is required.")
98+
}
99+
if o.name != "" && o.id != "" {
100+
return errorsx.G11NError("only one of 'clientName' or 'clientID' can be provided")
95101
}
96102
return nil
97103
}
@@ -102,22 +108,30 @@ func (o *apiclientsOptions) Run(cmd *cobra.Command, args []string) error {
102108
return nil
103109
}
104110

105-
auth, err := o.config.SetAuthToContext(cmd.Context())
111+
_, err := o.config.SetAuthToContext(cmd.Context())
106112
if err != nil {
107113
return err
108114
}
109115

110-
if cmd.CalledAs() == "apiclient" || len(o.name) > 0 {
111-
return o.handleSingleAPIClient(cmd, auth, args)
116+
if cmd.CalledAs() == "apiclient" || len(o.name) > 0 || len(o.id) > 0 {
117+
return o.handleSingleAPIClient(cmd, args)
112118
}
113119

114-
return o.handleAPIClientList(cmd, auth, args)
120+
return o.handleAPIClientList(cmd, args)
115121
}
116122

117-
func (o *apiclientsOptions) handleSingleAPIClient(cmd *cobra.Command, auth *config.AuthConfig, _ []string) error {
123+
func (o *apiclientsOptions) handleSingleAPIClient(cmd *cobra.Command, _ []string) error {
118124

119125
c := security.NewAPIClient()
120-
apic, uri, err := c.GetAPIClient(cmd.Context(), auth, o.name)
126+
var apic *security.APIClientConfig
127+
var uri string
128+
var err error
129+
130+
if o.id != "" {
131+
apic, uri, err = c.GetAPIClientByID(cmd.Context(), o.id)
132+
} else {
133+
apic, uri, err = c.GetAPIClientByName(cmd.Context(), o.name)
134+
}
121135
if err != nil {
122136
return err
123137
}
@@ -147,10 +161,10 @@ func (o *apiclientsOptions) handleSingleAPIClient(cmd *cobra.Command, auth *conf
147161
return nil
148162
}
149163

150-
func (o *apiclientsOptions) handleAPIClientList(cmd *cobra.Command, auth *config.AuthConfig, _ []string) error {
164+
func (o *apiclientsOptions) handleAPIClientList(cmd *cobra.Command, _ []string) error {
151165

152166
c := security.NewAPIClient()
153-
apiclis, uri, err := c.GetAPIClients(cmd.Context(), auth, o.search, o.sort, o.page, o.limit)
167+
apiclis, uri, err := c.GetAPIClients(cmd.Context(), o.search, o.sort, o.page, o.limit)
154168
if err != nil {
155169
return err
156170
}

0 commit comments

Comments
 (0)