|
| 1 | +// Copyright Project Harbor Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package configurations |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "strings" |
| 19 | + |
| 20 | + "github.com/goharbor/harbor-cli/pkg/api" |
| 21 | + "github.com/goharbor/harbor-cli/pkg/utils" |
| 22 | + "github.com/goharbor/harbor-cli/pkg/views/configurations/view" |
| 23 | + "github.com/spf13/cobra" |
| 24 | + "github.com/spf13/viper" |
| 25 | +) |
| 26 | + |
| 27 | +func ViewConfigCmd() *cobra.Command { |
| 28 | + var category string |
| 29 | + cmd := &cobra.Command{ |
| 30 | + Use: "view", |
| 31 | + Short: "View Harbor configurations", |
| 32 | + Long: `View Harbor system configurations. You can filter by category using full names or shorthand: |
| 33 | +
|
| 34 | +Categories: |
| 35 | +- authentication (auth): User and service authentication settings (LDAP, OIDC, UAA) |
| 36 | +- security (sec): Security policies and certificate settings |
| 37 | +- system (sys): General system behavior and storage settings |
| 38 | +
|
| 39 | +Examples: |
| 40 | + harbor config view # View all configurations |
| 41 | + harbor config view --category auth # View authentication configs |
| 42 | + harbor config view --cat sec # View security configs (shorthand) |
| 43 | + harbor config view --cat sys # View system configs |
| 44 | +
|
| 45 | + # Export configurations to files |
| 46 | + harbor config view -o json > config.json # Save all configs as JSON |
| 47 | + harbor config view --cat auth -o yaml | tee auth-config.yaml # Save auth configs as YAML and display |
| 48 | + harbor config view --cat sec -o json > security-config.json # Save security configs as JSON`, |
| 49 | + Args: cobra.NoArgs, |
| 50 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | + // Expand shorthand category names |
| 52 | + expandedCategory := expandCategoryShorthand(category) |
| 53 | + |
| 54 | + // Validate category if provided |
| 55 | + if expandedCategory != "" { |
| 56 | + validCategories := []string{"authentication", "security", "system"} |
| 57 | + isValid := false |
| 58 | + for _, valid := range validCategories { |
| 59 | + if expandedCategory == valid { |
| 60 | + isValid = true |
| 61 | + break |
| 62 | + } |
| 63 | + } |
| 64 | + if !isValid { |
| 65 | + return fmt.Errorf("invalid category '%s'. Valid options: authentication (auth), security (sec), system (sys)", category) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + response, err := api.GetConfigurations() |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + FormatFlag := viper.GetString("output-format") |
| 75 | + if FormatFlag != "" { |
| 76 | + configurations := utils.ExtractConfigurationsByCategory(response.Payload, expandedCategory) |
| 77 | + err = utils.PrintFormat(configurations, FormatFlag) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + } else { |
| 82 | + view.ViewConfigurations(response.Payload, expandedCategory) |
| 83 | + } |
| 84 | + return nil |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + cmd.Flags().StringVar(&category, "category", "", "Filter by category: authentication (auth), security (sec), system (sys)") |
| 89 | + cmd.Flags().StringVar(&category, "cat", "", "Filter by category (shorthand for --category)") |
| 90 | + |
| 91 | + return cmd |
| 92 | +} |
| 93 | + |
| 94 | +func expandCategoryShorthand(category string) string { |
| 95 | + switch strings.ToLower(category) { |
| 96 | + case "auth": |
| 97 | + return "authentication" |
| 98 | + case "sec": |
| 99 | + return "security" |
| 100 | + case "sys": |
| 101 | + return "system" |
| 102 | + default: |
| 103 | + return category |
| 104 | + } |
| 105 | +} |
0 commit comments