Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/harbor/root/repository/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Repository() *cobra.Command {
RepoViewCmd(),
RepoDeleteCmd(),
SearchRepoCmd(),
UpdateRepositoryCommand(),
)

return cmd
Expand Down
75 changes: 75 additions & 0 deletions cmd/harbor/root/repository/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,78 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package repository

import (
"fmt"

"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/repository/update"
"github.com/spf13/cobra"
)

func UpdateRepositoryCommand() *cobra.Command {
var description string

cmd := &cobra.Command{
Use: "update",
Short: "Update a repository",
Long: `Update the description of a repository.

This command updates the description associated with a repository
within a Harbor project.

Examples:
# Update repository description using project/repository format
harbor repository update library/nginx --description "Official nginx image"`,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
var projectName string
var repoName string

if len(args) > 0 {
projectName, repoName, err = utils.ParseProjectRepo(args[0])
if err != nil {
return fmt.Errorf("failed to parse project/repo: %w", err)
}
} else {
projectName, err = prompt.GetProjectNameFromUser()
if err != nil {
return fmt.Errorf("failed to get project name: %w", err)
}
repoName = prompt.GetRepoNameFromUser(projectName)
}

existingRepo, err := api.RepoView(projectName, repoName)
if err != nil {
return fmt.Errorf("failed to get existing repository: %w", err)
}

if cmd.Flags().Changed("description") {
err = api.UpdateRepository(projectName, repoName, description, existingRepo.Payload)
if err != nil {
return fmt.Errorf("failed to update repository: %w", err)
}
} else {
updatedDescription, err := update.UpdateRepositoryView(existingRepo.Payload)
if err != nil {
return fmt.Errorf("update cancelled or failed: %w", err)
}

err = api.UpdateRepository(projectName, repoName, updatedDescription, existingRepo.Payload)
if err != nil {
return fmt.Errorf("failed to update repository: %w", err)
}
}

fmt.Printf("Repository %s/%s updated successfully\n", projectName, repoName)
return nil
},
}

flags := cmd.Flags()
flags.StringVarP(&description, "description", "d", "", "Repository description")

return cmd
}
44 changes: 44 additions & 0 deletions doc/cli-docs/harbor-repo-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: harbor repo update
weight: 55
---
## harbor repo update

### Description

##### Update a repository

### Synopsis

Update the description of a repository.

This command updates the description associated with a repository
within a Harbor project.

Examples:
# Update repository description using project/repository format
harbor repository update library/nginx --description "Official nginx image"

```sh
harbor repo update [flags]
```

### Options

```sh
-d, --description string Repository description
-h, --help help for update
```

### Options inherited from parent commands

```sh
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
-o, --output-format string Output format. One of: json|yaml
-v, --verbose verbose output
```

### SEE ALSO

* [harbor repo](harbor-repo.md) - Manage repositories

1 change: 1 addition & 0 deletions doc/cli-docs/harbor-repo.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ Manage repositories in Harbor context
* [harbor repo delete](harbor-repo-delete.md) - Delete a repository
* [harbor repo list](harbor-repo-list.md) - list repositories within a project
* [harbor repo search](harbor-repo-search.md) - search repository based on their names
* [harbor repo update](harbor-repo-update.md) - Update a repository
* [harbor repo view](harbor-repo-view.md) - Get repository information

48 changes: 48 additions & 0 deletions doc/man-docs/man1/harbor-repo-update.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
.nh
.TH "HARBOR" "1" "Harbor Community" "Harbor User Manuals"

.SH NAME
harbor-repo-update - Update a repository


.SH SYNOPSIS
\fBharbor repo update [flags]\fP


.SH DESCRIPTION
Update the description of a repository.

.PP
This command updates the description associated with a repository
within a Harbor project.

.PP
Examples:
# Update repository description using project/repository format
harbor repository update library/nginx --description "Official nginx image"


.SH OPTIONS
\fB-d\fP, \fB--description\fP=""
Repository description

.PP
\fB-h\fP, \fB--help\fP[=false]
help for update


.SH OPTIONS INHERITED FROM PARENT COMMANDS
\fB-c\fP, \fB--config\fP=""
config file (default is $HOME/.config/harbor-cli/config.yaml)

.PP
\fB-o\fP, \fB--output-format\fP=""
Output format. One of: json|yaml

.PP
\fB-v\fP, \fB--verbose\fP[=false]
verbose output


.SH SEE ALSO
\fBharbor-repo(1)\fP
2 changes: 1 addition & 1 deletion doc/man-docs/man1/harbor-repo.1
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ Manage repositories in Harbor context


.SH SEE ALSO
\fBharbor(1)\fP, \fBharbor-repo-delete(1)\fP, \fBharbor-repo-list(1)\fP, \fBharbor-repo-search(1)\fP, \fBharbor-repo-view(1)\fP
\fBharbor(1)\fP, \fBharbor-repo-delete(1)\fP, \fBharbor-repo-list(1)\fP, \fBharbor-repo-search(1)\fP, \fBharbor-repo-update(1)\fP, \fBharbor-repo-view(1)\fP
30 changes: 30 additions & 0 deletions pkg/api/repository_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package api
import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/repository"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/search"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -107,3 +108,32 @@ func SearchRepository(query string) (search.SearchOK, error) {

return *response, nil
}

func UpdateRepository(projectName, repoName string, description string, existingRepo *models.Repository) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return err
}

repo := &models.Repository{
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you are creating a new blank repository object without fetching the info of the already existing repo from the api. I would expect that this could lead to some issues. I think the correct behavior would be something like getting the repo object from the api, updating the description and then calling the update request to the api.

ID: existingRepo.ID,
Name: existingRepo.Name,
ProjectID: existingRepo.ProjectID,
Description: description,
ArtifactCount: existingRepo.ArtifactCount,
PullCount: existingRepo.PullCount,
CreationTime: existingRepo.CreationTime,
UpdateTime: existingRepo.UpdateTime,
}

_, err = client.Repository.UpdateRepository(ctx, &repository.UpdateRepositoryParams{
ProjectName: projectName,
RepositoryName: repoName,
Repository: repo,
})
if err != nil {
return err
}

return nil
}
47 changes: 47 additions & 0 deletions pkg/views/repository/update/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package update

import (
"github.com/charmbracelet/huh"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
)

func UpdateRepositoryView(repo *models.Repository) (string, error) {
var description string

if repo.Description != "" {
description = repo.Description
}

theme := huh.ThemeCharm()

form := huh.NewForm(
huh.NewGroup(
huh.NewText().
Title("Repository Description").
Description("Update the description for " + repo.Name).
Value(&description).
Placeholder("Enter repository description..."),
),
).WithTheme(theme)

err := form.Run()
if err != nil {
return "", err
}

return description, nil
}
Loading