-
Notifications
You must be signed in to change notification settings - Fork 190
feat: add project config command
#448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 c20268f
Added the option for whether the argument is project ID or not.
muaz-32 1e023ce
Add `delete` command for metadata.
muaz-32 0e09f31
Add `view` command for metadata.
muaz-32 85b9322
Merge branch 'goharbor:main' into add-project-metadata
muaz-32 720e3a0
Updated the structure according to newer changes and added the `updat…
muaz-32 9421f12
Refactored the code. Remove duplicate code by bringing the declaratio…
muaz-32 a2340c6
Updated the client connecting code.
muaz-32 611a775
Refactored the code.
muaz-32 3eb59d7
Improved the metadata presentation format.
muaz-32 66f8ff9
Merge branch 'main' into project-config
rizul2108 f4619a1
first commit for intial rename
rizul2108 bd1e51c
lint fixes
rizul2108 a529d45
lint fixes
rizul2108 7756d57
remove delete config cmd
rizul2108 1c7f72d
improve list config cmd
rizul2108 81063bf
improve list config cmd
rizul2108 f1c49ae
add persistent flag
rizul2108 1f5f102
modify add cmd
rizul2108 085aa92
Merge branch 'main' of github.com:rizul2108/harbor-cli into project-c…
rizul2108 c14c9f7
merge main
rizul2108 68ee60a
lint fix
rizul2108 5cbb4de
add update command
rizul2108 62cbfda
update error handling
rizul2108 09d1b92
add flags in update
rizul2108 6a5ac61
Update cmd/harbor/root/project/config/update.go
rizul2108 5968176
Merge branch 'goharbor:main' into project-config
rizul2108 8f8dd8b
fix errors
rizul2108 f9bac3d
resolve errors
rizul2108 01c26e7
update desc
rizul2108 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
|
|
||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.