forked from kubernetes-sigs/kustomize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
removeresource.go
102 lines (88 loc) · 2.27 KB
/
removeresource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package remove
import (
"errors"
"path/filepath"
"slices"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
type removeResourceOptions struct {
resourceFilePaths []string
}
// newCmdRemoveResource remove the name of a file containing a resource to the kustomization file.
func newCmdRemoveResource(fSys filesys.FileSystem) *cobra.Command {
var o removeResourceOptions
cmd := &cobra.Command{
Use: "resource",
Short: "Removes one or more resource file paths from " +
konfig.DefaultKustomizationFileName(),
Example: `
remove resource my-resource.yml
remove resource resource1.yml resource2.yml resource3.yml
remove resource resources/*.yml
`,
RunE: func(cmd *cobra.Command, args []string) error {
err := o.Validate(args)
if err != nil {
return err
}
return o.RunRemoveResource(fSys)
},
}
return cmd
}
// Validate validates removeResource command.
func (o *removeResourceOptions) Validate(args []string) error {
if len(args) == 0 {
return errors.New("must specify a resource file")
}
o.resourceFilePaths = args
return nil
}
// RunRemoveResource runs Resource command (do real work).
func (o *removeResourceOptions) RunRemoveResource(fSys filesys.FileSystem) error {
mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil {
return err
}
m, err := mf.Read()
if err != nil {
return err
}
resources, err := globPatterns(m.Resources, o.resourceFilePaths)
if err != nil {
return err
}
if len(resources) == 0 {
return nil
}
newResources := make([]string, 0, len(m.Resources))
for _, resource := range m.Resources {
if slices.Contains(resources, resource) {
continue
}
newResources = append(newResources, resource)
}
m.Resources = newResources
return mf.Write(m)
}
func globPatterns(resources []string, patterns []string) ([]string, error) {
var result []string
for _, pattern := range patterns {
for _, resource := range resources {
match, err := filepath.Match(pattern, resource)
if err != nil {
return nil, err
}
if !match {
continue
}
result = append(result, resource)
}
}
return result, nil
}