Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
299b728
Added the metadata command inside project command.
muaz-32 May 28, 2024
c20268f
Added the option for whether the argument is project ID or not.
muaz-32 May 30, 2024
1e023ce
Add `delete` command for metadata.
muaz-32 May 30, 2024
0e09f31
Add `view` command for metadata.
muaz-32 May 30, 2024
85b9322
Merge branch 'goharbor:main' into add-project-metadata
muaz-32 May 31, 2024
720e3a0
Updated the structure according to newer changes and added the `updat…
muaz-32 May 31, 2024
9421f12
Refactored the code. Remove duplicate code by bringing the declaratio…
muaz-32 May 31, 2024
a2340c6
Updated the client connecting code.
muaz-32 Jun 2, 2024
611a775
Refactored the code.
muaz-32 Jun 3, 2024
3eb59d7
Improved the metadata presentation format.
muaz-32 Jun 7, 2024
66f8ff9
Merge branch 'main' into project-config
rizul2108 Apr 29, 2025
f4619a1
first commit for intial rename
rizul2108 Apr 30, 2025
bd1e51c
lint fixes
rizul2108 Apr 30, 2025
a529d45
lint fixes
rizul2108 Apr 30, 2025
7756d57
remove delete config cmd
rizul2108 Apr 30, 2025
1c7f72d
improve list config cmd
rizul2108 May 1, 2025
81063bf
improve list config cmd
rizul2108 May 1, 2025
f1c49ae
add persistent flag
rizul2108 May 3, 2025
1f5f102
modify add cmd
rizul2108 May 3, 2025
085aa92
Merge branch 'main' of github.com:rizul2108/harbor-cli into project-c…
rizul2108 May 14, 2025
c14c9f7
merge main
rizul2108 May 14, 2025
68ee60a
lint fix
rizul2108 May 14, 2025
5cbb4de
add update command
rizul2108 May 15, 2025
62cbfda
update error handling
rizul2108 May 15, 2025
09d1b92
add flags in update
rizul2108 May 15, 2025
6a5ac61
Update cmd/harbor/root/project/config/update.go
rizul2108 May 15, 2025
5968176
Merge branch 'goharbor:main' into project-config
rizul2108 May 15, 2025
8f8dd8b
fix errors
rizul2108 May 15, 2025
f9bac3d
resolve errors
rizul2108 May 19, 2025
01c26e7
update desc
rizul2108 May 19, 2025
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
2 changes: 2 additions & 0 deletions cmd/harbor/root/project/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package project

import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/project/config"
"github.com/spf13/cobra"
)

Expand All @@ -30,6 +31,7 @@ func Project() *cobra.Command {
ListProjectCommand(),
ViewCommand(),
LogsProjectCommmand(),
config.ProjectConfigCommand(),
SearchProjectCommand(),
)

Expand Down
32 changes: 32 additions & 0 deletions cmd/harbor/root/project/config/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 config

import "github.com/spf13/cobra"

var isID bool

func ProjectConfigCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Manage project configuration",
}
cmd.AddCommand(
UpdateProjectConfigCmd(),
ListProjectConfigCmd(),
)
cmd.PersistentFlags().BoolVarP(&isID, "id", "", false, "Use project ID instead of name")
Comment thread
bupd marked this conversation as resolved.

return cmd
}
81 changes: 81 additions & 0 deletions cmd/harbor/root/project/config/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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 config

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/project/config/list"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func ListProjectConfigCmd() *cobra.Command {
var err error
var projectNameorID string
cmd := &cobra.Command{
Use: "list [project_name]",
Short: "List configuration of a Harbor project by name or ID",
Long: `Display the configuration metadata of a Harbor project specified by its name or ID.

If no project name or ID is provided as an argument, you will be prompted to select a project interactively.

You can use the global flag '--output-format' to specify the output format, e.g. 'json' or 'yaml', for machine-readable output.

Examples:

# List configuration of project 'myproject' by name
harbor-cli project config list myproject

# List configuration of project with ID '123'
harbor-cli project config list 123

# Run interactively (prompt to select project)
harbor-cli project config list

# List config in JSON format
harbor-cli project config list myproject --output-format json
`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
projectNameorID, err = prompt.GetProjectNameFromUser()
if err != nil {
return fmt.Errorf("failed to get project name: %v", err)
}
isID = false
} else {
projectNameorID = args[0]
}
response, err := api.ListConfig(isID, projectNameorID)
if err != nil {
return fmt.Errorf("failed to list metadata: %v", utils.ParseHarborErrorMsg(err))
}
formatFlag := viper.GetString("output-format")
if formatFlag != "" {
err = utils.PrintFormat(response.Payload, formatFlag)
if err != nil {
return err
}
} else {
list.ListConfig(response.Payload)
}
return nil
},
}
return cmd
}
172 changes: 172 additions & 0 deletions cmd/harbor/root/project/config/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// 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 config

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/project/config/update"
"github.com/spf13/cobra"
)

var (
publicFlag string
autoScanFlag string
preventVulFlag string
reuseSysCVEFlag string
enableContentTrustFlag string
enableContentTrustCosignFlag string
severityFlag string
)

func UpdateProjectConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update [project_name]",
Short: "Interactively or via flags update project configuration in Harbor",
Long: `Update the configuration settings of a Harbor project either interactively or directly using command-line flags.

You can specify the project by its name or ID as an argument. If not provided, you will be prompted to select a project interactively.

Examples:

# Update project 'myproject' visibility to public
harbor-cli project config update myproject --public true

# Update multiple settings in one command
harbor-cli project config update myproject --public false --prevent-vul true --severity high

# Run interactively without flags
harbor-cli project config update

Supported flag values:

- Boolean flags (public, auto-scan, prevent-vul, reuse-sys-cve-allowlist, enable-content-trust, enable-content-trust-cosign): "true" or "false"
- Severity: one of "low", "medium", "high", "critical"
`,

Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
var projectIDOrName string
if len(args) > 0 {
projectIDOrName = args[0]
} else {
projectIDOrName, err = prompt.GetProjectNameFromUser()
if err != nil {
return fmt.Errorf("Failed to get project name: %v", err)
}
isID = false
}
resp, err := api.GetProject(projectIDOrName, isID)
if err != nil {
return fmt.Errorf("Failed to list project config: %v", utils.ParseHarborErrorMsg(err))
}
conf := resp.Payload.Metadata
flags := cmd.Flags()
flagsUsed := false

if flags.Changed("public") {
if err := validateFlag("public", publicFlag); err != nil {
return err
}
conf.Public = publicFlag
flagsUsed = true
}
if flags.Changed("auto-scan") {
if err := validateFlag("auto-scan", autoScanFlag); err != nil {
return err
}
conf.AutoScan = &autoScanFlag
flagsUsed = true
}
if flags.Changed("prevent-vul") {
if err := validateFlag("prevent-vul", preventVulFlag); err != nil {
return err
}
conf.PreventVul = &preventVulFlag
flagsUsed = true
}
if flags.Changed("reuse-sys-cve") {
if err := validateFlag("reuse-sys-cve", reuseSysCVEFlag); err != nil {
return err
}
conf.ReuseSysCVEAllowlist = &reuseSysCVEFlag
flagsUsed = true
}
if flags.Changed("enable-content-trust") {
if err := validateFlag("enable-content-trust", enableContentTrustFlag); err != nil {
return err
}
conf.EnableContentTrust = &enableContentTrustFlag
flagsUsed = true
}
if flags.Changed("enable-content-trust-cosign") {
if err := validateFlag("enable-content-trust-cosign", enableContentTrustCosignFlag); err != nil {
return err
}
conf.EnableContentTrustCosign = &enableContentTrustCosignFlag
flagsUsed = true
}
if flags.Changed("severity") {
if err := validateFlag("severity", severityFlag); err != nil {
return err
}
conf.Severity = &severityFlag
flagsUsed = true
}
if !flagsUsed {
update.UpdateProjectMetadataView(conf)
}

err = api.UpdateConfig(isID, projectIDOrName, *conf)
if err != nil {
return fmt.Errorf("Failed to update project config: %v", utils.ParseHarborErrorMsg(err))
}
fmt.Printf("Project %s configuration updated successfully.\n", projectIDOrName)
return nil
},
}

flags := cmd.Flags()

flags.StringVar(&publicFlag, "public", "", "Set project visibility (true/false)")
flags.StringVar(&autoScanFlag, "auto-scan", "", "Enable or disable auto scan (true/false)")
flags.StringVar(&preventVulFlag, "prevent-vul", "", "Enable or disable vulnerability prevention (true/false)")
flags.StringVar(&reuseSysCVEFlag, "reuse-sys-cve", "", "Enable or disable reuse of system CVE allowlist (true/false)")
flags.StringVar(&enableContentTrustFlag, "enable-content-trust", "", "Enable or disable content trust (true/false)")
flags.StringVar(&enableContentTrustCosignFlag, "enable-content-trust-cosign", "", "Enable or disable content trust cosign (true/false)")
flags.StringVar(&severityFlag, "severity", "", "Set severity level")

return cmd
}

func validateFlag(flagName, flagValue string) error {
allowed := map[string]bool{
"low": true,
"medium": true,
"high": true,
"critical": true,
}
if flagName == "severity" && !allowed[flagValue] {
return fmt.Errorf("Invalid value for --%s: %s. Allowed values are: low, medium, high, critical", flagName, flagValue)
}
if flagName != "severity" && flagValue != "true" && flagValue != "false" {
return fmt.Errorf("Invalid value for --%s: %s. Expected 'true' or 'false'", flagName, flagValue)
}

return nil
}
56 changes: 56 additions & 0 deletions doc/cli-docs/harbor-project-config-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: harbor project config list
weight: 85
---
## harbor project config list

### Description

##### List configuration of a Harbor project by name or ID

### Synopsis

Display the configuration metadata of a Harbor project specified by its name or ID.

If no project name or ID is provided as an argument, you will be prompted to select a project interactively.

You can use the global flag '--output-format' to specify the output format, e.g. 'json' or 'yaml', for machine-readable output.

Examples:

# List configuration of project 'myproject' by name
harbor-cli project config list myproject

# List configuration of project with ID '123'
harbor-cli project config list 123

# Run interactively (prompt to select project)
harbor-cli project config list

# List config in JSON format
harbor-cli project config list myproject --output-format json


```sh
harbor project config list [project_name] [flags]
```

### Options

```sh
-h, --help help for list
```

### Options inherited from parent commands

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

### SEE ALSO

* [harbor project config](harbor-project-config.md) - Manage project configuration

Loading