Skip to content

Commit 8d7691a

Browse files
committed
implement changes
fix edit sub-cmd Signed-off-by: Rizul Gupta <mail2rizul@gmail.com> implement changes Signed-off-by: Rizul Gupta <mail2rizul@gmail.com> lint fix Signed-off-by: Rizul Gupta <mail2rizul@gmail.com> left align fix Signed-off-by: Rizul Gupta <mail2rizul@gmail.com> fix left alignment Signed-off-by: Rizul Gupta <mail2rizul@gmail.com>
1 parent f0e1586 commit 8d7691a

15 files changed

Lines changed: 239 additions & 148 deletions

cmd/harbor/root/webhook/create.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@
1414
package webhook
1515

1616
import (
17+
"fmt"
18+
1719
"github.com/goharbor/harbor-cli/pkg/api"
1820
"github.com/goharbor/harbor-cli/pkg/prompt"
1921
"github.com/goharbor/harbor-cli/pkg/views/webhook/create"
20-
log "github.com/sirupsen/logrus"
2122
"github.com/spf13/cobra"
2223
)
2324

2425
func CreateWebhookCmd() *cobra.Command {
2526
var opts create.CreateView
2627

2728
cmd := &cobra.Command{
28-
Use: "create",
29+
Use: "create [name]",
2930
Short: "Create a new webhook for a Harbor project",
3031
Long: `This command creates a new webhook policy for a specified Harbor project.
3132
32-
You can either provide all required flags (project name, name, notify type, endpoint, etc.) directly to create the webhook non-interactively,
33-
or leave them out and be guided through an interactive prompt to input each field.`,
33+
You can either provide all required flags (project name, notify type, endpoint, etc.) directly to create the webhook non-interactively,
34+
or leave them out and be guided through an interactive prompt to input each field. The webhook name is required as an argument.`,
3435
Example: ` # Create a webhook using flags
35-
harbor-cli webhook create \
36+
harbor-cli webhook create my-webhook \
3637
--project my-project \
37-
--name my-webhook \
3838
--notify-type http \
3939
--event-type PUSH_ARTIFACT,DELETE_ARTIFACT \
4040
--endpoint-url https://example.com/webhook \
@@ -43,10 +43,14 @@ or leave them out and be guided through an interactive prompt to input each fiel
4343
--auth-header "Bearer mytoken"
4444
4545
# Create a webhook using the interactive prompt
46-
harbor-cli webhook create`,
47-
Args: cobra.NoArgs,
48-
Run: func(cmd *cobra.Command, args []string) {
46+
harbor-cli webhook create my-webhook`,
47+
Args: cobra.MaximumNArgs(1),
48+
RunE: func(cmd *cobra.Command, args []string) error {
4949
var err error
50+
if len(args) > 0 {
51+
opts.Name = args[0]
52+
}
53+
5054
createView := &create.CreateView{
5155
ProjectName: opts.ProjectName,
5256
Name: opts.Name,
@@ -69,14 +73,15 @@ or leave them out and be guided through an interactive prompt to input each fiel
6973
err = createWebhookView(createView)
7074
}
7175
if err != nil {
72-
log.Errorf("failed to create webhook: %v", err)
76+
return fmt.Errorf("failed to create webhook: %v", err)
7377
}
78+
return nil
7479
},
7580
}
81+
7682
flags := cmd.Flags()
7783

7884
flags.StringVarP(&opts.ProjectName, "project", "", "", "Project Name")
79-
flags.StringVarP(&opts.Name, "name", "", "", "Webhook Name")
8085
flags.StringVarP(&opts.Description, "description", "", "", "Webhook Description")
8186
flags.StringVarP(&opts.NotifyType, "notify-type", "", "", "Notify Type (http, slack)")
8287
flags.StringArrayVarP(&opts.EventType, "event-type", "", []string{}, "Event Types (comma separated)")
@@ -89,7 +94,9 @@ or leave them out and be guided through an interactive prompt to input each fiel
8994
}
9095

9196
func createWebhookView(view *create.CreateView) error {
92-
view.ProjectName = prompt.GetProjectNameFromUser()
97+
if view.ProjectName == "" {
98+
view.ProjectName = prompt.GetProjectNameFromUser()
99+
}
93100
err := create.WebhookCreateView(view)
94101
if err != nil {
95102
return err

cmd/harbor/root/webhook/delete.go

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,82 @@
1414
package webhook
1515

1616
import (
17-
"strconv"
17+
"fmt"
1818

19-
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
2019
"github.com/goharbor/harbor-cli/pkg/api"
2120
"github.com/goharbor/harbor-cli/pkg/prompt"
22-
log "github.com/sirupsen/logrus"
2321
"github.com/spf13/cobra"
2422
)
2523

2624
func DeleteWebhookCmd() *cobra.Command {
27-
var projectName string
28-
var webhookId string
29-
var webhookIdInt int64
30-
var selectedWebhook models.WebhookPolicy
25+
var (
26+
projectName string
27+
webhookId int64 = -1
28+
)
3129

3230
cmd := &cobra.Command{
33-
Use: "delete",
31+
Use: "delete [webhook-name]",
3432
Short: "Delete a webhook from a Harbor project",
35-
Long: `This command deletes a webhook from the specified Harbor project.
36-
You can either specify the project name and webhook ID directly using flags,
33+
Long: `Delete a webhook from a specified Harbor project.
34+
You can either specify the project name and webhook ID using flags,
35+
pass the webhook name as an argument,
3736
or interactively select a project and webhook if not provided.`,
38-
Example: ` # Delete a webhook by specifying the project and webhook ID
37+
Example: ` # Delete by project and webhook ID
3938
harbor-cli webhook delete --project my-project --webhook 5
4039
41-
# Delete a webhook by selecting the project and webhook interactively
40+
# Delete by project and webhook name
41+
harbor-cli webhook delete my-webhook --project my-project
42+
43+
# Fully interactive deletion
4244
harbor-cli webhook delete`,
43-
Args: cobra.NoArgs,
44-
Run: func(cmd *cobra.Command, args []string) {
45+
Args: cobra.MaximumNArgs(1),
46+
RunE: func(cmd *cobra.Command, args []string) error {
4547
var err error
48+
var webhookName string
49+
50+
if len(args) > 0 {
51+
webhookName = args[0]
52+
}
53+
54+
if webhookId != -1 && webhookName != "" {
55+
return fmt.Errorf("webhook ID and name cannot be provided together")
56+
}
57+
58+
if (webhookId != -1 || webhookName != "") && projectName == "" {
59+
return fmt.Errorf("project name must be provided when specifying webhook ID or webhook name")
60+
}
4661

47-
if projectName != "" && webhookId != "" {
48-
webhookIdInt, err = strconv.ParseInt(webhookId, 10, 64)
62+
if projectName == "" {
63+
projectName = prompt.GetProjectNameFromUser()
64+
}
65+
66+
if webhookName != "" {
67+
webhookId, err = api.GetWebhookID(projectName, webhookName)
4968
if err != nil {
50-
log.Errorf("failed to parse webhook id: %v", err)
51-
return
69+
return fmt.Errorf("failed to get webhook ID for '%s': %w", webhookName, err)
5270
}
53-
} else {
54-
projectName = prompt.GetProjectNameFromUser()
55-
selectedWebhook, err = prompt.GetWebhookFromUser(projectName)
71+
}
72+
73+
if webhookId == -1 {
74+
selectedWebhook, err := prompt.GetWebhookFromUser(projectName)
5675
if err != nil {
57-
return
76+
return fmt.Errorf("failed to select webhook: %w", err)
5877
}
59-
webhookIdInt = selectedWebhook.ID
78+
webhookId = selectedWebhook.ID
6079
}
61-
err = api.DeleteWebhook(projectName, webhookIdInt)
62-
if err != nil {
63-
log.Errorf("failed to delete webhook: %v", err)
80+
81+
if err := api.DeleteWebhook(projectName, webhookId); err != nil {
82+
return fmt.Errorf("failed to delete webhook: %w", err)
6483
}
84+
85+
fmt.Printf("✅ Webhook deleted successfully from project '%s'\n", projectName)
86+
return nil
6587
},
6688
}
6789

6890
flags := cmd.Flags()
69-
flags.StringVarP(&projectName, "project", "", "", "Project Name")
70-
flags.StringVarP(&webhookId, "webhook", "", "", "Webhook ID")
91+
flags.StringVarP(&projectName, "project", "", "", "Project name (required when providing webhook ID or name)")
92+
flags.Int64VarP(&webhookId, "webhook", "", -1, "Webhook ID (alternative to providing webhook name as argument)")
7193

7294
return cmd
7395
}

cmd/harbor/root/webhook/edit.go

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,27 @@
1414
package webhook
1515

1616
import (
17-
"strconv"
17+
"fmt"
1818

1919
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
2020
"github.com/goharbor/harbor-cli/pkg/api"
2121
"github.com/goharbor/harbor-cli/pkg/prompt"
2222
"github.com/goharbor/harbor-cli/pkg/views/webhook/edit"
23-
log "github.com/sirupsen/logrus"
2423
"github.com/spf13/cobra"
2524
)
2625

2726
func EditWebhookCmd() *cobra.Command {
2827
var opts edit.EditView
29-
var webhookId string
30-
var webhookIdInt int64
3128
cmd := &cobra.Command{
32-
Use: "edit",
29+
Use: "edit [WEBHOOK_NAME]",
3330
Short: "Edit an existing webhook for a Harbor project",
3431
Long: `This command allows you to update an existing webhook policy in a Harbor project.
3532
3633
You can either pass all the necessary flags (webhook ID, project name, etc.) to perform a non-interactive update,
3734
or leave them out and use the interactive prompt to select and update a webhook.`,
3835
Example: ` # Edit a webhook by providing all fields directly
39-
harbor-cli webhook edit \
36+
harbor-cli webhook edit my-webhook \
4037
--project my-project \
41-
--webhook-id 5 \
42-
--name updated-webhook \
4338
--notify-type http \
4439
--event-type PUSH_ARTIFACT \
4540
--endpoint-url https://new-url.com \
@@ -50,9 +45,27 @@ or leave them out and use the interactive prompt to select and update a webhook.
5045
5146
# Edit a webhook using the interactive prompt
5247
harbor-cli webhook edit`,
53-
Args: cobra.NoArgs,
54-
Run: func(cmd *cobra.Command, args []string) {
48+
Args: cobra.MaximumNArgs(1),
49+
RunE: func(cmd *cobra.Command, args []string) error {
5550
var err error
51+
52+
if len(args) > 0 {
53+
opts.Name = args[0]
54+
}
55+
56+
if opts.WebhookId != -1 && opts.Name != "" {
57+
return fmt.Errorf("webhook ID and name cannot be provided together")
58+
}
59+
if opts.ProjectName == "" && (opts.Name != "" || opts.WebhookId != -1) {
60+
return fmt.Errorf("project name is required when webhook name is provided")
61+
}
62+
if opts.ProjectName != "" && opts.Name != "" {
63+
opts.WebhookId, err = api.GetWebhookID(opts.ProjectName, opts.Name)
64+
if err != nil {
65+
return fmt.Errorf("failed to get webhook ID: %v", err)
66+
}
67+
}
68+
5669
editView := &edit.EditView{
5770
WebhookId: opts.WebhookId,
5871
ProjectName: opts.ProjectName,
@@ -65,33 +78,26 @@ or leave them out and use the interactive prompt to select and update a webhook.
6578
AuthHeader: opts.AuthHeader,
6679
VerifyRemoteCertificate: opts.VerifyRemoteCertificate,
6780
}
81+
6882
if opts.ProjectName != "" &&
69-
webhookId != "" &&
70-
opts.Name != "" &&
83+
opts.WebhookId != -1 &&
7184
opts.NotifyType != "" &&
7285
len(opts.EventType) != 0 &&
7386
opts.EndpointURL != "" {
74-
webhookIdInt, err = strconv.ParseInt(webhookId, 10, 64)
75-
if err != nil {
76-
log.Errorf("failed to parse webhook id: %v", err)
77-
return
78-
}
79-
opts.WebhookId = webhookIdInt
8087
err = api.UpdateWebhook(&opts)
8188
} else {
8289
err = editWebhookView(editView)
8390
}
84-
8591
if err != nil {
86-
log.Errorf("failed to edit webhook: %v", err)
92+
return fmt.Errorf("failed to edit webhook: %v", err)
8793
}
94+
return nil
8895
},
8996
}
90-
flags := cmd.Flags()
9197

98+
flags := cmd.Flags()
9299
flags.StringVarP(&opts.ProjectName, "project", "", "", "Project Name")
93-
flags.StringVarP(&webhookId, "webhook-id", "", "", "Webhook ID")
94-
flags.StringVarP(&opts.Name, "name", "", "", "Webhook Name")
100+
flags.Int64VarP(&opts.WebhookId, "webhook-id", "", -1, "Webhook ID")
95101
flags.StringVarP(&opts.Description, "description", "", "", "Webhook Description")
96102
flags.StringVarP(&opts.NotifyType, "notify-type", "", "", "Notify Type (http, slack)")
97103
flags.StringArrayVarP(&opts.EventType, "event-type", "", []string{}, "Event Types (comma separated)")
@@ -107,23 +113,32 @@ or leave them out and use the interactive prompt to select and update a webhook.
107113
func editWebhookView(view *edit.EditView) error {
108114
var selectedWebhook models.WebhookPolicy
109115
var err error
110-
view.ProjectName = prompt.GetProjectNameFromUser()
111-
selectedWebhook, err = prompt.GetWebhookFromUser(view.ProjectName)
112-
if err != nil {
113-
return err
116+
if view.ProjectName == "" {
117+
view.ProjectName = prompt.GetProjectNameFromUser()
118+
}
119+
if view.WebhookId == -1 {
120+
selectedWebhook, err = prompt.GetWebhookFromUser(view.ProjectName)
121+
if err != nil {
122+
return err
123+
}
124+
} else {
125+
selectedWebhook, err = api.GetWebhook(view.ProjectName, view.WebhookId)
126+
if err != nil {
127+
return err
128+
}
114129
}
115130
view.WebhookId = selectedWebhook.ID
116131
view.Description = selectedWebhook.Description
117132
view.Enabled = selectedWebhook.Enabled
118133
view.EventType = selectedWebhook.EventTypes
119134
view.Name = selectedWebhook.Name
120-
121-
view.EndpointURL = selectedWebhook.Targets[0].Address
122-
view.AuthHeader = selectedWebhook.Targets[0].AuthHeader
123-
view.PayloadFormat = string(selectedWebhook.Targets[0].PayloadFormat)
124-
view.VerifyRemoteCertificate = !selectedWebhook.Targets[0].SkipCertVerify
125-
view.NotifyType = selectedWebhook.Targets[0].Type
126-
135+
if len(selectedWebhook.Targets) > 0 {
136+
view.EndpointURL = selectedWebhook.Targets[0].Address
137+
view.AuthHeader = selectedWebhook.Targets[0].AuthHeader
138+
view.PayloadFormat = string(selectedWebhook.Targets[0].PayloadFormat)
139+
view.VerifyRemoteCertificate = !selectedWebhook.Targets[0].SkipCertVerify
140+
view.NotifyType = selectedWebhook.Targets[0].Type
141+
}
127142
edit.WebhookEditView(view)
128143
return api.UpdateWebhook(view)
129144
}

cmd/harbor/root/webhook/list.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package webhook
1515

1616
import (
17+
"fmt"
18+
1719
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/webhook"
1820
"github.com/goharbor/harbor-cli/pkg/api"
1921
"github.com/goharbor/harbor-cli/pkg/prompt"
@@ -41,7 +43,7 @@ Use the '--output-format' flag for raw JSON output.`,
4143
# Output in JSON format
4244
harbor-cli webhook list my-project --output-format=json`,
4345
Args: cobra.MaximumNArgs(1),
44-
Run: func(cmd *cobra.Command, args []string) {
46+
RunE: func(cmd *cobra.Command, args []string) error {
4547
var err error
4648
var resp webhook.ListWebhookPoliciesOfProjectOK
4749
var projectName string
@@ -54,22 +56,21 @@ Use the '--output-format' flag for raw JSON output.`,
5456

5557
resp, err = api.ListWebhooks(projectName)
5658
if err != nil {
57-
log.Errorf("failed to list webhooks: %v", err)
58-
return
59+
return fmt.Errorf("failed to list webhooks: %v", err)
5960
}
6061
if len(resp.Payload) == 0 {
6162
log.Infof("No webhooks found in project %s", projectName)
62-
return
63+
return nil
6364
}
6465
FormatFlag := viper.GetString("output-format")
6566
if FormatFlag != "" {
6667
err = utils.PrintFormat(resp, FormatFlag)
6768
if err != nil {
68-
log.Fatalf("failed to print in %s format: %v", FormatFlag, err)
69+
return fmt.Errorf("failed to print in %s format: %v", FormatFlag, err)
6970
}
70-
return
7171
}
7272
webhookViews.ListWebhooks(resp.Payload)
73+
return nil
7374
},
7475
}
7576
return cmd

0 commit comments

Comments
 (0)