Debian Codegraph @

cmd/config/internal/commands/count.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 package commands
5
6 import (
7 "fmt"
8 "io"
9 "sort"
10
11 "github.com/spf13/cobra"
12 "sigs.k8s.io/kustomize/cmd/config/ext"
13 "sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
14 "sigs.k8s.io/kustomize/cmd/config/runner"
15 "sigs.k8s.io/kustomize/kyaml/kio"
16 "sigs.k8s.io/kustomize/kyaml/sets"
17 "sigs.k8s.io/kustomize/kyaml/yaml"
18 )
19
20 func GetCountRunner(name string) *CountRunner {
21 r := &CountRunner{}
22 c := &cobra.Command{
23 Use: "count [DIR]",
24 Args: cobra.MaximumNArgs(1),
25 Short: commands.CountShort,
26 Long: commands.CountLong,
27 Example: commands.CountExamples,
28 RunE: r.runE,
29 }
30 runner.FixDocs(name, c)
31 c.Flags().BoolVar(&r.Kind, "kind", true,
32 "count resources by kind.")
33 c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", true,
34 "prints count of resources recursively in all the nested subpackages")
35 r.Command = c
36 return r
37 }
38
39 func CountCommand(name string) *cobra.Command {
40 return GetCountRunner(name).Command
41 }
42
43 // CountRunner contains the run function
44 type CountRunner struct {
45 IncludeSubpackages bool
46 Kind bool
47 Command *cobra.Command
48 RecurseSubPackages bool
49 }
50
51 func (r *CountRunner) runE(c *cobra.Command, args []string) error {
52 if len(args) == 0 {
53 input := &kio.ByteReader{Reader: c.InOrStdin()}
54
55 return runner.HandleError(c, kio.Pipeline{
56 Inputs: []kio.Reader{input},
57 Outputs: r.out(c.OutOrStdout()),
58 }.Execute())
59 }
60
61 e := runner.ExecuteCmdOnPkgs{
62 Writer: c.OutOrStdout(),
63 NeedOpenAPI: false,
64 RecurseSubPackages: r.RecurseSubPackages,
65 CmdRunner: r,
66 RootPkgPath: args[0],
67 }
68
69 return e.Execute()
70 }
71
72 func (r *CountRunner) ExecuteCmd(w io.Writer, pkgPath string) error {
73 input := kio.LocalPackageReader{PackagePath: pkgPath, PackageFileName: ext.KRMFileName()}
74
75 err := kio.Pipeline{
76 Inputs: []kio.Reader{input},
77 Outputs: r.out(w),
78 }.Execute()
79
80 if err != nil {
81 // return err if there is only package
82 if !r.RecurseSubPackages {
83 return err
84 }
85 // print error message and continue if there are multiple packages to annotate
86 fmt.Fprintf(w, "%s\n", err.Error())
87 }
88 return nil
89 }
90
91 func (r *CountRunner) out(w io.Writer) []kio.Writer {
92 var out []kio.Writer
93 if r.Kind {
94 out = append(out, kio.WriterFunc(func(nodes []*yaml.RNode) error {
95 count := map[string]int{}
96 k := sets.String{}
97 for _, n := range nodes {
98 m, _ := n.GetMeta()
99 count[m.Kind]++
100 k.Insert(m.Kind)
101 }
102 order := k.List()
103 sort.Strings(order)
104 for _, k := range order {
105 fmt.Fprintf(w, "%s: %d\n", k, count[k])
106 }
107 return nil
108 }))
109 } else {
110 out = append(out, kio.WriterFunc(func(nodes []*yaml.RNode) error {
111 fmt.Fprintf(w, "%d\n", len(nodes))
112 return nil
113 }))
114 }
115 return out
116 }