|
| 1 | +/* |
| 2 | +Copyright 2025 The Ketches Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "strings" |
| 22 | + |
| 23 | + krbclient "github.com/ketches/kube-recycle-bin/internal/client" |
| 24 | + "github.com/ketches/kube-recycle-bin/internal/completion" |
| 25 | + "github.com/ketches/kube-recycle-bin/pkg/tlog" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | +) |
| 29 | + |
| 30 | +type ViewFlags struct { |
| 31 | + ObjectResource string |
| 32 | + ObjectNamespace string |
| 33 | + OutputFormat string |
| 34 | +} |
| 35 | + |
| 36 | +var viewFlags ViewFlags |
| 37 | + |
| 38 | +var viewCmd = &cobra.Command{ |
| 39 | + Use: "view", |
| 40 | + Aliases: []string{"show", "display"}, |
| 41 | + Short: "View recyceled resource objects from RecycleItem", |
| 42 | + Example: ` |
| 43 | +# View recycled resource objects from RecycleItem with names foo and bar |
| 44 | +krb-cli view foo bar |
| 45 | +
|
| 46 | +# View recycled resource objects from RecycleItem deployments foo and filter by object resource deployments |
| 47 | +krb-cli view --object-resource deployments foo |
| 48 | +
|
| 49 | +# View recycled resource objects from RecycleItem deployments foo-deploy, service foo-svc and filter by object namespace dev |
| 50 | +krb-cli view --object-namespace dev foo-deploy foo-svc |
| 51 | +
|
| 52 | +# View recycled resource objects from RecycleItem with names foo and bar in JSON format |
| 53 | +krb-cli view foo bar --output json |
| 54 | +`, |
| 55 | + Args: cobra.MinimumNArgs(1), |
| 56 | + Run: func(cmd *cobra.Command, args []string) { |
| 57 | + runView(args) |
| 58 | + }, |
| 59 | + ValidArgsFunction: completion.RecycleItem, |
| 60 | +} |
| 61 | + |
| 62 | +func init() { |
| 63 | + rootCmd.AddCommand(viewCmd) |
| 64 | + |
| 65 | + viewCmd.Flags().StringVarP(&viewFlags.ObjectResource, "object-resource", "", "", "View recycled resource objects filtered by the specified object resource") |
| 66 | + viewCmd.Flags().StringVarP(&viewFlags.ObjectNamespace, "object-namespace", "", "", "View recycled resource objects filtered by the specified object namespace") |
| 67 | + viewCmd.Flags().StringVarP(&viewFlags.OutputFormat, "output", "o", "yaml", "Output format. One of: json|yaml, default is yaml") |
| 68 | + |
| 69 | + viewCmd.RegisterFlagCompletionFunc("object-resource", completion.RecycleItemGroupResource) |
| 70 | + viewCmd.RegisterFlagCompletionFunc("object-namespace", completion.RecycleItemNamespace) |
| 71 | + viewCmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 72 | + return []string{"json", "yaml"}, cobra.ShellCompDirectiveNoFileComp |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +func runView(args []string) { |
| 77 | + if len(args) == 0 { |
| 78 | + tlog.Panicf("✗ please specify recycle items to view.") |
| 79 | + } |
| 80 | + |
| 81 | + var objContents []string |
| 82 | + for _, recycleItemName := range args { |
| 83 | + recycleItem, err := krbclient.RecycleItem().Get(context.Background(), recycleItemName, client.GetOptions{}) |
| 84 | + if err != nil { |
| 85 | + tlog.Printf("✗ failed to get RecycleItem [%s]: %v, ignored.", recycleItemName, err) |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + switch viewFlags.OutputFormat { |
| 90 | + case "json": |
| 91 | + objContent, err := recycleItem.Object.IndentedJSON() |
| 92 | + if err != nil { |
| 93 | + tlog.Printf("✗ failed to view recycled resource object [%s: %s] from RecycleItem [%s] in JSON format: %s, error: %v", recycleItem.Object.Kind, recycleItem.Object.Key().String(), recycleItem.Name, recycleItem.Name, err) |
| 94 | + continue |
| 95 | + } |
| 96 | + objContents = append(objContents, objContent) |
| 97 | + // tlog.Printf("✓ recycled resource object [%s: %s] from RecycleItem [%s]: \n%s \n", recycleItem.Object.Kind, recycleItem.Object.Key().String(), recycleItem.Name, objContent) |
| 98 | + default: |
| 99 | + objContent, err := recycleItem.Object.YAML() |
| 100 | + if err != nil { |
| 101 | + tlog.Printf("✗ failed to view recycled resource object [%s: %s] from RecycleItem [%s] in YAML format: %s, error: %v", recycleItem.Object.Kind, recycleItem.Object.Key().String(), recycleItem.Name, recycleItem.Name, err) |
| 102 | + continue |
| 103 | + } |
| 104 | + objContents = append(objContents, objContent) |
| 105 | + // tlog.Printf("✓ recycled resource object [%s: %s] from RecycleItem [%s]: \n%s \n", recycleItem.Object.Kind, recycleItem.Object.Key().String(), recycleItem.Name, objContent) |
| 106 | + } |
| 107 | + } |
| 108 | + tlog.Print(strings.Join(objContents, "\n---\n")) |
| 109 | +} |
0 commit comments