func run()

in pkg/commands/filter/filter.go [63:101]


func run(ctx context.Context, o *options, brw cmdlib.BundleReaderWriter, gopt *cmdlib.GlobalOptions) error {
	bw, err := brw.ReadBundleData(ctx, gopt)
	if err != nil {
		return fmt.Errorf("error reading contents: %v", err)
	}

	fopts := &filter.Options{}
	if o.kinds != "" {
		fopts.Kinds = strings.Split(o.kinds, ",")
	}
	if o.names != "" {
		fopts.Names = strings.Split(o.names, ",")
	}
	if o.namespaces != "" {
		fopts.Namespaces = strings.Split(o.namespaces, ",")
	}
	if o.annotations != "" {
		fopts.Annotations = cmdlib.ParseStringMap(o.annotations)
	}
	if o.labels != "" {
		fopts.Labels = cmdlib.ParseStringMap(o.labels)
	}
	fopts.InvertMatch = o.invertMatch

	if o.filterType == "components" && bw.Bundle() != nil {
		bw.Bundle().Components = filter.NewFilter().FilterComponents(bw.Bundle().Components, fopts)
	} else if o.filterType == "objects" && bw.Bundle() != nil {
		for i, c := range bw.Bundle().Components {
			bw.Bundle().Components[i].Spec.Objects =
				filter.NewFilter().FilterObjects(c.Spec.Objects, fopts)
		}
	} else if o.filterType == "objects" && bw.Component() != nil {
		bw.Component().Spec.Objects = filter.NewFilter().FilterObjects(bw.Component().Spec.Objects, fopts)
	} else {
		return fmt.Errorf("unknown filter type: %s", o.filterType)
	}

	return brw.WriteBundleData(ctx, bw, gopt)
}